问题描述:

I'm trying to get this code to loop until the user gives correct input but after the first loop if the user gives bad input again the program will crash and give me a ValueError

while True:

try:

input1,input2= input("Enter the Lat and Long of the source point separated by a comma eg 50,30").split(',')

break

except ValueError:

print ("please Use a Comma")

input1,input2 = input("Enter the Lat and Long of the source point separated by a comma eg 50,30").split(',')

网友答案:

The trick would be in using the continue statement when you get the exception. That will restart the loop and go into the area protected by the try statement so if you get another exception it won't crash your program.

while True:
    try:
        value1, value2 = input("Enter the Lat and Long of the source point separated by a comma eg 50,30: ").split(',') 
        break
    except ValueError:
        print("Please Use a Comma")
        continue
相关阅读:
Top