Home > OS >  if i want to allow user to do calculation again, what should i do here?
if i want to allow user to do calculation again, what should i do here?

Time:06-21

#the required actions:

def multiplication(num1, num2):
    return num1*num2
def submisstion(num1, num2):
    return num1 num2
def subtract(num1, num2):
    return num1-num2
def div(num1, num2):
    return num1/num2
def power(num1, num2):
    return num1**num2

#taking user inputs

num1 = float(input("num1"))
calculation = input()
num2 = float(input("num2"))

#here i want to run the calculation upon user inputs

if calculation == "*":
    print(multiplication((num1), (num2)))
elif calculation == " ":
    print(submisstion((num1), (num2)))
elif calculation == "-":
    print(subtract(num1), (num2))
elif calculation == "/":
    print(div((num1), (num2)))
elif calculation == "^":
    print(power((num1), (num2)))

CodePudding user response:

I think you need an infinite loop to give the user the chance to input several calculations, in addition to a certain keyword to close the program:

while True:
    num1 = float(input("num1: "))
    calculation = input()
    num2 = float(input("num2: "))
    #here i want to run the calculation upon user inputs
    
    if calculation == "*":
        print(multiplication((num1), (num2)))
    elif calculation == " ":
        print(submisstion((num1), (num2)))
    elif calculation == "-":
        print(subtract(num1), (num2))
    elif calculation == "/":
        print(div((num1), (num2)))
    elif calculation == "^":
        print(power((num1), (num2)))
    elif calculation == "End program":
        break
  • Related