HCF Python程序未调试
我在Python上尝试了这段代码来计算HCF,结果显示:HCF Python程序未调试,python,visual-studio,Python,Visual Studio,我在Python上尝试了这段代码来计算HCF,结果显示: 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
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))
发生异常:TypeError
并非所有参数都在字符串格式化期间转换
文件“C:\Users\Dell\Desktop\Python\findhcf.py”,第7行,在computeHCF中
如果((x%i)==0和(y%i)==0):
文件“C:\Users\Dell\Desktop\Python\findhcf.py”,第13行,在
打印('a'和'b'的HCF'is',computeHCF(a,b))
请帮助我的老师找出我在这个程序中犯的错误,我尝试了很多东西,但都没有成功。谢谢。您可能正在使用Python 3.x,这导致了此错误。在Python 3.x中,input()将值读取为“string”而不是“int”。您可以像这样读取整数
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))
可能您正在使用Python3.x,这导致了此错误。在Python 3.x中,input()将值读取为“string”而不是“int”。您可以像这样读取整数
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))
input(..)
返回一个字符串。确保使用int()
将其转换为int。非常感谢@Austin!这就解决了<代码>输入(…)返回一个字符串。确保使用int()
将其转换为int。非常感谢@Austin!这就解决了!!如果某个答案有效,你应该永远“接受”它。哎呀,我的错,@ShivRajawat我不知道!(:如果某个答案有效,你应该永远“接受”它。哎呀,我的坏,@ShivRajawat我不知道!)