employee = float(raw_input('Employee code number or 0 for guest:') or 0.0)
if employee == isalpha:
print "Nice try buddy"
print "Welcome BIG_OLD_BUDDY" 此代码不能识别字母输入。
发布于 2017-08-09 11:43:32
有两种方法。
try:
employee = float(raw_input('Employee code number or 0 for guest: ') or 0.0)
except KnownException:
# You can handle Known exception here.
pass
except Exception, e:
# notify user
print str(e)employee = raw_input('Employee code number or 0 for guest:')
if(employee.isalpha()):
print "Nice try buddy"
print "Welcome BIG_OLD_BUDDY"
else:
print "Your employee no is:" + str(employee)除非有可能出现未知异常,否则
不会使用try和catch。建议使用if和else来处理事情。
Read more about : why not to use exceptions as regular flow of control
发布于 2017-08-09 11:25:07
试一试
try:
employee = float(raw_input('Employee code number or 0 for guest: ') or 0.0)
except ValueError:
print "Nice try buddy"
print "Welcome BIG_OLD_BUDDY"发布于 2017-08-09 11:30:40
您可以按照另一个答案的建议使用try/except,如果您想使用str.isalpha(),则必须对字符串调用它,而不是将其与字符串进行比较。例如:
employee = raw_input('Employee code number or 0 for guest:')
if employee.isalpha():
print "Nice try buddy"
print "Welcome BIG_OLD_BUDDY"
else:
employee = float(employee)https://stackoverflow.com/questions/45581155
复制相似问题