age = raw_input("Age: ")
while not age: # In Python, empty strings meet this condition. So does [] and {}. :)
print "Error!"
age = raw_input("Age: ")
您可以为此创建包装器函数。你知道吗
def not_empty_input(prompt):
input = raw_input(prompt)
while not input: # In Python, empty strings meet this condition. So does [] and {}. :)
print "Error! No input specified."
input = raw_input(prompt)
return input
然后:
address = not_empty_input("Address: ")
age = not_empty_input("Age: ")
对于
while
循环,无需编写input()
函数两次:您还可以检查输入是否为整数,并从字符串中获取整数。
age
的空字符串也会引发ValueError
异常:您可以为此创建包装器函数。你知道吗
然后:
相关问题 更多 >
编程相关推荐