我编写了这样的函数以查找一块土地的最小正方形
def find_smallest(small, big):
if small == big:
return small
else:
sub_small = big % small
sub_big = small
find_smallest(sub_small, sub_big)
但它报告错误为
>>> find_smallest(640, 1280)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in find_smallest
File "<stdin>", line 5, in find_smallest
ZeroDivisionError: integer division or modulo by zero
没有modulo by zero
错误。
出什么问题了?
答案 0 :(得分:1)
考虑find_smallest(2,8)
。
在第一个循环中,您设置了sub_small = big % small = 8 % 2 = 0
和sub_big = small = 2
。然后,您致电find_smallest(0,2)
。
然后在第二个循环中尝试执行您无法执行的sub_small = big % small = 2 % 0
。