Anti_Lolchik
Anti_Lolchik

Reputation: 23

Basic Python calculator

I am new to Python. I tried to make a simple calculator, but what is the problem?

def add(num1, num2):
    return num1 + num2

def subtract(num1, num2):
    return num1 - num2

def div(num1, num2):
    return num1/num2

def multi(num1,num2):
    return num1*num2

def main():
    operation = input("What do you want to do?(+, -, *, or /):")
    if (operation != "+" and operation != "-" and operation != "*" and operation != "/"):
        print("Your input is invalid. Please enter a valid input.")
    else:
        num1 = float(input("Enter value for num1: "))
        num2 = float(input("Enter value for num2: "))
        if (operation == "+"):
            print(add(num1, num2))
        elif (operation == "-"):
            print(subtract(num1, num2))
        elif (operation == "*"):
            print(multi(num1,num2))
        elif (operation == "/"):
            print(div(num1,num2))

    main()

Upvotes: 1

Views: 690

Answers (3)

Muhammad Maaz
Muhammad Maaz

Reputation: 1

Simple calculator with very Easy steps in python. You are not calling the functions inside main.

# Defining Functions


def sum (x,y):
    return x+y
def div (x,y):
    return x/y
def sub (x,y):
    return x-y
def multi (x,y):
    return x*y


# getting Input for the operation to be performed

choice =input("Plesae enter choice:)\n'div' for Divide \n'multi' for Multiply\n'sub' for Subtraction\n'sum' for Addition\n")

#Performing opration According to the input of user

if(choice == "sum"):#condition
    x = int(input("Please enter number 1 :"))
    y =  int(input("Please enter number 1 :"))
    ans = sum(x,y)
    print(f"your Answer is {x} + {y} = {ans} ")
    
    


elif(choice == "div"): #condition
    x = int(input("Please enter number 1 :"))
    y =  int(input("Please enter number 1 :"))
    ans = div(x,y)
    print(f"your Answer is {x} + {y} = {ans} ")
    
    
    
    

elif(choice == "sub"):#condition
    x = int(input("Please enter number 1 :"))
    y =  int(input("Please enter number 1 :"))
    ans = sub(x,y)
    print(f"your Answer is {x} + {y} = {ans} ") 
    
    
    

elif(choice == "multi"): #condition
    x = int(input("Please enter number 1 :"))
    y =  int(input("Please enter number 1 :"))
    ans = multi(x,y)
    print(f"your Answer is {x} + {y} = {ans} ")
    
    
else: 
    print("Please Enter a Valid Number")
    
 




   

Upvotes: 0

Yousef
Yousef

Reputation: 23

Your main() has a Tab behind (before) it. It didn't run for me at first.

The other things seem fine to me.

You could also have it in a loop if you want to make it nicer.

def add(num1, num2):
    return num1 + num2

def subtract(num1, num2):
    return num1 - num2

def div(num1, num2):
    return num1/num2

def multi(num1,num2):
    return num1*num2

def main():
    operation = input("What do you want to do?(+, -, *, or /):")
    if (operation != "+" and operation != "-" and operation != "*" and operation != "/"):
        print("Your input is invalid. Please enter a valid input.")
    else:
        num1 = float(input("Enter value for num1: "))
        num2 = float(input("Enter value for num2: "))
        if (operation == "+"):
            print(add(num1, num2))
        elif (operation == "-"):
            print(subtract(num1, num2))
        elif (operation == "*"):
            print(multi(num1,num2))
        elif (operation == "/"):
            print(div(num1,num2))

if __name__ == '__main__':
    while(True):
        main()
        if input('If you are done with calculating, type q: ') == 'q':
           break

Upvotes: 1

ItsMe
ItsMe

Reputation: 433

You call main from inside itself. Set this outside the function like this:

def add(num1, num2):
    return num1 + num2

def subtract(num1, num2):
    return num1 - num2

def div(num1, num2):
    return num1/num2

def multi(num1,num2):
    return num1*num2

def main():
    operation = input("What do you want to do?(+, -, *, or /):")
    if (operation != "+" and operation != "-" and operation != "*" and operation != "/"):
        print("Your input is invalid. Please enter a valid input.")
    else:
        num1 = float(input("Enter value for num1: "))
        num2 = float(input("Enter value for num2: "))
        if (operation == "+"):
            print(add(num1, num2))
        elif (operation == "-"):
            print(subtract(num1, num2))
        elif (operation == "*"):
            print(multi(num1,num2))
        elif (operation == "/"):
            print(div(num1,num2))

main() # Added main outside the function

Upvotes: 3

Related Questions