python - why are the right answers coming out wrong -
from random import randint correct = 0 in range(10): n1 = randint(1, 10) n2 = randint(1, 10) prod = n1 * n2 ans = input("what's %d times %d? " % (n1, n2)) if ans == prod: print ("that's right -- done.\n") correct = correct + 1 else: print ("no, i'm afraid answer %d.\n" % prod) print ("\ni asked 10 questions. got %d of them right." % correct) print ("well done!")
what's 1 times 5? 5 no, i'm afraid answer 5.
what's 9 times 3? 27 no, i'm afraid answer 27.
what's 4 times 1? 4 no, i'm afraid answer 4.
you have convert input string number:
for in range(10): n1 = randint(1, 10) n2 = randint(1, 10) prod = n1 * n2 ans = int(input("what's %d times %d? " % (n1, n2))) if ans == prod: print("that's right -- done.\n") correct += 1 else: print("no, i'm afraid answer %d.\n" % prod) print("\ni asked 10 questions. got %d of them right." % correct) print("well done!")
Comments
Post a Comment