【发布时间】:2020-11-27 12:31:54
【问题描述】:
def aboveIntegerInput(output_message="Enter your number: ", error_1="Please enter a number above {}!", error_2="Integers only!(Please do not leave this blank)", above=0):
while True:
try:
user_input = int(input(output_message))
if user_input >= above:
return int(user_input)
break
else:
print(error_1.format(above))
except ValueError:
print(error_2)
正如您在此处看到的,代码应该检查输入是否为整数,并且是否高于某个值,该值默认为 0,但可以更改。
当用户输入随机字母和符号时,它会看到值错误并返回“仅限整数!(请不要留空)”。
我希望能够检查用户是否没有输入任何内容,在这种情况下,只有它应该输出“这是空白/空”,目前处理这个问题的方法是根本不检查,只说“整数仅!(请不要将此留空)”,以防出现值错误。我希望能够更具体,而不是一次吐出所有的原因。谁能帮帮我?
提前致谢。
【问题讨论】:
标签: python validation input