函数返回无

时间:2015-09-26 05:19:55

标签: python python-2.7

发出错误然后做递归我得到无。

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'你会得到一个没有'

1 个答案:

答案 0 :(得分:1)

输入无效后,您不会返回任何内容。这样做:

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()

否则递归调用会返回一个顶级调用,但是那个只是吞下递归调用的返回值,因为它没有通过返回传递它。