Python while 循环额外迭代?
Python while loop extra iterations?
虽然代码按预期工作,但它重复的次数超出了需要,我不明白为什么。我包含了调试行,所以我可以看到过程的每一步,即使变量 'num' 在每次迭代结束时更新,一旦 num 的长度达到 0,它不会立即停止。我错过了什么?
这是我的输入代码:
def MathChallenge(num):
count = 0
#check for win
while len(str(num)) > 1:
# multiply function
count +=1
#print('count increased') #debug
#print('input num') #debug
#print num #debug
num = list(str(num))
#print('breaking down') #debug
#print (num) #debug
#print ('starting multiply') #debug
total = 1
for i in num:
total *= int(i)
# print(total) #debug
num = int(total)
#print ('ending multiply') #debug
#print num #debug
if len(str(num)) == 1:
break
MathChallenge(num)
return count
# keep this function call here
print MathChallenge(raw_input())
这是我的输出:
count increased
input num
9999
breaking down
['9', '9', '9', '9']
starting multiply
9
81
729
6561
ending multiply
6561
count increased
input num
6561
breaking down
['6', '5', '6', '1']
starting multiply
6
30
180
180
ending multiply
180
count increased
input num
180
breaking down
['1', '8', '0']
starting multiply
1
8
0
ending multiply
0
count increased
input num
180
breaking down
['1', '8', '0']
starting multiply
1
8
0
ending multiply
0
count increased
input num
6561
breaking down
['6', '5', '6', '1']
starting multiply
6
30
180
180
ending multiply
180
count increased
input num
180
breaking down
['1', '8', '0']
starting multiply
1
8
0
ending multiply
0
count increased
input num
180
breaking down
['1', '8', '0']
starting multiply
1
8
0
ending multiply
0
3
现在清楚了 - return
关键字 returns 来自 MathChallenge
的一次调用。你在做什么是在 while
循环中循环数字,每次迭代你 运行 MathChallenge
。每个新调用都会 运行 相同的循环。看看这个片段:
def foo(i):
print(i, end=" ")
for _ in range(i):
foo(i - 1)
return
print(foo(3)) # 3 2 1 0 1 0 2 1 0 1 0 2 1 0 1 0 hell lot numbers!
您应该决定采用迭代方法(只需删除 MathChallenge 的调用)还是递归方法:
import math
def MathChallenge(num, count):
if num < 10:
return count
digits = (int(d) for d in str(num))
# python3
# return MathChallenge(math.prod(digits), count + 1)
# python2
return MathChallenge(reduce(lambda x, y: x * y, digits, 1), count + 1)
print(MathChallenge(9999, 0))
虽然代码按预期工作,但它重复的次数超出了需要,我不明白为什么。我包含了调试行,所以我可以看到过程的每一步,即使变量 'num' 在每次迭代结束时更新,一旦 num 的长度达到 0,它不会立即停止。我错过了什么?
这是我的输入代码:
def MathChallenge(num):
count = 0
#check for win
while len(str(num)) > 1:
# multiply function
count +=1
#print('count increased') #debug
#print('input num') #debug
#print num #debug
num = list(str(num))
#print('breaking down') #debug
#print (num) #debug
#print ('starting multiply') #debug
total = 1
for i in num:
total *= int(i)
# print(total) #debug
num = int(total)
#print ('ending multiply') #debug
#print num #debug
if len(str(num)) == 1:
break
MathChallenge(num)
return count
# keep this function call here
print MathChallenge(raw_input())
这是我的输出:
count increased
input num
9999
breaking down
['9', '9', '9', '9']
starting multiply
9
81
729
6561
ending multiply
6561
count increased
input num
6561
breaking down
['6', '5', '6', '1']
starting multiply
6
30
180
180
ending multiply
180
count increased
input num
180
breaking down
['1', '8', '0']
starting multiply
1
8
0
ending multiply
0
count increased
input num
180
breaking down
['1', '8', '0']
starting multiply
1
8
0
ending multiply
0
count increased
input num
6561
breaking down
['6', '5', '6', '1']
starting multiply
6
30
180
180
ending multiply
180
count increased
input num
180
breaking down
['1', '8', '0']
starting multiply
1
8
0
ending multiply
0
count increased
input num
180
breaking down
['1', '8', '0']
starting multiply
1
8
0
ending multiply
0
3
现在清楚了 - return
关键字 returns 来自 MathChallenge
的一次调用。你在做什么是在 while
循环中循环数字,每次迭代你 运行 MathChallenge
。每个新调用都会 运行 相同的循环。看看这个片段:
def foo(i):
print(i, end=" ")
for _ in range(i):
foo(i - 1)
return
print(foo(3)) # 3 2 1 0 1 0 2 1 0 1 0 2 1 0 1 0 hell lot numbers!
您应该决定采用迭代方法(只需删除 MathChallenge 的调用)还是递归方法:
import math
def MathChallenge(num, count):
if num < 10:
return count
digits = (int(d) for d in str(num))
# python3
# return MathChallenge(math.prod(digits), count + 1)
# python2
return MathChallenge(reduce(lambda x, y: x * y, digits, 1), count + 1)
print(MathChallenge(9999, 0))