HISIOISIH 发表于 2018-8-25 17:52:08

python 函数 最大公约数

#编写一个函数 碾转法 求最大公约数

def gys123(a,b):
    while True:
      if a%b==0:
            gys=b
            break
      else:
            ys=a%b
            b=ys
            a=b
    return gys

temp=input('请输入两个数字用逗号隔开:')

l=temp.find(',')                                       #找到逗号位置
temp1=temp[:l]                                 #用逗号分开两个数
temp2=temp

a=max(temp1,temp2)
b=min(temp1,temp2)

print(gys123(a,b))

错误
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\python\16.1.1.1.py", line 26, in <module>
    print(gys123(a,b))
File "C:\Users\Administrator\Desktop\python\16.1.1.1.py", line 5, in gys123
    if a%b==0:
TypeError: not all arguments converted during string formatting

这是哪里错误

JessiFly 发表于 2018-8-25 17:59:38

本帖最后由 JessiFly 于 2018-8-25 18:01 编辑

a和b是字符串,b里面还多了个问号

比如我输入4和8:
>>> a
'4'
>>> b
',8'
页: [1]
查看完整版本: python 函数 最大公约数