小编Hri*_*iev的帖子

如何在Python中使用递归函数找到最大公约数?

我被要求使用 Python 中的递归函数找到整数xy的最大公约数。条件表示:如果 y 等于 0,则 gcd (x,y) 为x;否则 gcd(x,y) 为 gcd(y,x%y)。为了尝试该代码,我被要求从用户那里获取两个整数。这是我尝试过的:

def gcd(x , y):
    if y == 0:
        return x
    else:
        return (y, x % y)

num_one = int(input('Enter a value for x: '))
num_two = int(input('Enter a value for y: '))
if num_two == 0:
    print(num_one)
else:
    print(gcd(num_two))
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误: TypeError: gcd() Missing 1 requiredpositional argument: 'y'

先感谢您。

python recursion controls input function

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

标签 统计

controls ×1

function ×1

input ×1

python ×1

recursion ×1