Jan-15-2022, 03:04 PM
Hi,
I started learning Python yesterday. I'm trying to create a program that generates a sum of two random integers for the user to calculate. I want the code to generate a new calculation if the user chooses Yes after inputting the correct result and prompt the user to try again if the result is incorrect, but I can't figure out how to do it. Any hints, please? Thank you :)
This is what I have so far, but obviously wrong:
I started learning Python yesterday. I'm trying to create a program that generates a sum of two random integers for the user to calculate. I want the code to generate a new calculation if the user chooses Yes after inputting the correct result and prompt the user to try again if the result is incorrect, but I can't figure out how to do it. Any hints, please? Thank you :)
This is what I have so far, but obviously wrong:
import random
def random_calculation():
number1 = random.randint(1,100)
number2 = random.randint(1,100)
result = number1 + number2
user_result = int(input(f"Calculate: {number1} + {number2} = "))
if result == user_result:
new_calculation = input("Correct! New calculation? (Y/N) ")
if new_calculation == Y:
random_calculation()
else:
while result != user_result:
input(f"Incorrect. Try again. \nCalculate {number1} + {number2} = ")
random_calculation()
