【问题标题】:how to add restart command in python? [duplicate]如何在python中添加重启命令? [复制]
【发布时间】:2014-09-15 17:55:24
【问题描述】:

这是我写的一个简单的计算器,但完成后它不会重新启动应用程序 这是我的代码:

 def add(x, y):

 return x + y

def subtract(x, y):

return x - y

def multiply(x, y):

 return x * y

def divide(x, y):

 return x / y


 print("Select from the list bellow which oporation you want the calculator to do.")
 print("A.Add")
 print("S.Subtract")
 print("M.Multiply")
 print("D.Divide")

 choice = input("Enter choice(a/s/m/d):")
 if choice != 'a' and choice != 's' and choice != 'm' and choice != 'd':
     print (" the letter you intered is not in our lists!")

 num1 = int(input("Enter an interger as your first number: "))
 num2 = int(input("Enter an integer as second number: "))
 if choice == 'a':
    print(num1,"+",num2,"=", add(num1,num2))

 elif choice == 's':
    print(num1,"-",num2,"=", subtract(num1,num2))

 elif choice == 'm':
    print(num1,"*",num2,"=", multiply(num1,num2))

 elif choice == 'd':
    print(num1,"/",num2,"=", divide(num1,num2))
 else:
    print("Invalid input")
 input("press enter to close")

当它完成时,我希望它询问用户是否要重新启动。我在循环它不起作用时使用了不同的方法。

【问题讨论】:

  • 请向我们展示您尝试过的 while 循环,以便我们帮助您了解为什么它不起作用。
  • 你的缩进搞砸了..
  • 您的问题显示 no while 循环,因此无法说明您假设的循环为什么不起作用。
  • if choice not in {"a","s","m","d"} 可以替换你的长 if 语句
  • 这是我使用的 while 循环之一:'code' while True: restart = input("type 1 to play and 2 for no :") if restart==1: main() else: print("感谢您的参与") break @WinstonEwert

标签: python restart python-3.4


【解决方案1】:

循环直到用户想要退出:

def main():
    print('Select from  the list below which operation you want the calculator to do.')
    print("A.Add")
    print("S.Subtract")
    print("M.Multiply")
    print("D.Divide")
    while True:
        choice = input("Enter choice(a/s/m/d) or q to quit:")
        if choice not in {"a", "s", "m", "d","q"}:
            print (" the letter you entered is not in our lists!")
            continue # if invalid input, ask for input again
        elif choice == "q":
            print("Goodbye.")
            break
        num1 = int(input("Enter an integer as your first number: "))
        num2 = int(input("Enter an integer as second number: "))
        if choice == 'a':
            print("{} + {} = {}".format(num1, num2, add(num1, num2)))
        elif choice == 's':
            print("{} - {} = {}".format(num1, num2, subtract(num1, num2)))

我使用 str.format 打印您的输出,if choice not in {"a", "s", "m", "d","q"} 使用 in 测试 membership 替换长 if 语句。

您可能希望将 int 输入包装在 try/except 中,以避免在用户输入不正确时程序崩溃。

try:
   num1 = int(input("Enter an interger as your first number: "))
   num2 = int(input("Enter an integer as second number: "))
except ValueError:
   continue

如果您想按照评论中的示例进行操作:

def main():
    print('Select from  the list below which operation you want the calculator to do.')
    print("A.Add")
    print("S.Subtract")
    print("M.Multiply")
    print("D.Divide")
    while True:
        choice = raw_input("Enter choice(a/s/m/d)")
        if choice not in {"a", "s", "m", "d","q"}:
            print (" the letter you entered is not in our lists!")
            continue
        num1 = int(input("Enter an integer as your first number: "))
        num2 = int(input("Enter an integer as second number: "))
        if choice == 'a':
            print("{} + {} = {}".format(num1, num2, add(num1, num2)))
        elif choice == 's':
            print("{} - {} = {}".format(num1, num2, subtract(num1, num2)))
        inp = input("Enter 1 to play again or 2 to exit")
        if inp == "1":
            main()
        else:
            print("thanks for playing")
            break

【讨论】:

    【解决方案2】:

    而不是这个:

    if choice != 'a' and choice != 's' and choice != 'm' and choice != 'd' and choice != 'e':
            print (" the letter you intered is not in our lists!")
    else:
            num1 = int(input("Enter an interger as your first number: "))
            num2 = int(input("Enter an integer as second number: "))
    

    使用这个:

    if choice != 'a' and choice != 's' and choice != 'm' and choice != 'd' and choice != 'e':
            print (" the letter you intered is not in our lists!")
    elif choice==e:
            print("goodbye")
            break
    else:
            num1 = int(input("Enter an interger as your first number: "))
            num2 = int(input("Enter an integer as second number: "))
    

    【讨论】:

      【解决方案3】:

      您需要将处理用户输入的部分封装在一个 while 循环中。您还需要一个选项来在您的选择过程中中断该 while 循环。我添加了处理退出循环的输入值e。你的第一个 if 语句和最后的 else 语句是多余的,所以我也稍微改变了它们。

      def add(x, y):
          return x + y
      
      def subtract(x, y):
          return x - y
      
      def multiply(x, y):
          return x * y
      
      def divide(x, y):
          return x / y
      
      
      while True:
          print("Select from the list bellow which oporation you want the calculator to do.")
          print("A.Add")
          print("S.Subtract")
          print("M.Multiply")
          print("D.Divide")
          print("E.Exit")
      
          choice = input("Enter choice(a/s/m/d/e):")
          if choice != 'a' and choice != 's' and choice != 'm' and choice != 'd' and choice != 'e':
              print (" the letter you intered is not in our lists!")
          else:
              num1 = int(input("Enter an interger as your first number: "))
              num2 = int(input("Enter an integer as second number: "))
              if choice == 'a':
                  print(num1,"+",num2,"=", add(num1,num2))
      
              elif choice == 's':
                  print(num1,"-",num2,"=", subtract(num1,num2))
      
              elif choice == 'm':
                  print(num1,"*",num2,"=", multiply(num1,num2))
      
              elif choice == 'd':
                  print(num1,"/",num2,"=", divide(num1,num2))
              elif choice == 'e':
                  print("Goodbye")
                  break
      

      【讨论】:

      • while true 语法无效,当用户按下 e 时,您的代码不会退出
      猜你喜欢
      • 2013-02-04
      • 2019-03-16
      • 2015-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      • 2010-12-11
      相关资源
      最近更新 更多