【发布时间】:2014-11-01 18:18:05
【问题描述】:
我正在 python 3 中编写代码,我需要根据输入的内容输出两个不同的字符串。 它们都是 ValueErrors。
try:
number = False
while number == False:
chooseword = int(input("Please enter an integer number (0<=number<10) to choose the word in the list: "))
except ValueError: #empty input
print("Empty input!")
except ValueError: #non-integer, non-empty input
print("Input must be an integer!")
else: #do stuff
我已经尝试过这个问题的方法,但我只收到一条打印的消息。 How to catch empty user input using a try and except in python?
我还尝试通过使用 while 循环并尝试使用 try except 块捕获另一个选项来忽略两个选项之一中的 ValueError:
empty = True
while empty == True:
chooseword = int(input("Please enter an integer number (0<=number<10) to choose the word in the list: "))
if chooseword = "":
empty = True
【问题讨论】:
-
if chooseword = "":是语法错误(单个=,在if语句中是非法的)。