[英]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.
例如,calculate_matehamatical_expression(5,6,'+') 应该返回 11。我将分配分配给小函数,但是当我调用这些小函数时,它总是返回 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:您需要在
calculate_mathematical_expression
再次返回,例如:
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.在返回
mathematical_sum
并不影响它被从调用的函数。
return
return
When you return
from a function, it only returns to the function that called it.当您
return
从功能,它只返回到调用它的功能。 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:因此,当您在
mathematical_sum()
return
时,该值将返回到calculate_mathematical_expression()
您需要从该函数中再次返回,如下所示:
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
. ...否则
calculate_mathematical_expression()
返回None
。
operation_error()
does not work operation_error()
不起作用and
instead of or
.and
代替or
。 Otherwise your condition will always be True
True
None
.None
。 Here, your function always return None
None
Example:例子:
def operation_error(operation):
return operation != "+" and operation != "-" and operation != "*" and operation != "/"
operation_error()
operation_error()
Since you have a condition for each operator, you don't need the operation_error()
function, you can directly do this:由于您对每个运算符都有一个条件,因此您不需要
operation_error()
函数,您可以直接执行此操作:
else:
return None
...or even remove the else
statement and let calculate_mathematical_expression()
automatically return None
when reaching its end. ...或者甚至删除
else
语句并让calculate_mathematical_expression()
在到达结束时自动返回None
。
Your calculate_mathematical_expression function is not returning anything.您的 calculate_mathematical_expression 函数没有返回任何内容。 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.