相关疑难解决方法(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 foo(a):
    a.append(1)
    if len(a) > 10:
        print a
        return a
    else:
        foo(a)
Run Code Online (Sandbox Code Playgroud)

为什么这个递归函数返回None(参见下面的记录)?我不太明白我做错了什么.

In [263]: x = []

In [264]: y = foo(x)
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

In [265]: print y
None

python recursion list

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

我期待'真'但得到'无'

我有一个简单的Python脚本,递归检查一系列n数字是否是数字的因子x.如果任何数字不是我返回的因素False,否则当n==1我想要返回时True.但是我一直在回来,NoneType并希望得到关于如何解决这个问题的建议.

#Function
def recursive_factor_test(x, n):
    if n==1:
        return True
    else: 
        if  x % n == 0:
            #print "passed {}".format(n)
            recursive_factor_test(x,n-1)
        else:
            return False

#Example Expecting False
print recursive_factor_test(5041,7)
>>False
#Example Expecting True
print recursive_factor_test(5040,7)
>>None
type(recursive_factor_test(5040,7))
>>NoneType
Run Code Online (Sandbox Code Playgroud)

python

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

为什么在Python中使用递归时会出现这种情况?

最近在学习递归,写了一个简单的递归函数来验证我的理解:

def hello(n):
    if n == 1:
        return 'hello'
    else:
        print('hello')
        hello(n-1)

def returnhello():
    return 'hello'

print(returnhello())
print()
print(hello(5))
Run Code Online (Sandbox Code Playgroud)

其输出如下所示:

hello

hello
hello
hello
hello
None
Run Code Online (Sandbox Code Playgroud)

为什么递归中的最后一个调用打印 None 而不是 hello?我期待它打印 5 hello

python recursion

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

Python:递归返回无值

我正在尝试编写一个找到快乐数字的程序: 更多信息在这里。 这不是学校的任务,但我只是想练习Python 2.7。基本上,递归是其中至关重要的部分,因为我必须保持平方。在我的代码中,我使用递归继续运行它,我有一个基本案例,并且可以工作,但是由于某种原因,即使我调用该函数,也不会发生递归。它只是返回第一个数字的数字平方,这是一个错误测试,并且返回None,然后停止运行。有什么问题?

num = raw_input('Pick a positive integer that you want to check for a happy number')
def happyn(number,second,third,fourth):
  if len(number) == 2:
    go = int(number[0])**2 + int(number[1])**2
  elif len(number) == 3:
    go = int(number[0])**2 + int(number[1])**2 + int(number[2])**2
  elif len(number) == 4:
    go = int(number[0])**2 + int(number[1])**2 + int(number[2])**2 +    int(number[2]**2)

  if len(number) == 1 and int(number) == 1 or int(number) == 7:
    return True
  elif len(number) == 1:
    return False
  else:
    print go
    go = str(go) …
Run Code Online (Sandbox Code Playgroud)

python recursion python-2.7

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

为什么python不返回布尔值

我有这个小函数,它接受两个整数a,b并检查是否ab提升到某个指数.这是代码.

def is_power(a,b):

    if not a%b==0:
        return a%b==0

    elif a/b==1:
       return a/b==1

    else:
        a = a/b
        is_power(a,b)

print is_power(,) 
Run Code Online (Sandbox Code Playgroud)

问题是,None无论我输入什么,这总是会返回.

但是如果我用打印替换所有返回,那么它们会给出正确的结果,即TrueFalse.

def is_power(a,b):

    if not a%b==0:
        print a%b==0

    elif a/b==1:
       print a/b==1

    else:
        a = a/b
        is_power(a,b)

is_power(,) 
Run Code Online (Sandbox Code Playgroud)

为什么会这样?这可能是一个菜鸟问题,但我仍然无法想到它.谢谢

python python-2.7

0
推荐指数
2
解决办法
185
查看次数

Python函数返回None(检查所有简单的解决方案,它们不起作用)

现在,我已经编写了Python的二进制搜索(版本2.7).有时,它工作正常,但有时它会返回None,尽管搜索的值在数组中.我已经尝试了解决这个问题的每个简单方法:我已经检查了函数返回的变量是否被定义,是否执行了返回语句所在的工作流的分支.和:变量定义,则分支执行.

这是代码:

def binarySearch( array, desiderata, iMin, iMax ):
# Returns the index of the first instance of what we search
print 'min'
print iMin
print 'max'
print iMax

# If our search array is empty
if ( iMin > iMax ):
    return None

midP = (iMin + iMax)/2
curre = tapeNr( array[midP][TAPE_NUMBER] )
final = tapeNr( desiderata )
print 'curre'
print curre
print 'final'
print final
print 'midP'
print midP

if ( curre < final …
Run Code Online (Sandbox Code Playgroud)

python binary return function

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

PYTHON 从递归函数返回一个列表

我正在编写一个程序,程序的一部分是我想使用递归函数创建一个包含字符串中所有子字符串的列表。

但是,当我返回列表时,我什么也没得到。变量 substringList 具有 None 值。

如何返回列表而不丢失其中的所有数据?

def main(string):
    substringList = []
    substringList = substring(string, substringList)

def substring(string, substringList):#Recursive function to create all the
    length = len(string)             #substrings**strong text**

    if length == 0:
        return substringList

    else:
        substringList.append(string)
        substring(string[1::], substringList)


string = "bananas"
main(string)
Run Code Online (Sandbox Code Playgroud)

python

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

我正在尝试使用python中的递归查找数字中的特定数字


def checkLuckyDigit(num):
    if num == 0:
        return False
    elif (num%10) == 8:
        return True
    else:
        checkLuckyDigit(num//10)

num = int(input("Enter a number = "))

if(checkLuckyDigit(num)):
    print("There is a lucky digit")
else:
    print("There is no lucky digit")
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我需要找出用户输入的数字是否有8,当我输入4348 时,它显示有一个幸运数字,但当我输入3438434 时,它显示没有幸运数字,函数返回None

我最近从 C++ 转移到了 python,所以我真的很困惑我的错误是什么。

python

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

标签 统计

python ×9

recursion ×4

function ×2

python-2.7 ×2

return ×2

binary ×1

list ×1