提供空白输入时,以int()为底数10的int()无效文字

时间:2018-06-28 07:13:04

标签: python-2.7

当输入为空时,我有以下代码给我ValueError

U = find_lastuid()   # Return variable from other function
uidnum = int(raw_input("What is uid number? (default is: %s)" % U))
if not uidnum:
    print("defualt uid is used: %s" % uidnum)
else:
    print("UID is %s" % uidnum)


uidnum = int(raw_input("What is uid number? (default is: %s)" % U))
ValueError: invalid literal for int() with base 10: ''

有人可以告诉您此代码出了什么问题吗? 我可以在REPL中看到类似的代码

>>> id = 234
>>> a = raw_input("Enter id")
Enter id
>>> if not id:
...     print(id is blank)
... else:
...     print(id)
...
234

1 个答案:

答案 0 :(得分:0)

这是因为您无法将空的string转换为int,但是您可以使用try和{{1} }块来处理:

except