mardi 22 janvier 2019

Repeating function first time through

I am running the following code and it loops through it with an output, but then goes to the 'elif' statement skipping the if statement the first time through. After the second time through, even if I give it the wrong answer, it stops. How can I get it to repeat with a new set of random numbers for an incorrect guess, but then accept it if it is correct?

from random import randint



def solve():
    a = randint(0,10)
    b = randint(0,10)
    total = a + b
    print(str(a) + ' + ' + str(b) + ' =')
    guess = input()
    if (guess == total):
        print("Correct")
    elif (guess != total):
        print('Try again')
        a = randint(0,10)
        b = randint(0,10)
        total = a + b
        print(str(a) + ' + ' + str(b) + ' =')
        guess = input()


solve()




Aucun commentaire:

Enregistrer un commentaire