In: Computer Science
create a program that asks user math questions and keeps track of answers...
Python
Allow the user to decide whether or not to keep playing after each math challenge.
Ensure the user’s answer to each math problem is greater than or equal to zero.
Keep track of how many math problems have been asked and how many have been answered correctly.
When finished, inform the user how they did by displaying the total number of math problems, the number they answered correctly, and their percent correct.
Provide meaningful comments
A_count=0#variable used to count answer
Q_count=0#varible used to count question
flagg="y"#varible used to loop until user set it as N
def add():
global A_count#accessing global variable A_count
import random
a = random.randint(0,1000)#getting random numbers for
calculations
b = random.randint(0,1000)
result=a+b
if result<0:
#condition to check whether answer is less than zero if yes calls
fuction recursively
#until numbers with postive value is obtained as random
number
add()
else:
print("Find ",a,"+",b,"and Enter your Answer")
u_result=int(input())#reads user answer
if(u_result<0):
print("Result cannot be negative ! ! Do again")
add()
elif(u_result==result):#if user answer and calculated answer is
same answer is right and the Answer count incremented
print("Right Answer !!")
A_count+=1
else:
print("Wrong Answer !! Right answer is ",result)
def sub():
global A_count
import random
a = random.randint(0,1000)
b = random.randint(0,1000)
result=a-b
if result<0:
sub()
else:
print("Find ",a,"-",b,"and Enter your Answer")
u_result=int(input())
if(u_result<0):
print("Result cannot be negative ! ! Do again")
sub()
elif(u_result==result):
print("Right Answer !!")
A_count+=1
else:
print("Wrong Answer !! Right answer is ",result)
def div():
global A_count
import random
a = random.randint(0,1000)
b = random.randint(0,1000)
result=a/b
if result<0:
div()
else:
print("Find ",a,"/",b,"and Enter your Answer")
u_result=int(input())
if(u_result<0):
print("Result cannot be negative ! ! Do again")
div()
elif(u_result==result):
print("Right Answer !!")
A_count+=1
else:
print("Wrong Answer !! Right answer is ",result)
def mul():
global A_count
import random
a = random.randint(0,1000)
b = random.randint(0,1000)
result=a*b
if result<0:
mul()
else:
print("Find ",a,"*",b,"and Enter your Answer")
u_result=int(input())
if(u_result<0):
print("Result cannot be negative ! ! Do again")
mul()
elif(u_result==result):
print("Right Answer !!")
A_count+=1
else:
print("Wrong Answer !! Right answer is ",result)
while(flagg=="Y" or flagg=="y" ):#loops until user set it with
letter other than y
x=0
print("Enter your choice \n")
print("Menu\n1.Addition\n2.Substraction\n3.Division\n4.Multiplication\n5.Exit")
x=int(input())
if x==1:
add()
Q_count+=1
elif x==2:
sub()
Q_count+=1
elif x==3:
div()
Q_count+=1
elif x==4:
mul()
Q_count+=1
elif x==5:
flagg="N"
break;
else:
print("Invalid Entry")
print("Do you want to continue??Y/N")
flagg=input()
if Q_count !=0:
print("Total Number of problems attended : ",Q_count )
print("Right answers : ",A_count)
print("Percentage of Right answer :
",((A_count/Q_count)*100))
else:
print("No Questions Attempted!!")
Output
Enter your choice
Menu
1.Addition
2.Substraction
3.Division
4.Multiplication
5.Exit
1
Find 474 + 929 and Enter your Answer
1403
Right Answer !!
Do you want to continue??Y/N
y
Enter your choice
Menu
1.Addition
2.Substraction
3.Division
4.Multiplication
5.Exit
2
Find 695 - 40 and Enter your Answer
650
Wrong Answer !! Right answer is 655
Do you want to continue??Y/N
1
Total Number of problems attended : 2
Right answers : 1
Percentage of Right answer : 50.0