jeudi 18 juin 2020

try & if statement in while loop

I would like to build a function in which if the entered integer is between 1 and 10, return the result.

Here is my code:

while True:
    try:
        num = int(input("Enter a number (1-10): "))
    except ValueError:
        print("Wrong input")            
    else:
        if 1 <= num <= 9:
            break
        else:
            print("Wrong input")
            continue

When you enter an integer, the break does not function properly and it seems to go into a definite loop. Is it wrong to incorporate if statement into else?

Aucun commentaire:

Enregistrer un commentaire