whenever i use hcf in this calculator code infinite loop won't work but in other operations like +-*/% infinite loops works fine.how do i solve it?which should i write to allow infinte loop working even after using hcf operation.thanks in advance
这是代码
print('THIS IS A CALCULATOR PROGRAMME.')
i=1
while i==1:
a=float(input('Enter first number:'))
b=float(input('Enter second number:'))
o=input('Enter operator[+,-,*,/,%,hcf]:')
if o=='+':
r=a+b
print(a,o,b,'=',r)
elif o=='-':
r=a-b
print(a,o,b,'=',r)
elif o=='*':
r=a*b
print(a,o,b,'=',r)
elif o=='/':
r=a/b
print(a,o,b,'=',r)
elif o=='%':
r=a%b
print(a,o,b,'=',r)
elif o=='hcf':
if a<b:
smaller=a
else:
smaller=b
hcf=1
i=1
while i<=smaller:
if a%i==0 and b%i==0:
hcf=i
i=i+1
print('THE HCF OF',a,'AND',b,'IS',hcf)
else:
print('OPERATOR IS INCORRECT')
我尝试在使用 hcf 操作时使用无限循环,我希望返回到输入 a 和值部分,但它根本没有发生
问题是你在两个循环中都使用了 i 。只需将 HCF 部分中的 i 替换为其他内容,无限循环就可以正常工作。
for x in range(1, smaller + 1):
if a % x == 0 and b % x == 0:
hcf = x