简体   繁体   中英

calculator program with a loop to quit and restart

I've got a calculator program and I am trying to

  1. Figure out how to get it to loop back to the beginning

  2. When you select the quit option, it proceeds to ask for the two numbers before quitting and I can't seem to figure out how to bypass that and just quit.

I've got the calculator working. Just can't get it to quit or loop back properly if you select an operation

    def add(num1, num2):
        return num1 + num2
    def subtract(num1, num2):
        return num1 - num2
    def multiply(num1, num2):
        return num1 * num2
    def divide(num1, num2):
        return num1 / num2
    def exponent(num1, num2):
        return num1 ** num2
    def hypotenuse(num1, num2):
        return sqrt (num1**2 + num2**2)
     def square_root(num1, num2):
        return sqrt (num1 + num2)



    print("A for Addition")
    print("B for Subtraction")
    print("C for Multiplication")
    print("D for Division")
    print("E for Expopent")
    print("F for Hypotenuse")
    print("G for Square root of sum of two numbers")
    print("Q to Quit program")

    operation = input("Select operation you would like to perform :")

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

    if operation in ['A','a']:
       print(num1,"+",num2,"=", add(num1, num2))
    elif operation in ['B', 'b']:
       print(num1,"-",num2,"=", subtract(num1, num2))
    elif operation in ['C', 'c']:
       print(num1,"*",num2,"=", multiply(num1, num2))
    elif operation in ['D', 'd']:
       print(num1,"/",num2,"=", divide(num1, num2))
    elif operation in ['E', 'e']:
       print(num1, "^", num2,"=", exponent(num1, num2))
    elif operation in ['F', 'f']:
        print("Hypotenuse of", num1,"and", num2,  "=", hypotenuse(num1, num2))
    elif operation in ['G', 'g']:
        print("Square root of the sum of", num1, "and", num2, "=", square_root(num1, num2))
    while operation in ['Q', 'q']: 
            exit()

So all the operations work. I would just like to know how to make it loop back to the beginning once the operation is finished. As far as the quit, how do I bypass it asking for the two numbers and just quit?

The following should address your issues. I also imported the math package for sqrt .

import math

def add(num1, num2):
    return num1 + num2
def subtract(num1, num2):
    return num1 - num2
def multiply(num1, num2):
    return num1 * num2
def divide(num1, num2):
    return num1 / num2
def exponent(num1, num2):
    return num1 ** num2
def hypotenuse(num1, num2):
    return math.sqrt(num1**2 + num2**2)
def square_root(num1, num2):
    return math.sqrt(num1 + num2)

print("A for Addition")
print("B for Subtraction")
print("C for Multiplication")
print("D for Division")
print("E for Expopent")
print("F for Hypotenuse")
print("G for Square root of sum of two numbers")
print("Q to Quit program")

operation = input("Select operation you would like to perform :").lower()

while operation not in ['q']:
    num1 = int(input("Enter first number: "))
    num2 = int(input("Enter second number: "))

    if operation in 'a':
        print(num1,"+",num2,"=", add(num1, num2))
    elif operation in 'b':
        print(num1,"-",num2,"=", subtract(num1, num2))
    elif operation in 'c':
        print(num1,"*",num2,"=", multiply(num1, num2))
    elif operation in 'd':
        print(num1,"/",num2,"=", divide(num1, num2))
    elif operation in 'e':
        print(num1, "^", num2,"=", exponent(num1, num2))
    elif operation in 'f':
        print("Hypotenuse of", num1,"and", num2,  "=", hypotenuse(num1, num2))
    elif operation in 'g':
        print("Square root of the sum of", num1, "and", num2, "=", square_root(num1, num2))
    else:
        print('Please choose again')

    operation = input("Select operation you would like to perform :")
else: 
    print('Calculator turning off.')

I cleaned this up a bit and also added an else statement that loops back if an invalid input is received.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM