【发布时间】:2016-03-10 17:46:47
【问题描述】:
你能告诉我为什么在下面的代码中使用了 try 和 except 吗? 为什么分数=-1?我的意思是为什么只有-1
inp = input('Enter score: ')
try:
score = float(inp)
except:
score = -1
if score > 1.0 or score < 0.0:
print ('Bad score')
elif score > 0.9:
print ('A')
elif score > 0.8:
print ('B')
elif score > 0.7:
print ('C')
elif score > 0.6:
print ('D')
else:
print ('F')
我们不能使用下面没有 try 和 except 命令的代码。
score = float(input('Enter score: '))
if score > 1.0 or score < 0.0:
print ('Bad score')
elif score > 0.9:
print ('A')
elif score > 0.8:
print ('B')
elif score > 0.7:
print ('C')
elif score > 0.6:
print ('D')
else:
print ('F')
【问题讨论】:
-
@elegent: 呃,
if not(0. <= score <= 1.):? -
@HughBothwell:是的 :) 谢谢你是对的!
标签: python python-3.x try-catch except