函数返回无 [英] Function returns None
本文介绍了函数返回无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在犯错误然后进行递归后,我得到了 None.
After making an error then doing recursion i get None.
def getplayerinput():
a = ["rock","paper","scissors"]
plin = raw_input("Choose %s/%s/%s: " %(a[0], a[1], a[2]))
print plin,'-first print'
if plin not in a:
print "Wrong input"
getplayerinput()
else:
print plin,'-second print'
return plin
for i in range(0,11):
print getplayerinput()
如果您先输入rock",然后输入cat",然后输入paper",您将得到None".
If you input first 'rock' then 'cat' then 'paper' you will get a 'None'.
推荐答案
一旦输入无效,您就不会返回任何内容.改为这样做:
You do not return anything once the input is not valid. Do this instead:
def getplayerinput():
a = ["rock","paper","scissors"]
plin = raw_input("Choose %s/%s/%s: " %(a[0], a[1], a[2]))
print plin,'-first print'
if plin not in a:
print "Wrong input"
return getplayerinput() # <- added return
else:
print plin,'-second print'
return plin
for i in range(0,11):
print getplayerinput()
否则递归调用会向顶级调用返回一些东西,但它只会吞下递归调用的返回值,因为它不会通过 return 传递它.
Otherwise the recursion call will return something to a top level call, but that one just swallows the return value from the recursion call, since it doesn't pass it on via return.
这篇关于函数返回无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文