Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 函数在第二次调用后不返回值_Python_Function - Fatal编程技术网

Python 函数在第二次调用后不返回值

Python 函数在第二次调用后不返回值,python,function,Python,Function,我正在尝试完成一个python Hangman小型项目,以帮助我学习编码 我已经创建了一个函数,希望能要求玩家输入一个字母,检查字母是否只有一个字母,并确保它不是一个数字 以下是我目前掌握的代码: def getLetter(): letter = raw_input('Please enter a letter...') if letter.isdigit() == True: print('That is a number!') getLet

我正在尝试完成一个python Hangman小型项目,以帮助我学习编码

我已经创建了一个函数,希望能要求玩家输入一个字母,检查字母是否只有一个字母,并确保它不是一个数字

以下是我目前掌握的代码:

def getLetter():
    letter = raw_input('Please enter a letter...')

    if letter.isdigit() == True:
        print('That is a number!')
        getLetter()

    if len(str(letter)) >1:
        print("Sorry, you have to enter exactly ONE letter")
        getLetter()

    else:
        return letter
当第一次输入正确时,即一个字母时,此功能正常工作。但是,当输入不正确时,例如“ddddd”,程序会要求输入另一个字母,但会返回初始输入(“ddddd”)而不是新输入,或者根本不返回任何内容


如何使其返回新输入?我需要清除原始输入吗?如果是,如何返回?

您没有返回递归调用的结果;无论在哪里调用
getLetter()
都会删除返回值

在这些调用之前添加
return
,将结果传递到调用链上:

return getLetter()
但是,您根本不应该使用递归;改为使用循环:

def getLetter():
    while True:
        letter = raw_input('Please enter a letter...')

        if letter.isdigit() == True:
            print('That is a number!')
            continue

        if len(str(letter)) >1:
            print("Sorry, you have to enter exactly ONE letter")
            continue

        return letter

每个递归调用都会添加到调用堆栈中,调用堆栈的大小不是无限的;最终用户总是输入数字会破坏你的程序!有关更多信息,请参阅。

您没有返回递归调用的结果;无论在哪里调用
getLetter()
都会删除返回值

def get_valid_letter():    
    while True:

        letter = raw_input('Please enter a letter...')
        if letter.isdigit() == True:
            print('That is a number!')

        elif len(str(letter)) >1:
            print("Sorry, you have to enter exactly ONE letter")

        else:
            return letter
在这些调用之前添加
return
,将结果传递到调用链上:

return getLetter()
但是,您根本不应该使用递归;改为使用循环:

def getLetter():
    while True:
        letter = raw_input('Please enter a letter...')

        if letter.isdigit() == True:
            print('That is a number!')
            continue

        if len(str(letter)) >1:
            print("Sorry, you have to enter exactly ONE letter")
            continue

        return letter

每个递归调用都会添加到调用堆栈中,调用堆栈的大小不是无限的;最终用户总是输入数字会破坏你的程序!有关更多信息,请参阅。

递归不是验证输入的最佳方法。永远不要低估用户破坏程序的能力,在这种情况下,输入无效输入的次数太多以至于达到最大递归深度。循环是验证输入的一个更好的选择。递归感觉非常错误,在你的例子中很难理解,我认为循环是一个更好的选择。哦,您忘记了添加到John Doe的
return
,如果要使用上面的代码,请在if语句中添加
return
,使其
return getLetter()
递归不是验证输入的最佳方法。永远不要低估用户破坏程序的能力,在这种情况下,输入无效输入的次数太多以至于达到最大递归深度。循环是验证输入的一个更好的选择。递归感觉非常错误,在你的例子中很难理解,我认为循环是一个更好的选择。哦,您忘记了添加到John Doe的
return
,如果要使用上面的代码,请在if语句中添加
return
,使其成为
return getLetter()
def get_valid_letter():    
    while True:

        letter = raw_input('Please enter a letter...')
        if letter.isdigit() == True:
            print('That is a number!')

        elif len(str(letter)) >1:
            print("Sorry, you have to enter exactly ONE letter")

        else:
            return letter