背景:Python的初学者;搜索这个问题,但我找到的答案更多的是关于“什么”而不是“为什么”
我打算做的是:创建一个函数,它接受用户输入的测试分数,并根据等级比例/曲线输出字母等级;下面是代码:score = input("Please enter test score: ")
score = int(score)
def letter_grade(score):
if 90 <= score <= 100:
print ("A")
elif 80 <= score <= 89:
print ("B")
elif 70 <= score <= 79:
print("C")
elif 60 <= score <= 69:
print("D")
elif score < 60:
print("F")
print (letter_grade(score))
执行时,返回:Please enter test score: 45
F
None
不打算使用None。我发现如果我使用(letter_grade(score)而不是print (letter_grade(score)),那么None将不再出现。
我能找到的最接近的答案是“python中的函数不返回任何值,除非明确指示执行其他操作”。但是我在最后一行调用了一个函数,所以我有点困惑。
所以我想我的问题是:是什么导致了None的消失?我确信这是非常基本的东西,但我找不到任何解释“幕后”机制的答案。所以我很感激有人能帮我解释一下。谢谢您!