Question

In: Computer Science

Develop a python program that prompts the user to take up the quiz (which has choices)...

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

Solutions

Expert Solution

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 :-


Related Solutions

Develop a python program to ask the user to take up the quiz(General knowledge) which has...
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.
Python A program which prompts the user to enter an number which is a integer n...
Python A program which prompts the user to enter an number which is a integer n and creates a tuple where the values are all numbers between 1 and n (both inclusive). Note: you can assume that the integer will always be > 1. Input Result 3 Enter an integer: 3 (1, 2, 3)
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
Write a Python program using functions and mainline logic which prompts the user to enter a...
Write a Python program using functions and mainline logic which prompts the user to enter a number, then generates that number of random integers and stores them in a file. It should then display the following data to back to the user: The list of integers The lowest number in the list The highest number in the list The total sum of all the numbers in the list The average number in the list At a minimum, the numbers should...
Write a Python program with correct indentation using functions and mainline logic which prompts the user...
Write a Python program with correct indentation using functions and mainline logic which prompts the user to enter a number, then generates that number of random integers and stores them in a list. It should then display the following data to back to the user: The list of integers The lowest number in the list The highest number in the list The total sum of all the numbers in the list The average number in the list At a minimum,...
python.Write a python program that prompts the user to enter the year and first day of...
python.Write a python program that prompts the user to enter the year and first day of the year, and displays the first day of each month in the year. For example, if the user entered the year 2020 and 3 for Wednesday, January 1, 2020, your program should display the following output: January 1, 2020 is Wednesday February 1, 2020 is Saturday …… December 1, 2020 is Tuesday
USE PYTHON. Write a program that prompts the user to enter 5 test scores. The program...
USE PYTHON. Write a program that prompts the user to enter 5 test scores. The program should display a letter grade for each score and the average test score. Hint: Declare local variables under main() program Prompts the user to enter 5 test scores Define a function to calculate the average score: this should accept 5 test scores as argument and return the avg Define a function to determine the letter grade: this should accept a test score as argument...
Write a program which prompts the user for the year of their birth, and which then...
Write a program which prompts the user for the year of their birth, and which then calculates their age (ignoring the month/day). Split the program into `main`, a "prompt for year" function which tests to make sure the user's input is a valid year, and a "calculate age" function which performs the actual calculation. (You can assume the current year is 2017.) for my intro to c++ class
Write a program In python of Jupiter notebook (using loops) that prompts the user to enter...
Write a program In python of Jupiter notebook (using loops) that prompts the user to enter his/her favorite English saying, then counts the number of vowels in that (note that the user may type the saying using any combination of upper or lower case letters). Example: Enter your favorite English saying: Actions speak LOUDER than words. Number of wovels: 10
Develop a python program to create a quiz with limited time and attempts!!! Add comments and...
Develop a python program to create a quiz with limited time and attempts!!! Add comments and screenshot of running the program quiz could be of anything like , what's the sum of 2&2. There should be number of attempts(limited) suppose if the answer is wrong.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT