Search code examples
pythonrestartpython-3.4

how to add restart command in python?


This is a simple calculator i wrote but after finishing it won't restart the application this is my code:

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

when its finished i want it to ask the user if they want to restart or not . i used different while looping its not working.


Solution

  • Just loop until the user wants to quit:

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

    I used str.format to print your output, if choice not in {"a", "s", "m", "d","q"} uses in to test for membership replacing the long if statement.

    You might want to wrap the int input inside a try/except to avoid your program crashing if the user does not enter the correct input.

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

    If you want to do it like the example in your comment:

    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