Python中不支持的操作数类型

Python中不支持的操作数类型,python,Python,我是python新手,在函数/正确定义某些东西方面遇到困难。 现在我得到了TypeError:不支持的+操作数类型:'NoneType'和'NoneType' 我必须以某种方式输出它,并满足给定给我的特定方向,但我似乎无法理解这一点。任何帮助都将不胜感激。我将包括我必须遵守的规则的图像,我知道如何在不遵守规则的情况下使其工作,但在我试图坚持规则的情况下无法使其工作。 score\u input没有return语句,因此它返回None您不从函数score\u input返回输入。因此返回类型为N

我是python新手,在函数/正确定义某些东西方面遇到困难。 现在我得到了TypeError:不支持的+操作数类型:'NoneType'和'NoneType'

我必须以某种方式输出它,并满足给定给我的特定方向,但我似乎无法理解这一点。任何帮助都将不胜感激。我将包括我必须遵守的规则的图像,我知道如何在不遵守规则的情况下使其工作,但在我试图坚持规则的情况下无法使其工作。


score\u input
没有
return
语句,因此它返回
None

您不从函数
score\u input
返回输入。因此返回类型为
None

为了使代码正常工作,您需要在函数中添加
return{expression}

以下是我将如何更改代码:

# A function that prompts the user for a name and returns it to the
# calling statement.
def getName():
    name = input("Please enter your name: ")
    return name

# A function that prompts the user for a score and returns it to the
# calling statement. 
def score_input():
    return int(input("Enter your score: "))
    
# A function that receives two numbers and returns the average of those
# two values to the calling statement.
def find_avg():
    return (score_a + score_b) / 2
    
# A function that receives a string and a number (the name and the
# average score) and prints it out on the screen in the appropriate format.
def output():
    print("Hi, {}. Your average score is {}".format(name, avg))

#############################################################################

# prompt for name
name = getName()

# prompt for two scores
score_a = score_input()
score_b = score_input()

# calculate the average
avg = find_avg()

# display the final output
output()



您的两个函数忽略了返回计算值,因此默认情况下它们返回
None
。只需将其更改为:

def score_input():
    return int(input("Enter your score: "))
  # ^^^^^^ added return

def find_avg():
    return (score_a + score_b) / 2
  # ^^^^^^ added return
当你打电话给他们时,你不会得到
None
s

旁注:依赖非常量全局变量通常被认为是不好的形式。我建议将
output
更改为可重用的,方法是让它接收要作为参数输出的项,例如:

def output(name, avg):
    print("Hi, {}. Your average score is {}".format(name, avg))
然后用以下方式称呼它:

output(name, avg)
find_avg
也是如此。将脚本功能打包到
main
方法中有助于避免意外地依赖globals,而globals会得到这样的最终代码(为简洁起见,省略注释):


该错误发生在哪一行?请提供一个类似于在getName中返回用户名的值,您应该返回在score_input()和find_avg()中处理的值。由于它们当前不返回任何值,Python将为每个函数分配的值视为“无”。请提供预期值。显示中间结果与您预期的不同之处。我们应该能够复制和粘贴一个连续的代码块,执行该文件,并再现您的问题以及跟踪问题点的输出。显然,您希望这些变量具有正常值,而不是
None
。你认为他们从哪里得到这些价值观?他们是怎么变成无的?在这里发布之前,您应该对此进行跟踪。除getName之外的所有函数都不返回任何内容,也就是说它们不返回任何内容。当您看到
NoneType
错误时,一般经验法则是:查找不返回任何内容的函数。
find\u avg
也有相同的问题。@ShadowRanger:是,但是,
score\u输入
导致引用的错误。
output(name, avg)
def getName():
    return input("Please enter your name: ")

def score_input():
    return int(input("Enter your score: "))
    
def find_avg(a, b):
    return (a + b) / 2
    
def output(name, avg):
    print("Hi, {}. Your average score is {}".format(name, avg))

#############################################################################

# Wrapping script functionality in main ensures all variables are local,
# not global, which ensures you don't accidentally depend on global state
def main():
    name = getName()
    
    score_a = score_input()
    score_b = score_input()
    
    avg = find_avg(score_a, score_b)
    
    output(name, avg)

if __name__ == '__main__':
    main()  # Called only when invoked as a script, not when imported as a module