I'm trying to make my program run where if someone gets the question wrong (i.e. the user enters a number to large or too little that it will ask the user to input an Answer again. I've tried using While True: loops, but that makes my program have an infinite loop that never ends. Any help would be appreciated
import random
def main() :
# Generate two random numbers between 1 and 100
n1 = random.randint(1,100)
n2 = random.randint(1,100)
# Ask the answer
print("What is the sum:",n1,"+",n2)
attempt = 1
answer = int(input("Answer: "))
if answer != n1 + n2 and answer < n1 + n2:
print("Wrong! The correct answer is greater than " + str(answer) + ".")
else:
if answer != n1 + n2 and answer > n1 + n2:
print("Wrong! The correct answer is less than " + str(answer) + ".")
else:
if answer == n1 + n2:
print("Yes! The correct answer is " + str(answer) + "." + "\n You got it in " + str(attempt) + " attempts.")
exit()
main()
Aucun commentaire:
Enregistrer un commentaire