i=0
while (i==0):
n1=float(input("Enter the first number: \n"))
n2=float(input("Enter the second number: \n"))
a=str(input("Choose the operation: \n 1.) + \n 2.) - \n 3.) * \n 4.) / \n 5.) % \n"))
if(a==1):
print("The addition of the inputted numbers is ", sum(n1,n2),"\n")
elif(a==2):
print("The difference between the inputted numbers is ", diff(n1,n2),"\n")
elif(a==3):
print("The multiplication of the inputted numbers is ", product(n1,n2),"\n")
elif(a==4):
print("The division of the inputted numbers is ", div(n1,n2),"\n")
elif(a==5):
print("The modulus of the inputted numbers is ", modulus(n1,n2),"\n")
else:
print("Do you want to quit the calculator?")
b=int(input("Press 00 for 'Yes' and 11 for 'No'\n"))
if(b==00):
break
else:
print("Enter a valid option for the operation number.\nThe calculator is re-starting.")
continue
这是我的python代码。我已经定义了这里使用的所有功能。
我得到的输出是:
当我在图像中圈出的位置输入正确的操作数字3时,为什么输出没有给出最终的计算答案?为什么它一次又一次地重复相同的循环?
请帮助我。
我只是要总结评论并添加一些内容。
i
它总是会0
因为在循环内部未更改而一直中断。a
定义为str
一个str
永远等于一个数字:你必须做的:int(a)==1
或a==str(1)
11
并且如预期的那样,计算器说他将重新启动,但没有break
。continue
将重新启动循环,同时break
将其停止。正如@chepner指出的:如果continue是最后一个可能的语句,则它不会执行任何操作。00
是等于0
除此之外,您的代码实际上非常好。但是,也许您应该修改python的基础知识。这绝对不是侮辱,而是真诚的建议。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句