如何在python中添加restart命令?

时间:2014-09-15 17:55:25

标签: python restart python-3.4

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

 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")

当它完成时我希望它询问用户是否要重新启动。我使用不同的循环它不工作。

3 个答案:

答案 0 :(得分:0)

直到用户想要退出为止:

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来测试替换long if语句的membership

您可能希望将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

答案 1 :(得分:0)

而不是:

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: "))

删除:

elif choice == 'e':
        print("Goodbye")
        break

答案 2 :(得分:-1)

您需要在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