Calculator Loop in Python

Need to add a loop to my calculator by giving the user an option to restart the calculator by putting the code in a while loop with the condition that the input from user should be, 'y' or 'Y'.

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 operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

choice = input("Enter choice(1/2/3/4):")

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

if choice == '1':
   print(num1,"+",num2,"=", add(num1,num2))

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

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

elif choice == '4':
   print(num1,"/",num2,"=", divide(num1,num2))
else:
   print("Invalid input")
6

6 Answers

Just add somethings to Alex's post

cont = "y"
while cont.lower() == "y":
    print("Select operation\n1.Add\n2.Subtract\n3.Multiply\n4.Divide")

    choice = input("Enter choice(1/2/3/4):")

    num1 = int(input("Enter first number: "))
    num2 = int(input("Enter second number: "))

    if choice == '1':
       print(num1,"+",num2,"=", (num1 + num2))

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

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

    elif choice == '4':
       print(num1,"/",num2,"=", (num1 / num2))
    else:
       print("Invalid input")
    cont = input("Continue?y/n:")
    if cont == "n":
        break

Don't really see what the problem is, you can just use another input statement and check the value in the while loop condition...

cont = "y"
while cont == "y" or cont == "Y":
    print("Select operation.")
    print("1.Add")
    print("2.Subtract")
    print("3.Multiply")
    print("4.Divide")

    choice = input("Enter choice(1/2/3/4):")

    num1 = int(input("Enter first number: "))
    num2 = int(input("Enter second number: "))

    if choice == '1':
       print(num1,"+",num2,"=", (num1 + num2))

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

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

    elif choice == '4':
       print(num1,"/",num2,"=", (num1 / num2))
    else:
       print("Invalid input")
    cont = raw_input("Continue?y/n:")
11

you can simply modify your code like this

def continue():
    resp = input("Continue ? Y/N ")
    if resp == "n" or resp == "N":
        return False
    return True
while True:
    print("Select operation.")
    print("1.Add")
    print("2.Subtract")
    print("3.Multiply")
    print("4.Divide")

    choice = input("Enter choice(1/2/3/4):")

    num1 = int(input("Enter first number: "))
    num2 = int(input("Enter second number: "))

    if choice == '1':
       print(num1,"+",num2,"=", (num1 + num2))

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

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

    elif choice == '4':
       print(num1,"/",num2,"=", (num1 / num2))

    else:
       print("Invalid input")
    if not continue() :
       break

you can change the continue function as you like

2

Here's a while loop example :

Changed

again = "y"
while again:
    # ...
    #enter your code here
    # ...
    a = input("do you want to do again (y/Y): ")
    if a not in ("y","Y"):
        a=""
0
enter code here
    while True:
        b = int(input('first num: '))
        c = input('operator: ')
        d = int(input('second num: '))
        if c == '+':
            print(b + d)
        elif c == '-':
            print(b - d)
        elif c == '*':
            print(b * d)
        elif c == '/':
            print(b / d)
        q = input('do you want to continue?: ')
        if q == 'y':
            continue
        else:
            break
1

Here's another while loop example

menu ="""
 0. chose 0 to quit
 1. chose 1 to add
 2. chose 2 to sub
 3. chose 3 to multi
 4. chose 4 to div

"""
chose= None


while(chose != 0):
    print(menu)
    num1 =int(input('first num is: '))
    num2 =int(input('second num is: '))
    chose= int(input("plz enter your chose: "))
    if(chose == 1):

        print(num1, " + ",num2," = ",num1+num2)
    elif(chose == 2):
        print(num1, " - ",num2," = ",num1+num2)
    elif(chose == 3):
        print(num1, " * ",num2," = ",num1+num2)
    elif(chose == 4):
        if(num2 == 0):
            print("You can not divide by zero")
        else:
            print(num1, " / ",num2," = ",num1+num2)
    else:
        print('plz enter a correct option')
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest