Python:从函数返回时无值

Python:从函数返回时无值,python,function,variables,return,nonetype,Python,Function,Variables,Return,Nonetype,我正在编写一个程序,将3名球员及其分数存储在一个列表中,然后在最后打印出来。其实很简单,但是我试图从一个名为playerscore()的函数中调用玩家分数的值,该函数阻止您输入大于5的分数 当您使用正确的值运行它时,它可以正常工作,但是如果您输入的值不正确>5,则它将再次开始playerscore函数,并允许输入新值,但返回“无” teamlist=[] def playercreator(): 计数器=0 当计数器5: 打印(“您的攻击分数必须介于0和5之间”) 打印(分数)#检查 playe

我正在编写一个程序,将3名球员及其分数存储在一个列表中,然后在最后打印出来。其实很简单,但是我试图从一个名为playerscore()的函数中调用玩家分数的值,该函数阻止您输入大于5的分数

当您使用正确的值运行它时,它可以正常工作,但是如果您输入的值不正确>5,则它将再次开始playerscore函数,并允许输入新值,但返回“无”

teamlist=[]
def playercreator():
计数器=0
当计数器<3时:
name=playername()
分数=玩家分数()
打印(分数)#检查-在playerscore()中的if/else之后返回时,此项出错
团队列表。追加(姓名+“”+str(分数))
计数器=计数器+1
def playername():
name=input(“您希望您的播放器使用什么名称?\n”)
报税表(姓名)
def playerscore():
全球团队总数
分数=输入(“输入分数?\n”)
打印(分数)#检查
如果整数(分数)>5:
打印(“您的攻击分数必须介于0和5之间”)
打印(分数)#检查
playerscore()
其他:
返回整数(分数)
playercreator()
对于团队列表中的x:
打印(x)
例如,这些是输入和输出:

What name do you want for your player?
p1
input score?
3
What name do you want for your player?
p2
input score?
6
Your attack score must be between 0 and 5
input score?
5
What name do you want for your player?
p3
input score?
2

p1 3
p2 None
p3 2

我觉得我错过了一些明显的东西。有人能给我指出正确的方向吗?

如果if块中缺少返回语句(当分数大于5分时):

输出:

What name do you want for your player?
shovon
input score?
2
2
What name do you want for your player?
sorida
input score?
23
Your attack score must be between 0 and 5
input score?
43
Your attack score must be between 0 and 5
input score?
234
Your attack score must be between 0 and 5
input score?
1
1
What name do you want for your player?
shody
input score?
2
2
shovon 2
sorida 1
shody 2
发件人:

事实上,即使没有return语句的函数也会返回一个值,尽管这是一个相当无聊的值。此值称为None(它是一个内置名称)


进行此操作时:

if int(score)>5:
    playerscore()

调用
playerscore
函数时不使用
return
语句。这将生成
None
值。

当函数不返回任何内容时,值将为
None


因此,您应该检查代码中
得分
大于5的情况。(将其与分数不大于5的情况进行比较。)

您试图对代码执行的递归类型只需对代码进行少量更正即可。。。具体如下:

def playerscore():
global teamtotal
score=input("input score?\n")
print(score) #check
if int(score)>5:
    print("Your attack score must be between 0 and 5")
    print(score) #check
    return playerscore()
else:
    return int(score)
您可以注意到,这次我们返回了
playerscore()
。由于您似乎正在学习基础知识,我想提出一种稍微不同的方法,因为如果播放器键入字符串(一些字母)而不是数字,您将得到ValueError异常。您可以在异常捕获中继续使用递归函数,并使用while循环使播放器将数字保持在所需的范围内。下面是我防止ValueError异常的建议:

def playerscore():
global teamtotal
score=input("input score?\n")
try:
    while int(score) < 0 or int(score) > 5:
        print("Your attack score must be between 0 and 5")
        print(score)  # check
        score = input("input score?\n")
except ValueError:
    print("Your attack score must be A NUMBER between 0 and 5")
    return playerscore()
return int(score)
def playerscore():
全球团队总数
分数=输入(“输入分数?\n”)
尝试:
当int(分数)<0或int(分数)>5时:
打印(“您的攻击分数必须介于0和5之间”)
打印(分数)#检查
分数=输入(“输入分数?\n”)
除值错误外:
打印(“你的攻击分数必须是0到5之间的数字”)
return playerscore()
返回整数(分数)

我希望这有帮助。问候

就这么简单!我知道它必须是那样的,但是如果它不符合标准,我完全忘记了返回。谢谢你的帮助,太好了。我还没走到这一步,但最初计划捕捉错误。谢谢你的帮助。
def playerscore():
global teamtotal
score=input("input score?\n")
try:
    while int(score) < 0 or int(score) > 5:
        print("Your attack score must be between 0 and 5")
        print(score)  # check
        score = input("input score?\n")
except ValueError:
    print("Your attack score must be A NUMBER between 0 and 5")
    return playerscore()
return int(score)