Search code examples
pythonpython-3.xcomparison

In Python, my functions don't return values


When I ran the program, it didn't return me the values that were inputed which was num1 and num2.

Could somebody please tell me what I'm doing wrong?

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('Lists of operations: ')
print('1. add')
print('2. subtract')
print('3. multiply')
print('4. divide')

operations = int(input('Select an operation (1/2/3/4): '))

num1 = float(input('Enter a number: '))
num2 = float(input('Enter a number: '))
# Functions aren't returning values
if '1' == operations:
    print(num1,'+',num2,'equals'+ add(num1, num2))
# Functions aren't returning values
elif '2' == operations:
    print(num1,'-',num2,'equals', subtract(num1, num2))
# Functions aren't returning values
elif '3' == operations:
    print(num1,'*',num2,'equals', multiply(num1, num2))
# Functions aren't returning values
elif '4' == operations:
    print(num1,'/',num2,'equals', divide(num1, num2))

Solution

  • You're comparing integers to strings:

    if '1' == operations:
    

    Use 1 instead of '1' and the same for other numbers:

    if 1 == operations: