'I was making a simple calculator using python but I got stuck in a stage [closed]

Here this is my code, I made many mistakes here but I don't know how to fix all the errors. In this code we need to give a number as an input and enter value means what kind of calculation You want to do and second number this if I give this value '+' means it will add the the inp0 and inp2 but here I don't know to bring the answer.

inp0 = input("Enter one number: ")

inp1  = input("Enter value: ")

inp2 = input("Enter second number: ")

def calculate():
    if inp1 == '+':
        inp0+inp2
    else :
        inp0-inp2 
    

    if inp1 == 'x' :
        inp0*inp2 
    else :
        inp0/inp2 

ans = calculate()


Solution 1:[1]

It might look like a long answer, but these are all relatively basic concepts that you should definitely know about. In your code above, below every statement you have done the required mathematical operation, i.e., you have written inp0+inp2 etc. But, you have neither stored the answer in a variable nor returned the values. This is why there is no output. You could solve this problem using two methods. Method 1: Using return statement next to every operation

inp0 = input("Enter one number: ")

inp1  = input("Enter value: ")

inp2 = input("Enter second number: ")

def calculate():
    if inp1 == '+':
        return inp0+inp2
    else :
        return inp0-inp2 
    

    if inp1 == 'x' :
        return inp0*inp2 
    else :
        return inp0/inp2 

ans = calculate()
print(ans)

This way, every time you add or subtract or multiply or divide, the program executes the desired operation and returns the value to the variable ans you have created. OR Method 2: Using a variable to store the answer and printing it directly

inp0 = input("Enter one number: ")

inp1  = input("Enter value: ")

inp2 = input("Enter second number: ")

def calculate():
    result=0
    if inp1 == '+':
        result= inp0+inp2
    else :
        result= inp0-inp2 
    

    if inp1 == 'x' :
        result= inp0*inp2 
    else :
        result= inp0/inp2 
    
    print(result)

calculate()

ans variable is not required here as we are directly using the print statement in the function.

METHOD 3: To shorten the if statements by using if-elif-else

inp0 = input("Enter one number: ")

inp1  = input("Enter value: ")

inp2 = input("Enter second number: ")

def calculate():
    if inp1 == '+':
        return inp0+inp2
    elif inp1=='-' :
        return inp0-inp2 
    elif inp1 == 'x' :
        return inp0*inp2 
    elif inp1=='/' :
        return inp0/inp2 
    else:
        print("Invalid input")

ans = calculate()

This is faster than the code which has 2 if-else statements, because there, python checks both the statements unnecessarily. Even if the first if statement(if inp1=='+') comes out to be True, it still checks the 2nd if statement(if inp1=='x'). But in the 3rd method, if any one of the conditions is true, then it slips the remaining conditions, and executes the remaining code.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 E_net4 - Krabbe mit Hüten