In: Computer Science
Develop a python program to ask the user to take up the quiz(General knowledge) which has around 4 or 5 questions & choices for those questions.
For each question user should have 2 or 3 attempts suppose if they are wrong.
Develop the in such a manner to add few more questions and remove some questions.
CODE:
#this class depicts a question which has four options
#a question and a correct option
class Question():
#constructor
def
__init__(self,question,optionA,optionB,optionC,optionD,correctOptionNumber):
self.question = question
self.optionA = optionA
self.optionB = optionB
self.optionC = optionC
self.optionD = optionD
#the optionNumber should be either 1,2,3 or 4
if(correctOptionNumber not in range(1,5)):
raise Excpetion("invalid option entered as correct option")
self.correctOption = correctOptionNumber
#str() method
def __str__(self):
return "Q. {}\n1. {}\n2. {}\n3. {}\n4.
{}".format(self.question,
self.optionA,
self.optionB,
self.optionC,
self.optionD)
def main():
#list of Question objects in the list which can be added or
removed
Quiz = []
#appending the question objects in the list
Quiz.append(Question("What is the capital of United States of
America?",
"New York",
"Washington D.C.",
"California",
"Las Vegas",
2))
Quiz.append(Question("How many legs does a caterpillar has?",
"6",
"50",
"4",
"2",
1))
Quiz.append(Question("Who is the king of the jungle?",
"Cat",
"Dog",
"Lion",
"Cheetah",
3))
Quiz.append(Question("What is the capital of United
Kingdom?",
"Germany",
"Washington D.C.",
"Paris",
"London",
4))
right = 0
wrong = 0
#all the questions in the list is asked
for i in Quiz:
attempts = 0
correctlyAnswered = False
print("\n___________________________________________")
print(i)
print("___________________________________________")
while(True):
#asking the correct option
answer = int(input("Enter the correct option Number: "))
attempts += 1
if(answer == i.correctOption):
print('\nCorrect!')
#points gained if answered correctly
correctlyAnswered = True
break
else:
print('\nWrong! {} attempts left!'.format(str(2-attempts)))
#if two wrong attempts are made points lost
if(attempts == 2):
print("\nYou couldn't answer")
break
#if that question was answered correctly points scored otherwise
not
if(correctlyAnswered):
right += 1
else:
wrong += 1
#printing the score of the quiz
print("\nFinal Score:\nRight: {}\nWrong:
{}".format(str(right),str(wrong)))
main()
_________________________________________
CODE IMAGES:
_____________________________________________
OUTPUT:
_________________________________________
Feel free to ask any questions in the comments section
Thank You!