【问题标题】:Python Cannot return the final valuePython无法返回最终值
【发布时间】:2014-03-31 18:17:09
【问题描述】:

我一直坚持这个任务。 我试图用我的代码做不同的组合以获得回报但失败了。 问题要求使用递归查找一段时间内的辐射暴露。

问题:我可以正确地得到所有的计算[我使用在线 python 执行器检查它],但是当处理到最终返回时,结果是None。不知道为什么我的代码不能返回最终的计算结果。我希望:那里的一些大师可以给我一些线索谢谢。

global totalExposure
totalExposure=0 

def f(x):
    import math
    return 10*math.e**(math.log(0.5)/5.27 * x)

def radiationExposure(start, stop, step):
    time=(stop-start)
    newStart=start+step

    if(time!=0):
        radiationExposure(newStart, stop, step) 
        global totalExposure
        totalExposure+=radiation   
        radiation=f(start)*step
    else:
        return totalExposure

测试用例 1:

>>> radiationExposure(0, 5, 1)
39.10318784326239

【问题讨论】:

    标签: python math return


    【解决方案1】:

    正如其他提到的,您的 if 语句没有回报。看来您忘记了 if 子句中的 return 。 else中有一个,if中没有。

    【讨论】:

      【解决方案2】:

      @furas 的代码是迭代的而不是递归的:

      def radiationExposure2(start, stop, step):
      
          totalExposure = 0
          time = stop - start
          newStart = start + step
          oldStart = start
      
          while time > 0:
             totalExposure += f(oldStart)*step
      
             time = stop - newStart
             oldStart = newStart
             newStart += step
      
          return totalExposure
      

      转换为for循环:

      def radiationExposure3(start, stop, step):
      
          totalExposure = 0
          for time in range(start, stop, step):
             totalExposure += f(time) * step
      
          return totalExposure
      

      使用生成器表达式:

      def radiationExposure4(start, stop, step):
          return sum(f(time) * step for time in range(start, stop, step))
      

      【讨论】:

        【解决方案3】:

        没有global的简洁版

        import math
        
        def f(x):
            return 10*math.e**(math.log(0.5)/5.27 * x)
        
        def radiationExposure(start, stop, step):
        
            totalExposure = 0
            time = stop - start
            newStart = start + step
        
            if time > 0:
               totalExposure = radiationExposure(newStart, stop, step) 
               totalExposure += f(start)*step
        
            return totalExposure
        
        rad = radiationExposure(0, 5, 1)
        
        # rad = 39.1031878432624
        

        【讨论】:

        • 谢谢。很简洁。
        【解决方案4】:

        正如 Paulo 所说,您的 if 声明没有回报。另外,您在分配变量 radiation 之前引用了它。一些调整,我就能让它工作。

        global totalExposure
        totalExposure = 0 
        
        def f(x):
            import math
            return 10 * math.e**(math.log(0.5)/5.27 * x)
        
        def radiationExposure(start, stop, step):
        
            time = (stop-start)
            newStart = start+step
        
            if(time!=0):
                radiationExposure(newStart, stop, step) 
                global totalExposure
                radiation = f(start) * step
                totalExposure += radiation
                return totalExposure
            else:
                return totalExposure
        
        rad = radiationExposure(0, 5, 1)
        # rad = 39.1031878433
        

        【讨论】:

          【解决方案5】:

          您似乎忘记了if 子句中的returnelse 中有一个,但 if. 中没有

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-09-24
            • 1970-01-01
            • 2022-10-02
            • 2011-01-19
            • 2019-09-10
            • 1970-01-01
            • 2013-11-17
            • 1970-01-01
            相关资源
            最近更新 更多