-4

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.

8
  • Please show us the while looping that you tried so we can help you understand why it didn't work. Sep 15, 2014 at 17:56
  • 1
    Your indentation is messed up..
    – thebjorn
    Sep 15, 2014 at 17:57
  • 1
    Your question displays no while loop, so it is impossible to tell why your supposed loop did not work.
    – chepner
    Sep 15, 2014 at 18:00
  • 1
    if choice not in {"a","s","m","d"} can replace your long if statement Sep 15, 2014 at 18:00
  • this is one of the while loop i used: 'code' while True: restart = input("type 1 to play again and 2 for no :") if restart==1: main() else: print("thanks for playing") break @WinstonEwert
    – melika
    Sep 15, 2014 at 18:08

3 Answers 3

0

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
0

Instead of this:

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

Use this:

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

You'll need to wrap the part that's processing user input in a while loop. You'll also need an option to break that while loop in your selection process. I added an input value of e that handles exiting the loop. Your first if statement and the else statement at the end were redundant, so I switched them around a little as well.

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
1
  • while true is not valid syntax and your code does not exit when the user presses e Sep 15, 2014 at 18:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.