簡體   English   中英

HCF Python程序未調試

[英]HCF Python Program Not Debugging

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))

請幫助我弄清楚我在該程序中犯的錯誤,我嘗試了很多事情,但是根本沒有用。 謝謝。

可能您正在使用Python 3.x,這會導致此錯誤。 在Python 3.x中,input()以“字符串”而不是“ int”的形式讀取值。 您可以像這樣讀取整數。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM