FOR PYTHON: Write a function game() that teaches children how to add single-digi
ID: 3763060 • Letter: F
Question
FOR PYTHON: Write a function game() that teaches children how to add single-digit numbers. The function should take an integer n as a parameter and ask the child to answer n single-digit addition questions. The numbers should be chosen randomly from the range [0, 9] (that is including both 0 and 9). The user will enter the answer when prompted. Your function should print 'Correct' for correct answers and 'Incorrect' for incorrect answers. After n questions, the function should print the number of questions answered correctly. The function should also recover gracefully from non-integer values typed by the user. In those cases, the function should re-prompt the child to enter the answer without counting the invalid value either as correct or incorrect. The following shows several sample runs of the function:
Explanation / Answer
def game(n):
x = random.randrange (0,10)
y = random.randrange(0,10)
numbers = (x+y)
print (x, "+", y)
guess = eval(input("Enter your guess: "))
count = 0
total = 1
while total <= n:
x = random.randrange(0,10)
y = random.randrange(0,10)
numbers = x + y
print (x, "+", y)
guess = eval(input("Enter your guess: "))
total = total + 1
if guess == numbers:
count = count + 1
print("Correct.")
else:
print("Incorrect")
print("You got", count, "correct answers out of", total)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.