In: Computer Science
Develop a python program that prompts the user to take up the quiz (which has choices) with limited attempts and limited time.
Suppose if the user answered wrong then they have to get numer of attempts(limited)
Quiz should have questions on General knowldge & multiple choices for those questions
Code for the given Question :-
import random
import time
import os
# function to restart the program
def restart():
restart = input("Would you like to play again ? (Y/N) ")
if restart.lower() == "yes" or restart.lower() == "y":
clear = lambda: os.system('cls')
clear()
script()
if restart.lower() == "n" or restart.lower() == "no":
print("Goodbye")
print("=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=")
exit()
#class for storing questions data
class QA:
def __init__(self, question, correctAnswer, otherAnswers):
self.question = question
self.corrAnsw = correctAnswer
self.otherAnsw = otherAnswers
# here you can add more questions in the same format
# QA("ques","Ans",["other options"])
qaList = [
QA("What year was Apple Inc. founded?", "1976", ["1978","1980","1974"]),
QA("What is the unit of currency in Laos?", "Kip", ["Ruble","Konra","Dollar"]),
QA("What event marked the start of World War II?", "Invasion of Poland (1939)", ["Invasion of Russia (1942)","Battle of Britain (1940)","Invasion of Normandy (1944)"]),
QA("What year is considered to be the year that the British Empire ended?", "1997", ["1986","1981","1971"]),
QA("What country is not a part of Scandinavia?", "Finland", ["Norway","Sweden","Denmark"]),
QA("What is the largest lake in the African continent?", "Lake Victoria", ["Lake Tanganyika","Lake Malawi","Lake Turkana"]),
QA("How many stars are featured on New Zealand's flag?", "4", ["5","2","0"]),
]
# function to check remaining time
def checktime( start_time):
elapsed_time = time.time() - start_time
if elapsed_time > seconds:
print("TIME OVER")
return 0
else:
print("Remaining time:",int((seconds-elapsed_time)))
return 1
# function that displays ques and check the ans is correct or not
def play_quiz():
corrCount = 0
random.shuffle(qaList)
start_time = time.time()
while True:
if not checktime( start_time):
break
for question in qaList:
if not checktime( start_time):
break
print(question.question)
print("Possible answers are:")
possible = question.otherAnsw + [question.corrAnsw]
random.shuffle(possible)
count = 0
while count < len(possible):
print(str(count + 1) + ": " + possible[count])
count += 1
print("Please enter the number of your answer:")
userAnsw = input()
while not userAnsw.isdigit():
print("That was not a number. Try again:")
userAnsw = input()
userAnsw = int(userAnsw)
while not (0 < userAnsw <= len(possible)):
userAnsw = int(input("That number doesn't correspond to any answer. Please enter the number of your "
"answer:"))
attempts_remaining = no_of_attempts
while attempts_remaining - 1:
if possible[userAnsw - 1] == question.corrAnsw:
print("Your answer was correct.")
print("")
corrCount += 1
break
else:
print("Your answer was incorrect.")
attempts_remaining -= 1
print("Tries remaining:{}".format(attempts_remaining))
print("Try again")
if not checktime(start_time):
break
userAnsw = int(input())
if attempts_remaining - 1 == 0:
print("Your answer is incorrect")
print("Correct answer is {}".format(question.corrAnsw))
print("")
if not checktime( start_time):
break
print("Your score is", corrCount)
if corrCount >= int(len(qaList) / 1.5):
print("Congratulations You Won!!!")
print("=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=")
else:
print("Sorry you failed.")
print("=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=")
restart()
# this is the main function
def script():
print("Want to test your General Knowledge Skills?")
print("You will be given {} questions and have to get at least {} correct to win in {} seconds".format(len(qaList),
int(len(qaList) / 1.5),seconds))
print("For each incorrect answer you will get {} attempts".format(no_of_attempts))
inp = input("Do you want to start? (Y/N) ")
if inp.lower() == "y" or inp.lower() == "yes":
play_quiz()
elif inp.lower() == "n" or inp.lower() == "no":
exit()
else:
print("Invalid input")
script()
no_of_attempts = 2 # here you can set the no. of attempt given per ques to user
seconds = 1*60 # here you can set the time for your quiz
script()
Screenshots of above code for indentation reference :-
Output of the Above code :-