In: Computer Science
Hello!
Working with python
This program asks user math questions and keeps track of answers...
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
I need for this code to allow user to enter a float as user result
and then round users result so it matches with actual rounded result
I need this done for add,sub,div, and mul
_____________________________________________________________________________________
Answer_count=0 #variable used to count answer
Question_count=0 #varible used to count question
keep_going="y" #varible used to loop until user set it as N
def add():
global Answer_count
#accessing global variable Answer_count
import random
a = random.randint(0,1000)
#getting random numbers for calculations
b = random.randint(0,1000)
result=a+b
round(result)
if result<0:
#condition to check whether answer is less than zero if yes calls function recursively
#until numbers with positive value is obtained as random number
add()
else:
print("Find ",a,"+",b,"and Enter your Answer")
users_result=int(input())
#reads user answer
if(users_result<0):
print("Result cannot be negative ! ! Do again")
add()
elif(users_result==result):
#if user answer and calculated answer is same answer is right and the Answer count incremented
print("Right Answer !!")
Answer_count+=1
else:
print("Wrong Answer !! Right answer is ",result)
def sub():
global Answer_count
import random
a = random.randint(0,1000)
b = random.randint(0,1000)
result=a-b
round(result)
if result<0:
sub()
else:
print("Find ",a,"-",b,"and Enter your Answer")
users_result=int(input())
if(users_result<0):
print("Result cannot be negative ! ! Do again")
sub()
elif(users_result==result):
print("Right Answer !!")
Answer_count+=1
else:
print("Wrong Answer !! Right answer is ",result)
def div():
global Answer_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")
users_result=int(input())
if(users_result<0):
print("Result cannot be negative ! ! Do again")
div()
elif(users_result==result):
print("Right Answer !!")
Answer_count+=1
else:
print("Wrong Answer !! Right answer is ",result)
def mul():
global Answer_count
import random
a = random.randint(0,1000)
b = random.randint(0,1000)
result=a*b
round(result)
if result<0:
mul()
else:
print("Find ",a,"*",b,"and Enter your Answer")
users_result=int(input())
if(users_result<0):
print("Result cannot be negative ! ! Do again")
mul()
elif(users_result==result):
print("Right Answer !!")
Answer_count+=1
else:
print("Wrong Answer !! Right answer is ",result)
while(keep_going=="Y" or keep_going=="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()
Question_count+=1
elif x==2:
sub()
Question_count+=1
elif x==3:
div()
Question_count+=1
elif x==4:
mul()
Question_count+=1
elif x==5:
keep_going="N"
break;
else:
print("Invalid Entry")
print("Do you want to continue??Y/N")
keep_going=input()
if Question_count !=0:
print("Total Number of problems attended : ",Question_count )
print("Right answers : ",Answer_count)
print("Percentage of Right answer : ",((Answer_count/Question_count)*100))
else:
print("No Questions Attempted!!")
Solution:
Change below lines in each method, { add(), sub(), div(), mul() }
Updated code of div() function:
def div():
global Answer_count
import random
a = random.randint(0,1000)
b = random.randint(0,1000)
result=a/b
result = round(result, 2) # Updated code
if result<0:
div()
else:
print("Find ",a,"/",b,"and Enter your Answer")
users_result=float(input()) # Updated code
users_result = round(users_result, 2) # Updated code
print("you typed: ", users_result)
if(users_result<0):
print("Result cannot be negative ! ! Do again")
div()
elif(users_result==result):
print("Right Answer !!")
Answer_count+=1
else:
print("Wrong Answer !! Right answer is ",result)
Input:
Enter your choice
Menu
1.Addition
2.Substraction
3.Division
4.Multiplication
5.Exit
3
Find 74 / 841 and Enter your Answer: 0.0822
Wrong Answer !! Right answer is 0.09
Enter your choice
Menu
1.Addition
2.Substraction
3.Division
4.Multiplication
5.Exit
5
Output:
Total Number of problems attended : 1
Right answers : 0
Percentage of Right answer : 0.0
Attaching screenshot of code to understand about indentation in python code
Make similar changes to other methods as well.
PS: Let me know if you are able to make changes to the code. Ask if you have any doubt.