我需要知道该怎么做,但这可能是不可能的

时间:2019-12-04 19:42:41

标签: python python-3.x calculator

  import math
  import time
  import sys
  sys.setrecursionlimit(1000000000)
  def exit1():
    break
  def start():
    import time
    ans = input("Do you wish to start again? [y/n]\n")
    if ans == 'y' or ans == "yes" or ans == "":
      print(' \n' * 60)
    elif ans == 'n' or ans == "no":
      exit1()
    else:
      print("Invalid Input\n")
      time.sleep(3)
      start()

  def main():
    while True:
      print("Calculator:\n")
      aa = float(input("First number- "))
      ans = float(
          input(" do you want to:\n Add (1)\n Subtract (2)\n Multiply (3)\n Divide (4)\n Calculate an Exponent (5)\n "
                "Calculate Circumference of a circle(6)\n Calculate the Area of a Circle(7)\n Square Root(8)\n"))
      try:
        if ans != 1 and ans != 2 and ans != 3 and ans != 4 and ans != 5 and ans != 6 and ans != 7 and ans != 8:
          print("Invalid Operator Input")
          time.sleep(3)
          print(' \n' * 60)
          main()
        if ans == 6:
            print("If given diameter, Circumference is " + (aa * math.pi))
            start()
        if ans == 7:
            print("If given radius, Area is ")
            print(aa ** 2 * math.pi)
        if ans == 8:
            print("the square root of", + aa, "is", + (aa ** (1.0 / 2)))
            time.sleep(3)
            start()
        bb = float(input("Second Number- "))
        if ans == 1:
            pass
        elif ans == 2:
            print(aa - bb)
        elif ans == 3:
            print(aa * bb)
        elif ans == 4:
            print(aa / bb)
        elif ans == 5:
            print(aa ** bb)
        else:
            print("Invalid input")
        if aa == 9 and ans == 1 and bb == 10:
            print("21!")
        if aa != 9 and ans == 1 and bb != 10:
            print(aa + bb)
      except:
        print("Result Number Too High")

  if __name__ == '__main__':
    main()

如果运行我的代码,则会收到错误:循环外发生“中断”。我不知道该怎么做或是否有办法去做我想做的事情,即停止一切但不终止程序。我想再次显示箭头。我希望仍然能够调用main()。我只想能够在调用exit1()时停止while循环。

2 个答案:

答案 0 :(得分:2)

我看到您正在呼叫exit1(),它的定义为:

def exit1():
    break

这是您错误的出处,由于中断不在循环中,因此无法使用,也不能return break。您可能只需要用exit1()替换所有break的调用。

您不希望将其更改为quit()或类似的exit1()嵌入start()中间的main()中。

答案 1 :(得分:1)

与其在start()的末尾而不是通过遍历来调用start(),而是尝试运行一个循环:

import time

def start():
    should_continue = True
    while should_continue:
        ans = input("Do you wish to start again? [y/n]\n")
        if ans == 'y' or ans == "yes" or ans == "":
            print(' \n' * 60)
        elif ans == 'n' or ans == "no":
            should_continue = False
        else:
            print("Invalid Input\n")
            time.sleep(3)

while块中的代码将一遍又一遍地运行,直到should_continue被设置为假为止(每个循环读取一个新的ans,每次都跳过不成立的条件)此时它将不会继续下一次迭代。

或者,您可以使用break而不是创建整个新变量(break仅可以在循环中直接使用):

def start():
    while True:
        ans = input("Do you wish to start again? [y/n]\n")
        if ans == 'y' or ans == "yes" or ans == "":
            print(' \n' * 60)
        elif ans == 'n' or ans == "no":
            break  # notice how this is inside a while loop - it will jump out of the while loop
        else:
            print("Invalid Input\n")
            time.sleep(3)
相关问题