Python:检测输入错误以及如何提供解决方案

I am a beginner in python and made a couple of programs, one of the same things I see, is that when ever I make a code like this do = int(input("Enter number >")) I accidentally type a letter, I want to be able to give like a wrong input message and try again, but python would always detect the difference in variable container.

评论
陌路夜未央
陌路夜未央

You can catch ValueError:

success = False
while not success:
    try:
        do = int(input("Enter number >"))
        success = True
    except ValueError:
        print("Wrong input, please try again")
点赞
评论