gpt4 book ai didi

Python while 循环减法

转载 作者:太空宇宙 更新时间:2023-11-03 15:19:59 24 4
gpt4 key购买 nike

这是一个非常愚蠢的问题,但我已经有一段时间没有使用它了,不记得该怎么做。我正在做一个递归问题,需要显示倒计时的数字,以基本上显示递归是如何工作的。但是,我似乎无法弄清楚。这是我的代码。

def main():

#have user input first number
x = int(input('Enter the first number: '))

#have user input second number
y = int(input('Enter the second number: '))

#calculate result by calling recursive function
result = mult(x, y)

#print the result
print(x, 'times', y, 'is', result)

#define a function that uses recursion
def mult(x, y):
# create a loop to display the numbers
count = y
while count > 0:
print('First number =', x, 'Second number =', count)
count -= 1
#use recursion to multiply the numbers
if x == 0:
return 0
elif x == 1:
return y
else:
return y + mult(x - 1, y)

main()

我需要输出来说明这一点:

Enter the first number: 5
Enter the second number: 7
First number = 5 Second Number = 7
First number = 5 Second Number = 6
First number = 5 Second Number = 5
First number = 5 Second Number = 4
First number = 5 Second Number = 3
First number = 5 Second Number = 2
First number = 5 Second Number = 1
5 times 7 = 35

所以它在大部分情况下都可以工作,但是现在它显示的是:

Enter the first number: 5
Enter the second number: 7
First number = 5 Second number = 7
First number = 5 Second number = 6
First number = 5 Second number = 5
First number = 5 Second number = 4
First number = 5 Second number = 3
First number = 5 Second number = 2
First number = 5 Second number = 1
First number = 4 Second number = 7
First number = 4 Second number = 6
First number = 4 Second number = 5
First number = 4 Second number = 4
First number = 4 Second number = 3
First number = 4 Second number = 2
First number = 4 Second number = 1
First number = 3 Second number = 7
First number = 3 Second number = 6
First number = 3 Second number = 5
First number = 3 Second number = 4
First number = 3 Second number = 3
First number = 3 Second number = 2
First number = 3 Second number = 1
First number = 2 Second number = 7
First number = 2 Second number = 6
First number = 2 Second number = 5
First number = 2 Second number = 4
First number = 2 Second number = 3
First number = 2 Second number = 2
First number = 2 Second number = 1
First number = 1 Second number = 7
First number = 1 Second number = 6
First number = 1 Second number = 5
First number = 1 Second number = 4
First number = 1 Second number = 3
First number = 1 Second number = 2
First number = 1 Second number = 1
5 times 7 is 35

最佳答案

打印这两个数字的目的是为了跟踪递归。因此,很自然地,数字必须从递归函数内部打印。换句话说,您不想创建额外的循环(使用while)来显示数字,但递归就是循环.

您也只想在每个递归步骤中打印一次数字。

另外可能让您感到困惑的是,您在所需的输出和参数顺序之间交换了“first”/x 和“second”/y 的角色的递归。 (您希望输出中的“第二个”数字减少,但您减少了 multfirst 参数 (x)。)

它应该看起来像这样:

def mult(x, y):
"""Calculate x * y by using recursion."""
print('First number =', x, 'Second number =', y)
if y == 0:
return 0
elif y == 1:
return x
else:
return x + mult(x, y - 1)

if __name__ == '__main__':
x = int(input('Enter the first number: '))
y = int(input('Enter the second number: '))

# calculate result by calling recursive function
result = mult(x, y)

# print the result
print(x, 'times', y, 'is', result)

关于Python while 循环减法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43617429/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com