相关疑难解决方法(0)

为什么我的递归python函数返回None?

我有这段自称的代码:

def get_input():
    my_var = input('Enter "a" or "b": ')

    if my_var != "a" and my_var != "b":
        print('You didn\'t type "a" or "b". Try again.')
        get_input()
    else:
        return my_var

print('got input:', get_input())
Run Code Online (Sandbox Code Playgroud)

现在,如果我输入"a"或"b",一切都很好.输出是:

Type "a" or "b": a
got input: a
Run Code Online (Sandbox Code Playgroud)

但是,如果我输入其他内容然后输入"a"或"b",我会得到:

Type "a" or "b": purple
You didn't type "a" or "b". Try again.
Type "a" or "b": a
got input: None
Run Code Online (Sandbox Code Playgroud)

我不知道为什么get_input()要回来None,因为它应该只返回my_var.print语句显示None正确的值,但函数由于某种原因不返回该值.

python recursion return function

46
推荐指数
2
解决办法
7万
查看次数

递归函数在Python中返回none

我有这段代码,出于某种原因,当我尝试返回路径时,我得到的是:

def get_path(dictionary, rqfile, prefix=[]):

for filename in dictionary.keys():
    path = prefix+[filename]
    if not isinstance(dictionary[filename], dict):

        if rqfile in str(os.path.join(*path)):
            return str(os.path.join(*path))

    else:
        get_path(directory[filename], rqfile, path)
Run Code Online (Sandbox Code Playgroud)

有办法解决这个问题吗?提前致谢.

python recursion

7
推荐指数
1
解决办法
1万
查看次数

Python函数返回无.为什么?

count = []

def problem14(n):
    count.append(n)
    if n == 1:
        return count
    if n % 2 == 0:
        n = n/2
        problem14(n)
    else:
        n = 3*n + 1
        problem14(n)


print problem14(13)
Run Code Online (Sandbox Code Playgroud)

所以这是我写的代码.我不知道为什么它会返回None,而在我看来它应该返回列表'count'.有帮助吗?

python python-2.7

1
推荐指数
1
解决办法
166
查看次数

标签 统计

python ×3

recursion ×2

function ×1

python-2.7 ×1

return ×1