当输入为空时,我有以下代码给我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
答案 0 :(得分:0)
这是因为您无法将空的string
转换为int
,但是您可以使用try
和{{1} }块来处理:
except