Python:从函数返回时没有值

时间:2017-11-07 11:24:37

标签: python function variables return nonetype

我正在编写一个程序,将3名玩家及其分数存储在一个列表中,然后在最后打印出来。真的很简单,但是我试图从名为playerscore()的函数中调用玩家得分的值,这会阻止你输入分数> 5.

当您使用正确的值运行它时,这可以正常工作,但如果您输入的值不正确> 5然后它再次开始玩家核心功能并允许输入新值但返回"无"

teamlist = []

def playercreator():
    counter=0
    while counter < 3:
        name = playername()
        score = playerscore()
        print(score) #check - this one goes wrong when returned after the if/else in playerscore()
        teamlist.append(name+" "+str(score))
        counter = counter + 1

def playername():
    name=input("What name do you want for your player?\n")
    return (name)

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
        playerscore()
    else:
        return int(score)

playercreator()

for x in teamlist:
    print(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

我觉得我有一些明显缺失的东西。有人能指出我正确的方向吗?

4 个答案:

答案 0 :(得分:4)

您在if块中缺少return语句(当分数大于5时):

def playerscore():
    global teamtotal
    score=input("input score?\n")
    if int(score)>5:
        print("Your attack score must be between 0 and 5")        
        return playerscore()
    else:
        return int(score)

输出:

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

来自official documentation

  

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

答案 1 :(得分:2)

当你这样做时:

if int(score)>5:
    playerscore()

您在没有playerscore声明的情况下调用return函数。这会产生None值。

答案 2 :(得分:1)

您尝试对代码进行的递归类型将对您的代码进行小幅修正......其后:

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)

我希望有所帮助。问候。

答案 3 :(得分:0)

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

因此,您应该检查代码中score大于5的情况。(将其与 大于5的情况相比较。)