Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从这个python代码中获得正确的输出?_Python - Fatal编程技术网

如何从这个python代码中获得正确的输出?

如何从这个python代码中获得正确的输出?,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 = "ex

我有一个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))
score=int(输入(“请输入分数:”)
grade=“”
如果得分<60:
grade=“失败”
elif分数<80:#介于60和80之间
grade=“通过”
elif评分<90分:
grade=“好”
其他:
grade=“优秀”
打印(“分数为(0),等级为(1)”。格式(分数,等级))

谁能告诉我哪里出了问题?非常感谢你

您应该将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.")
if score 90:#假设之前没有max score,您将最后一条语句保留为else。
grade=“优秀”
其他:#没有必要,但包含在内总是很好的。
打印(“不是有效分数,请重试。”)
并且,正如@loocid所评论的,将
print(“分数为(0),级别为(1)”。格式(分数,等级))更改为
print(“分数为{0},级别为{1}”。格式(分数,等级))

虽然我更喜欢


print(“分数是“+str(分数)+”,级别是“+str(成绩))

{0}
而不是打印字符串中的
(0)
。@Loocid和
{1}
而不是
(1)
@Loocid,非常感谢!我会对这个问题投反对票,但我没有这样做的名声,因为我是一个相当不活跃的用户(根据某些标准,我是新用户)。:(