[Python] Python Value exception [duplicate]

Discussão em 'Python' iniciado por Stack, Novembro 28, 2024.

  1. Stack

    Stack Membro Participativo

    I am a Python learner and just on the Exception module. I wrote this code, need two int input and then calculate sum, otherwise ask the user to re-enter the number which is not valid.

    Now, when num1 is not a number, the code can ask the user to re-enter, but when the second is not a number, it come to the top, asking to re-enter the first number, not the second.

    flag = True
    while flag:
    try:
    num1 = int(input("Please enter the first number:"))
    num2 = int(input("Please enter the second number:"))
    except ValueError:
    print("Please enter a number")
    continue
    else:
    sum = num1 + num2
    flag = False
    print(sum)


    I expect the code to get the user to re-enter the number (num1 or num2) which the input is invalid.

    Continue reading...

Compartilhe esta Página