python - 如何从此 python 代码中获得正确的输出?
问题描述
我有一个 python 代码,它无法获得正确的输出。代码如下所示:
score = int(input("Please input a score: "))
grade = ""
if score < 60:
grade = "failed"
elif score < 80: # between 60 and 80
grade = "pass"
elif score < 90:
grade = "good"
else:
grade = "excellent"
print("score is (0), level is (1)".format(score,grade))
谁能告诉我问题出在哪里?太感谢了!
解决方案
您应该将 if 语句更改为:
if score <= 60:
grade = "failed"
elif score <= 80 and score > 60: # between 60 and 80
grade = "pass"
elif score <= 90 and score > 80:
grade = "good"
elif score > 90: #Assuming there is no max score since before, you left the last statement as an else.
grade = "excellent"
else: #Not necessary but always nice to include.
print("Not a valid score, please try again.")
而且,正如@loocid 评论的那样,更改print("score is (0), level is (1)".format(score,grade))
为print("score is {0}, level is {1}".format(score,grade))
虽然我更喜欢
print("score is " + str(score) + ", level is " + str(grade))
推荐阅读
- git - 如何在不添加自己的子模块的情况下添加子模块?
- c++ - find by hash value from an unordered_set which element is cumtomed type
- salesforce - 自动播放在闪电轮播中不起作用
- java - How to remove the empty icon gap from a JMenuItem (Java Swing)?
- unity3d - 为什么着色器不包括构建?
- ios - 有没有办法访问蓝牙或耳机的功能并通过 ios swift 代码触发它们
- delphi - 使用 Delphi 从 Outlook 收件箱收集电子邮件
- flutter - 颤振:从数组中调用特定元素
- java - Spock - 模拟私人领域
- jsf - 将 JSF2.2 应用程序从 Glassfish3 升级到 Glassfish5