Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在while循环中使用try-except命令请求用户输入_Python_Loops_Try Except - Fatal编程技术网

Python 如何在while循环中使用try-except命令请求用户输入

Python 如何在while循环中使用try-except命令请求用户输入,python,loops,try-except,Python,Loops,Try Except,我是Python初学者,第一次尝试使用try和除了。我要求用户输入一个整数值,但是如果用户输入一个字符串,我想一次又一次地询问用户,直到给出一个整数,而不是结束程序 此时,如果用户给出一个字符串,则只要求用户给出一次另一个答案,但如果用户再次给出错误的输入,则程序停止 下面是我的意思的一个例子 我查看了有关Stackoverflow的类似问题,但没有任何建议可以解决 travel_score = 0 while True: try: travel_score = int

我是Python初学者,第一次尝试使用
try
除了
。我要求用户输入一个整数值,但是如果用户输入一个字符串,我想一次又一次地询问用户,直到给出一个整数,而不是结束程序

此时,如果用户给出一个字符串,则只要求用户给出一次另一个答案,但如果用户再次给出错误的输入,则程序停止

下面是我的意思的一个例子

我查看了有关Stackoverflow的类似问题,但没有任何建议可以解决

travel_score = 0

while True:
    try:
        travel_score = int(input("How many times per year do you travel? Please give an integer number"))
    except ValueError:
        travel_score = int(input("This was not a valid input please try again"))


print ("User travels per year:", travel_score)

问题是,一旦抛出
ValueError
异常,它将被捕获到
except
块中,但如果再次抛出,则不会有更多的
except
来捕获这些新错误。解决方案是仅在
try
块中转换答案,而不是在用户输入后立即转换

试试这个:

travel\u得分=0
is_int=False
回答=输入(“您每年旅行多少次?请给出一个整数:”)
虽然不是完整的:
尝试:
答案=int(答案)
这是真的吗
旅行分数=答案
除值错误外:
回答=输入(“这不是有效的输入,请重试:”)
打印(“用户每年旅行:”,旅行分数)

问题在于,第二次输入没有异常处理

travel_score = 0

while True:
    try:
        travel_score = int(input("How many times per year do you travel? Please give an integer number"))
    except ValueError:
        # if an exception raised here it propagates
        travel_score = int(input("This was not a valid input please try again"))


print ("User travels per year:", travel_score)
处理此问题的最佳方法是,如果用户的输入无效,则向用户返回一条信息性消息,并允许循环返回到开头并以这种方式重新提示:

# there is no need to instantiate the travel_score variable
while True:
    try:
        travel_score = int(input("How many times per year do you travel? Please give an integer number"))
    except ValueError:
        print("This was not a valid input please try again")
    else:
        break  # <-- if the user inputs a valid score, this will break the input loop

print ("User travels per year:", travel_score)
#无需实例化travel_score变量
尽管如此:
尝试:
旅行分数=int(输入(“你每年旅行多少次?请给出一个整数”))
除值错误外:
打印(“这不是有效的输入,请重试”)
其他:

break#@Luca Bezerras答案很好,但您可以让它更紧凑一些:

travel_score = input("How many times per year do you travel? Please give an integer number: ")

while type(travel_score) is not int:    
    try:
        travel_score = int(travel_score)
    except ValueError:
        travel_score = input("This was not a valid input please try again: ")


print ("User travels per year:", travel_score)

有关代码格式的帮助,请参阅此元问题:@SuperShoot No可能重复,它不会重复,因为对
int
的转换仅在
try
块中完成。@Malou当然,很高兴能提供帮助!:)的确,很好的重构:P