【问题标题】:python - return returns Nonepython - 返回返回无
【发布时间】:2018-05-04 12:27:50
【问题描述】:

我编写了一个函数,它获取两个数字和一个操作(字符串),并返回两个数字与给定操作的结果。例如,calculate_matehamatical_expression(5,6,'+') 应该返回 11。我将分配分配给小函数,但是当我调用这些小函数时,它总是返回 None。有人可以向我解释为什么会这样吗?这是我写的代码:

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)

【问题讨论】:

  • calculate_mathematical_expression 中,您没有对返回的值做任何事情。而operation_error 什么都不做。
  • returnreturn None 或没有 return 声明做同样的事情:return None
  • 你们是对的!谢谢!只是我被告知要划分为小功能,然后构建更大的功能,这就是它出现的原因。

标签: python python-3.x return


【解决方案1】:

你需要在calculate_mathematical_expression里面再次返回,例如:

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

mathematical_sum 中的返回值不会影响调用它的函数。

【讨论】:

  • 谢谢!这行得通。虽然我不明白为什么它不影响。对此有解释吗?谢谢!
  • @CharlesCarmichael 如果内部返回使外部函数返回,那么您根本无法轻松分解函数。 x = mathematical_sum(...) 将在您可以对 x 执行任何操作之前结束函数 calculate_mathematical_expression
【解决方案2】:

您的 calculate_mathematical_expression 函数没有返回任何内容。试试下面的代码:

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)

【讨论】:

    【解决方案3】:

    你需要return

    当你从一个函数中return时,它只返回到调用它的函数。所以当你在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)
    

    ...否则calculate_mathematical_expression() 返回None


    operation_error() 不起作用

    • 使用and 而不是or 否则您的条件将始终为True
    • 返回布尔值,而不是 None 在这里,您的函数总是返回 None

    例子:

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

    你不需要operation_error()

    由于每个运算符都有条件,所以不需要operation_error()函数,可以直接这样做:

    else:
        return None
    

    ...甚至删除else 语句,让calculate_mathematical_expression() 在到达其末尾时自动返回None

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-06
      • 2014-02-23
      • 2013-07-27
      • 2018-07-19
      相关资源
      最近更新 更多