[英]Python: None value when returning from function
I'm writing a program that stores 3 players and their scores in a list and then prints them out at the end. 我正在编写一个程序,将3个球员及其得分存储在列表中,然后在最后打印出来。 Simple stuff really, however I'm trying to call the value for the player score from a function called playerscore() which prevents you from entering a score >5.
确实很简单,但是我试图通过一个名为playerscore()的函数调用玩家得分的值,该函数阻止您输入得分> 5。
this works fine when you run through it with the correct value, but if you input an incorrect value > 5 then it begins the playerscore function again and allows for a new value to be entered but returns "None" 当您使用正确的值运行它时,它工作正常,但是如果输入的错误值> 5,它将再次启动playerscore函数,并允许输入新值,但返回“ None”
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)
for example, these are the inputs and outputs: 例如,这些是输入和输出:
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
I feel like there's something obvious that i'm missing. 我觉得我很想念一些东西。 Can anyone point me in the right direction?
谁能指出我正确的方向?
You are missing a return statement in the if block (when the score is greater than 5): 您在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)
Output: 输出:
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
From official documentation : 根据官方文件 :
In fact, even functions without a return statement do return a value, albeit a rather boring one.
实际上,即使没有return语句的函数也确实会返回一个值,尽管这很无聊。 This value is called None (it's a built-in name).
此值称为“无”(这是一个内置名称)。
When you are doing this: 执行此操作时:
if int(score)>5:
playerscore()
you call playerscore
function without return
statement. 您调用
playerscore
函数而无需return
语句。 This produces None
value. 这将产生
None
值。
The type of recursion you're trying to do with your code will work with a small correction on your code... which follows: 您尝试对代码进行的递归类型将对您的代码进行一些小的更正...如下:
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)
You can notice that this time, we returned playerscore()
. 您可能会注意到,这次,我们返回了
playerscore()
。 As it seems like you're learning the basics, I'd like to propose a slightly different approach, as you'll get a ValueError exception if the player types a string (some letters) instead of a number. 好像您正在学习基础知识一样,我想提出一种略有不同的方法,因为如果播放器键入字符串(某些字母)而不是数字,则会出现ValueError异常。 You can keep using your recursive function in the exception catch, and use a while loop to make the player to keep the number within your desired range.
您可以在异常捕获中继续使用递归函数,并使用while循环使播放器将数字保持在所需范围内。 Here follows my suggestion to prevent the ValueError exception:
我的建议如下,以防止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)
I hope that helps. 希望对您有所帮助。 Regards.
问候。
When function doesn't return anything, value will be None
. 当函数不返回任何值时,值将为
None
。
So you should check your code for the case where score
is larger than 5. (Compare it to the case where score is not larger than five.) 因此,对于
score
大于5的情况,您应该检查代码。(与分数不大于5的情况进行比较)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.