Question

In: Computer Science

Hello! Working with python This program asks user math questions and keeps track of answers... Allow...

Hello!

Working with python

This program asks user math questions and keeps track of answers...

  1. Allow the user to decide whether or not to keep playing after each math challenge.

  2. Ensure the user’s answer to each math problem is greater than or equal to zero.

  3. Keep track of how many math problems have been asked and how many have been answered correctly.

  4. 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!!")

Solutions

Expert Solution

Solution:

Change below lines in each method, { add(), sub(), div(), mul() }

  • Replace each user input line "users_result = int(input())" with "users_result = float(input())"
  • Round off user result up to 2 decimal places, i.e, add line users_result = round(users_result, 2)
  • Round off actual output up to 2 decimal places, i.e, result = round(result, 2)
  • Now, float values entered by user are handled properly.

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.


Related Solutions

create a program that asks user math questions and keeps track of answers... Python Allow the...
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...
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
Create a program that keeps track of the following information input by the user: First Name,...
Create a program that keeps track of the following information input by the user: First Name, Last Name, Phone Number, Age Now - let's store this in a multidimensional array that will hold 10 of these contacts. So our multidimensional array will need to be 10 rows and 4 columns. You should be able to add, display and remove contacts in the array. In Java
Write a python program that will allow a user to draw by inputting commands. The program...
Write a python program that will allow a user to draw by inputting commands. The program will load all of the commands first (until it reaches command "exit" or "done"), and then create the drawing. Must include the following: change attributes: color [red | green | blue] width [value] heading [value] position [xval] [yval] drawing: draw_axes draw_tri [x1] [y1] [x2] [y2] [x3] [y3 draw_rect [x] [y] [b] [h] draw_poly [x] [y] [n] [s] draw_path [path] random random [color | width...
Create an application that asks a user to answer 5 math questions. JAVA
Create an application that asks a user to answer 5 math questions. JAVA
In Python write a program that asks the user to enter the monthly costs for the...
In Python write a program that asks the user to enter the monthly costs for the following expenses incurred from operating his or her automobile: loan payment, insurance, gas, oil, tires, and maintenance the program should then display the total monthly cost of these expenses, and the total annual cost of these expenses. your program MUST have BOTH a main function AND a function named calcExpenses to calculate the expenses. DO NOT display the expenses inside of the calcExpenses function!!...
Write a design algorithm and a python program which asks the user for the length of...
Write a design algorithm and a python program which asks the user for the length of the three sides of two triangles. It should compute the perimeter of the two triangles and display it. It should also display which triangle has the greater perimeter. If both have the same perimeter it should display that the perimeters are the same. Formula: Perimeter of triangleA = (side1A + side2A +side3A) See the 2 sample outputs. Enter side1 of TriangleA: 2 Enter side2...
Python English algorithm explanation Write a program that asks the user for the name of a...
Python English algorithm explanation Write a program that asks the user for the name of a file in the current directory. Then, open the file and process the content of the file. 1)If the file contains words that appear more than once, print “We found duplicates.” 2)If the file does not contain duplicate words, print “There are no duplicates.”
Python English algorithm explanation Write a program that asks the user for the name of a...
Python English algorithm explanation Write a program that asks the user for the name of a file in the current directory. Then, open the file and process the content of the file. 1)If the file contains words that appear more than once, print “We found duplicates.” 2)If the file does not contain duplicate words, print “There are no duplicates.”
Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT