im trying to build a program that will take an integer n as input and then ask n single-digit addition questions. The numbers to be added should be chosen randomly from the range [0,9](i.e., 0 to 9 inclusive). THe user will enter the answer when prompted. the function should print 'correct' for correct answers and 'incorrect' for incorrect answers. After n questions, the function should print the number of correct answers.
>>>add(2)
8 + 2 =
Enter answer: 10
correct.
8 + 4 =
Enter answer: 5
Incorrect.
You got 1 correct answer out of 2
the code I got so far is:
import random
def game(n):
dig1 = random.randrange(0,10)
dig2 = random.randrange(0,10)
for i in range(n):
print (dig1, '+', dig2, '=')
answer = eval(input('Enter Answer: '))
if answer == dig1 + dig2:
print ('Correct.')
else:
print ('Incorrect.')
You need to create new random digits each loop:
def game(n):
for i in range(n):
dig1 = random.randrange(0,10)
dig2 = random.randrange(0,10)
print (dig1, '+', dig2, '=')
answer = eval(input('Enter Answer: '))
if answer == dig1 + dig2:
print ('Correct.')
else:
print ('Incorrect.')