Question

In: Computer Science

Program a math quiz on Python Specifications: - The program should ask the user a question...

Program a math quiz on Python

Specifications:

- The program should ask the user a question and allow the student to enter the answer. If the answer is correct, a message of congratulations should be displayed. If the answer is incorrect, a message showing the correct answer should be displayed.

- the program should use three functions, one function to generate addition questions, one for subtraction questions, and one for multiplication questions.

- the program should store the questions and answers in a dictionary or list.

- the program should keep a count of the number of correct and incorrect responses.

Solutions

Expert Solution


import random as rand

# generating random question by random generation of numbers
# below is the function for addition of questions
def additionQuestions():
    num1 = rand.randint(1,20)
    num2 = rand.randint(1,20)
    ans = num1 + num2
    question = "{} + {} = ".format(num1, num2)
    return [question, ans]

# below is the function for subtraction of questions
def subtractQuestions():
    num1 = rand.randint(1,20)
    num2 = rand.randint(1,20)
    ans = num1 - num2
    question = "{} - {} = ".format(num1, num2)
    return [question, ans]

# below is the function for multiplication of questions
def multiplyQuestions():
    num1 = rand.randint(1,20)
    num2 = rand.randint(1,20)
    ans = num1 * num2
    question = "{} x {} = ".format(num1, num2)
    return [question, ans]

correct = 0
incorrect = 0

dict = { }
for i in range(0,5):
    # generationg random number from 1 to 3
    # if 1 then we have to generate addition questions
    # if 2 then we have to generate subtraction questions
    # if 3 then we have to generate multiplication questions
    randNum = rand.randint(1,3)
    if randNum == 1:
        dict[i] = additionQuestions()
    if randNum == 2:
        dict[i] = subtractQuestions()
    if randNum == 3:
        dict[i] = multiplyQuestions()

    # retrieving questions and answers
    question = dict[i][0]
    answer = dict[i][1]

    # reading users answer and validating users answer
    try:
        userAnswer = int(input(question))
    except:
        incorrect += 1
        print("Invalid Answer")
        print("Correct Answer : ", answer)
        print("--------------------------------")
        continue

    if userAnswer == answer:
        correct += 1
        print("Congratulations")
    else:
        incorrect += 1
        print("Incorrect Answer")
        print("Correct Answer : ",answer)
    print("--------------------------------")

# printing the report of result
print("------------ Report ------------")
print("Correct Answers   : ",correct)
print("Incorrect Answers : ",incorrect)
print("Total Answers     : ",(correct+incorrect))

Code Screenshot:

Output:


Related Solutions

Need a program in java that creates a random addition math quiz The program should ask...
Need a program in java that creates a random addition math quiz The program should ask the user to enter the following The smallest and largest positive numbers to be used when generating the questions - The total number of questions to be generated per quiz - The total number of the quiz's to create from then the program should generate a random math (Just addition) quiz from what the user entered
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.
Design and write a Python 3.8 program that gives and grades a math quiz. The quiz...
Design and write a Python 3.8 program that gives and grades a math quiz. The quiz will give 2 problems for each of the arithmetic operations: +, -, *, /, and %. First, two addition problems will be presented (one at a time) with random numbers between 1 and 20. Then two subtraction problems, multiplication, division and modulus. For division use // and do floor division; for example: for 10//3, the answer should be 3. For an example of the...
In python. Projectile motion: Write a python program that will ask the user for      an...
In python. Projectile motion: Write a python program that will ask the user for      an initial height y0, initial velocity v, launch angle theta, and mass m.      Create two functions, one that will calculate max height      of the projectile, and one that will calculate the range. Ask the     user which one he/she would like to calculate, then present them with the answer. (use kg, m and m/s)
Calculating Delivery Cost Program in Python write a program in Python that will ask a user...
Calculating Delivery Cost Program in Python write a program in Python that will ask a user to enter the purchase total, the number of the items that need to be delivered and delivery day. Then the system displays the cost of delivery along with the total cost. Purchase total > $150 Yes Number of the items (N) N<=5 N>=6 Delivery day Same Day Next Day Same Day Next Day Delivery charges ($) 8 N * 1.50 N * 2.50 N...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will first enter a password, then enters the same password again; If the second input is the same as first one, the user successfully creates the password. Print “Well done.”; Otherwise, the user will be directed to repeat the whole process (go to step 1.)
Write a Python program that will ask the user to input a word, will return the...
Write a Python program that will ask the user to input a word, will return the first letter of the input word, and ask the user to put another word, and so on, in the form of a loop. If the user chooses to stop, he or she should input the integer "0" for the loop to stop.
Flowchart + Python. Ask the user for a value. Then, ask the user for the number...
Flowchart + Python. Ask the user for a value. Then, ask the user for the number of expressions of that value. Use While Loops to make a program. So then, it should be so that 5 expressions for the value 9 would be: 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 Flowcharts and Python Code. Not just Python code. I cannot read handwriting....
Number Analysis Program (Specific Design Specifications) Design a Python program that asks the user to enter...
Number Analysis Program (Specific Design Specifications) Design a Python program that asks the user to enter a series of 20 numbers. The program should store the numbers in a list then display the following data: The lowest number in the list The highest number in the list The total of the numbers in the list The average of the numbers in the list This python program must include the following functions. Your program must use the exact names, parameter lists,...
PYTHON Modify the program in section Ask the user for a first name and a last...
PYTHON Modify the program in section Ask the user for a first name and a last name of several people.  Use a loop to ask for user input of each person’s first and last names  Each time through the loop, use a dictionary to store the first and last names of that person  Add that dictionary to a list to create a master list of the names  Example dictionary: aDict = { "fname":"Douglas", "name":"Lee" } ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT