这个 Python 2.x 程序的输出是啥? [复制]
Posted
技术标签:
【中文标题】这个 Python 2.x 程序的输出是啥? [复制]【英文标题】:What will be the output of this Python 2.x program? [duplicate]这个 Python 2.x 程序的输出是什么? [复制] 【发布时间】:2017-09-26 14:09:38 【问题描述】:这是程序:
score = raw_input("Enter your score between 0.0 and 1.0: ")
try:
fscore = float(score)
except:
print "Enter a numerical value for score."
exit()
if fscore >= 0.0 and fscore <= 1.0:
if fscore >= 0.9:
print "A"
elif fscore >= 0.8:
print "B"
elif fscore >= 0.7:
print "C"
elif fscore >= 0.6:
print "D"
elif score >= 0.5:
print "E"
elif score < 0.5:
print "F"
else:
print "Score is out of range."
这是一个简单的程序,可以根据学生的分数给他们评分。如果你仔细注意到,我在第 4 个elif
语句中犯了一个语义错误。我没有写elif fscore >= 0.5
,而是写了elif score >= 0.5
。
让我感兴趣的是,如果我在score
中传递0.4
,这个程序会给我输出E
。我不应该收到错误吗?即使我没有收到错误,为什么E
?为什么不A
或B
或其他任何东西?
【问题讨论】:
我是这个社区和 python 的新手。我不知道要搜索什么。 【参考方案1】:我相信这与此线程中提供的答案有关: Compare string to float in Python
请注意,您实际上是在将字符串(用户的输入)与浮点数(在 elif 中设置的 0.5)进行比较。显然这是一个设计错误。
注意:我之前也不知道这个:)
【讨论】:
感谢您的回复。但它一直是市场重复的,我得到了答案。 :)以上是关于这个 Python 2.x 程序的输出是啥? [复制]的主要内容,如果未能解决你的问题,请参考以下文章