简体   繁体   中英

python - return returns None

I wrote a function which gets two numbers and an operation (string) and returns the result of the two numbers with the given operation. For example calculate_matehamatical_expression(5,6,'+') should return 11. I divide the the assignment to little functions, but when I call these little function it always returns None. Can someone explain to me why that happens? This is the Code I wrote:

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

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

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

def mathematical_division(num1,num2):
    if num2 != 0:
        return num1 / num2
    else:
        return None

def operation_error(operation):
    if operation != "+" or operation != "-" or operation != "*" or operation != "/":
        return None




def calculate_mathematical_expression(num1,num2,operation):
    if operation == "+":
        mathematical_sum(num1,num2)
    elif operation == "-":
        mathematical_difference(num1,num2)
    elif operation == "*":
        mathematical_product(num1,num2)
    elif operation == "/":
        mathematical_division(num1,num2)
    else:
        operation_error(operation)

You need to return again inside calculate_mathematical_expression , eg:

def calculate_mathematical_expression(num1,num2,operation):
    if operation == "+":
        return mathematical_sum(num1,num2)

The return in mathematical_sum doesn't affect the function it's being called from.

You need to return

When you return from a function, it only returns to the function that called it. So when you return in mathematical_sum() , the value is returned to calculate_mathematical_expression() & you need to return again from this function, like this:

if operation == "+":
    return mathematical_sum(num1,num2)
elif operation == "-":
    return mathematical_difference(num1,num2)
elif operation == "*":
    return mathematical_product(num1,num2)
elif operation == "/":
    return mathematical_division(num1,num2)
else:
    return operation_error(operation)

...otherwise calculate_mathematical_expression() returns None .


operation_error() does not work

  • Use and instead of or . Otherwise your condition will always be True
  • Return a boolean, not None . Here, your function always return None

Example:

def operation_error(operation):
    return operation != "+" and operation != "-" and operation != "*" and operation != "/"

You don't need operation_error()

Since you have a condition for each operator, you don't need the operation_error() function, you can directly do this:

else:
    return None

...or even remove the else statement and let calculate_mathematical_expression() automatically return None when reaching its end.

Your calculate_mathematical_expression function is not returning anything. Try following code:

def calculate_mathematical_expression(num1,num2,operation):
    if operation == "+":
        return mathematical_sum(num1,num2)
    elif operation == "-":
        return mathematical_difference(num1,num2)
    elif operation == "*":
        return mathematical_product(num1,num2)
    elif operation == "/":
        return mathematical_division(num1,num2)
    else:
        return operation_error(operation)

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