HCF Python程序未调试

时间:2018-11-17 03:55:21

标签: python visual-studio

def computeHCF(x,y):
    if x>y:
        small=int(y)
    else:
        small=int(x)
    for i in range(1,small+1):
        if ((x%i)==0) and ((y%i)==0)):
            hcf=i
    return hcf  

a=input('Enter first number: ')
b=input('Enter second number: ')
print('The HCF of ',a,' and ',b,' is ',computeHCF(a,b))

我在Python上尝试了这段代码来计算HCF,结果显示:

Exception has occurred: TypeError
not all arguments converted during string formatting
  File "C:\Users\Dell\Desktop\Python\findhcf.py", line 7, in computeHCF
    if ((x%i)==0 and (y%i)==0):
  File "C:\Users\Dell\Desktop\Python\findhcf.py", line 13, in <module>
    print('The HCF of ',a,' and ',b,' is ',computeHCF(a,b))

请帮助我弄清楚我在该程序中所犯的错误,我尝试了很多事情,但是根本没有用。谢谢。

1 个答案:

答案 0 :(得分:2)

可能您正在使用Python 3.x,这会导致此错误。在Python 3.x中,input()以“字符串”而不是“ int”的形式读取值。您可以像这样读取整数。

a=int(input('Enter first number: '))