您可以从打印出状态开始,f
然后看看为什么您在声明g
中没有发生任何事情。if
def mcd(n1,n2):
mxcd = 0
for i in range(1,n1):
f = n1 % i
print('i is {}, f is {}'.format(i, f))
for j in range(1,n2):
g = n2 % j
print('\n j is {}, g is {}'.format(j, g))
if (f == 0 and g == 0 and f == g):
mxcd = f
else:
mxcd = "No hay comun divisor"
return mxcd
运行类似的东西mcd(2,4)
可以让你知道很多:
i is 1, f is 0
j is 1, g is 0
j is 2, g is 0
j is 3, g is 1
'No hay comun divisor'
您确实打开了该if
语句,您可以使用类似的技巧看到它:
def mcd(n1,n2):
mxcd = 0
for i in range(1,n1):
f = n1 % i
for j in range(1,n2):
g = n2 % j
if (f == 0 and g == 0 and f == g):
print('hello')
mxcd = f
else:
mxcd = "No hay comun divisor"
return mxcd
> mcd(4,8)
hello
hello
hello
hello
hello
hello
'No hay comun divisor'
从那里开始,您需要纠正一些问题才能获得正确的答案,但我会将这个练习留给您:)