Enter the list of Python back when numbers are not the case law.

Asked 1 years ago, Updated 1 years ago, 105 views

If you receive non-numeric characters to create the list, you will be prompted again, but the code will return to the beginning. How can I ask you to re-enter the part where the error occurred?

while True:

    try:
         num1=int (input ("first digit input")
         num2=int (input ("second digit input")
         num3=int (input ("third digit")

    except ValueError:
        print (not a number). Re-enter.")

python list try-except

2022-09-21 17:58

1 Answers

def integer_input(msg):
    while True:
        try:
            n = int(input(msg))
            break
        except ValueError:
            print ("Enter an integer")
    return n

num1 = integer_input ("Enter the first digit :")

I think we can do it like this.


2022-09-21 17:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.