|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
def gcd(x,y):
tuple1 = (x,y)
a = max(tuple1)
b = min(tuple1)
print(a)
print(b)
while b :
a = b
b = a%b
return a
while True:
num1 = int(input("请输入第一个数字:"))
num2 = int(input('请输入第二个数字:'))
x = gcd(num1,num2)
print(x)
为什么结果不对捏?
hi
1. 出错原因:程序逻辑发生错误 'a = b',下面有详细分析
2. 代码建议:判断数字大小的代码可以去掉,while循环已经实现了该功能
- def gcd(a,b):
- #tuple1 = (x,y) # 判断数字大小的代码可以去掉,while循环可以实现该功能
- #a = max(tuple1)
- #b = min(tuple1)
- while b : #比如 a=6, b=4
- #a = b #这行代码导致 a=4,从而 b为0
- #b = a%b
- a,b = b, a%b #这样赋值可以避免上面的逻辑错误
- return a
- while True:
- num1 = int(input("请输入第一个数字:"))
- num2 = int(input('请输入第二个数字:'))
- x = gcd(num1,num2)
- print(x)
复制代码
|
|