我们到底如何在Python中利用“ continue”?

诺丽
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时,为什么输出没有给出最终的计算答案?为什么它一次又一次地重复相同的循环?

请帮助我。

黑客

我只是要总结评论并添加一些内容。

  1. 如果不中断while循环,它将永远运行,因为i它总是会0因为在循环内部未更改而一直中断
  2. 正如亨利Lathuille指出OT友好a定义为str一个str永远等于一个数字:你必须做的:
    int(a)==1a==str(1)
  3. 您的输入是11并且如预期的那样,计算器说他将重新启动,但没有break
  4. continue将重新启动循环,同时break将其停止。正如@chepner指出的:如果continue是最后一个可能的语句,则它不会执行任何操作。
  5. @chepner还指出的00是等于0

除此之外,您的代码实际上非常好。但是,也许您应该修改python的基础知识。这绝对不是侮辱,而是真诚的建议。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章