【发布时间】:2016-12-05 12:08:31
【问题描述】:
我写了一个计算两个数字的 GCD 的代码。 (24,12) 的 gcd 是 12。函数compute_gcd 计算 GCD 并返回它,它会打印在主函数中。但是,当我将其返回到主函数时,输出为none,而当我在compute_gcd 函数本身中打印它时,输出为12。
退回 GCD 时我哪里出错了?
def compute_gcd(a,b):
if(b==0):
return a # Prints 12 if I replace with print a
else:
compute_gcd(b,a%b)
def main():
a=24
b=12
print compute_gcd(a,b) # Prints none
main()
【问题讨论】:
-
else部分不返回
标签: python python-2.7 return return-value greatest-common-divisor