[英]python programming error RecursionError: maximum recursion depth exceeded in comparison
this is my python program which is exercise 6.4 answer i put also.这是我的 python 程序,它是我也放的练习 6.4 的答案。
#!/usr/bin/env python3
"""
Exercise 6.4.
A number, a, is a power of b if it is divisible by b and a/b is a power of b.
Write a function called is_power that takes parameters a and b and returns
True if a is a power of b.
Note: you will have to think about the base case.
"""
def is_power(a, b):
"""Checks if a is power of b."""
if a == b:
return True
elif a%b == 0:
return is_power(a/b, b)
else:
return False
print("is_power(10, 2) returns: ", is_power(10, 2))
print("is_power(27, 3) returns: ", is_power(27, 3))
print("is_power(1, 1) returns: ", is_power(1, 1))
print("is_power(10, 1) returns: ", is_power(10, 1))
print("is_power(3, 3) returns: ", is_power(3, 3))
I am getting this error, whenever i tried it again and again showing this error.我收到此错误,每当我一次又一次地尝试显示此错误时。 Please guide me where is the mistake in my program.
请指导我的程序中的错误在哪里。
is_power(10, 2) returns: False
is_power(27, 3) returns: True
is_power(1, 1) returns: True
Traceback (most recent call last):
File "C:\Users\Aaban Shakeel\Desktop\Think-Python-2e---my-solutions-master\ex6\ex6.4.py", line 23, in <module>
print("is_power(10, 1) returns: ", is_power(10, 1))
File "C:\Users\Aaban Shakeel\Desktop\Think-Python-2e---my-solutions-master\ex6\ex6.4.py", line 16, in is_power
return is_power(a/b, b)
File "C:\Users\Aaban Shakeel\Desktop\Think-Python-2e---my-solutions-master\ex6\ex6.4.py", line 16, in is_power
return is_power(a/b, b)
File "C:\Users\Aaban Shakeel\Desktop\Think-Python-2e---my-solutions-master\ex6\ex6.4.py", line 16, in is_power
return is_power(a/b, b)
[Previous line repeated 1021 more times]
File "C:\Users\Aaban Shakeel\Desktop\Think-Python-2e---my-solutions-master\ex6\ex6.4.py", line 13, in is_power
if a == b:
RecursionError: maximum recursion depth exceeded in comparison
This is because for b
=1 your recursion will never end - I would add:这是因为对于
b
=1 你的递归永远不会结束 - 我会补充说:
def is_power(a, b):
"""Checks if a is power of b."""
if a == b:
return True
elif b==1:
return False
elif a%b == 0:
return is_power(a/b, b)
else:
return False
You have to add extra check for number 1, because your algorithm gets stuck (param 'a' stops changing) in this line你必须为数字 1 添加额外的检查,因为你的算法在这一行卡住了(参数 'a' 停止改变)
is_power(a / b, b)
if b == 1 you call the same function forever (actually until you reach maximum recursion depth :P )如果 b == 1 你永远调用同一个函数(实际上直到你达到最大递归深度:P)
is_power(10 / 1, 1)
is_power(10 / 1, 1)
...
Recursion limit guards you from overflowing the stack.递归限制可以防止堆栈溢出。
It seems like it is better to re-write the algorithm iteratively rather than using recursion.似乎最好迭代地重写算法而不是使用递归。 This is due to tail recursion is not efficient in python.
这是因为尾递归在 python 中效率不高。 More info: https://stackoverflow.com/a/3323013/5746085
更多信息: https : //stackoverflow.com/a/3323013/5746085
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.