【问题标题】:Coursera issue for me我的 Coursera 问题
【发布时间】:2021-10-16 16:50:56
【问题描述】:

请帮帮我,我在做一个在线课程,有人问我这个问题

fractional_part 函数将分子除以分母,只返回小数部分(0 到 1 之间的数字)。完成函数的主体,使其返回正确的数字。 注意:由于除以 0 会产生错误,因此如果分母为 0,则函数应返回 0 而不是尝试除法。

这是我的代码:

def fractional_part(numerator, denominator):
# Operate with numerator and denominator to 
# keep just the fractional part of the quotient
   if denominator == 0 or numerator == 0:
      print("0")
   else:
        fraction = numerator / denominator
        while fraction > 1:
           fraction1 = fraction - 1
   return fraction1

print(fractional_part(5, 5)) # Should be 0
print(fractional_part(5, 4)) # Should be 0.25
print(fractional_part(5, 3)) # Should be 0.66...
print(fractional_part(5, 2)) # Should be 0.5
print(fractional_part(5, 0)) # Should be 0
print(fractional_part(0, 5)) # Should be 0

这是我的输出:

UnboundLocalError:在赋值之前引用了局部变量“fraction1”

【问题讨论】:

  • fraction1while fraction > 1: 中定义。这意味着如果 fraction 为 1 或更小,则不会定义 fraction1
  • 在语句的 else 部分,在 while 循环中。如果分数小于 1,fraction1 = fraction - 1 将被忽略并跳转到 return 语句。在 return 语句中,它不知道 fraction1 是什么,因为您从未使用过它/从未将其分配给某物
  • 您好,欢迎来到 StackOverflow。请花点时间阅读这篇关于如何提问的文章stackoverflow.com/help/how-to-ask 也阅读这篇关于如何以最低要求提出一个好问题的文章stackoverflow.com/help/minimal-reproducible-example

标签: python php python-3.x function


【解决方案1】:

你必须在你的代码中定义fraction1

您只是在while 循环内创建fraction1 变量,并且while 循环仅在fraction > 1 时运行。

如果fraction <= 1 怎么办?您没有在代码中的任何位置创建任何名为fraction1 的变量。

最后,您的函数返回未定义的 fraction1

您还必须在循环内将 fraction 减 1,否则循环将无限期地继续。

def fractional_part(numerator, denominator):
# Operate with numerator and denominator to 
# keep just the fractional part of the quotient
   if denominator == 0 or numerator == 0:
       return 0
   else:
       fraction = numerator / denominator
       fraction1 = 0
       while fraction > 1:
           fraction1 = fraction - 1
           fraction -= 1
       return fraction1
        

【讨论】:

    【解决方案2】:

    如果将fraction1 更改为fraction 将解决fraction1 未定义的问题。那么你需要在第一个if之后添加fraction = 0来解决如果denominatornumerator0

    def fractional_part(numerator, denominator):
    # Operate with numerator and denominator to 
    # keep just the fractional part of the quotient
       if denominator == 0 or numerator == 0:
          fraction = 0 #to solve if denominator or numerator is 0
          print("0")
       else:
            fraction = numerator / denominator
            while fraction > 1:
               fraction = fraction - 1
       return fraction
    

    【讨论】:

      【解决方案3】:
      def fractional_part(numerator, denominator):
          # Operate with numerator and denominator to
          # keep just the fractional part of the quotient
          if denominator == 0:
              return 0 #raise Exception('Not possible')
          elif numerator == 0:
              return 0
          # check if result of division is already smaller than 1
          if numerator < denominator:
             return numerator / denominator
      
          remainder  = numerator % denominator
          return remainder / denominator
      
      print(fractional_part(23, 5))
      print(fractional_part(5, 4))
      

      输出

      0.6
      0.25
      

      【讨论】:

      • ẁhile 方法给出近似值:fractional_part(23, 5) -> 0.59999999... 而不是 0.6
      【解决方案4】:

      要解决这个问题,您需要删除 Floor division,所以我们使用这个 Python 算术运算符 //

      def fractional_part(numerator, denominator):
            if denominator == 0 or numerator == 0:
               return 0
      
            else:
                fraction = ((numerator / denominator)-(numerator // denominator))
                return fraction
          
      
      print(fractional_part(5, 5)) # Should be 0
      print(fractional_part(5, 4)) # Should be 0.25
      print(fractional_part(5, 3)) # Should be 0.66...
      print(fractional_part(5, 2)) # Should be 0.5
      print(fractional_part(5, 0)) # Should be 0
      print(fractional_part(0, 5)) # Should be 0
      

      输出

      0.0
      0.25
      0.6666666666666667
      0.5
      0
      0
      

      【讨论】:

        【解决方案5】:
        def fractional_part(numerator, denominator):
            if numerator==0 or denominator==0:
                return 0
            else:
                result=numerator/denominator
                while result>=1:
                    result-=1
                return result   
        
        print(fractional_part(5, 5)) # Should be 0
        print(fractional_part(5, 4)) # Should be 0.25
        print(fractional_part(5, 3)) # Should be 0.66...
        print(fractional_part(5, 2)) # Should be 0.5
        print(fractional_part(5, 0)) # Should be 0
        print(fractional_part(0, 5)) # Should be 0
        

        【讨论】:

        • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
        【解决方案6】:
        def fractional_part(numerator, denominator):
            if numerator == denominator or denominator ==0 or numerator == 0:
                return 0
            else:
                result=numerator/denominator
                if result > 1 : 
                   result1=result-int(result)
                   return result1   
                return result              
            print(fractional_part(5, 5)) # Should be 0
            print(fractional_part(5, 4)) # Should be 0.25
            print(fractional_part(5, 3)) # Should be 0.66...
            print(fractional_part(5, 2)) # Should be 0.5
            print(fractional_part(5, 0)) # Should be 0
            print(fractional_part(0, 5)) # Should be 0
        

        【讨论】:

          【解决方案7】:
          def fractional_part(numerator, denominator):
              if denominator == 0 or numerator == 0:
                 return 0
              else:
                result = numerator / denominator
                if result >= 2:
                      result1 = result - int(2)
                      return result1
                elif result >= 1:
                      result2 = result - int(1)
                      return result2
          
          print(fractional_part(5, 5)) # Should be 0
          print(fractional_part(5, 4)) # Should be 0.25
          print(fractional_part(5, 3)) # Should be 0.66...
          print(fractional_part(5, 2)) # Should be 0.5
          print(fractional_part(5, 0)) # Should be 0
          print(fractional_part(0, 5)) # Should be 0
          

          【讨论】:

            猜你喜欢
            • 2017-05-05
            • 1970-01-01
            • 1970-01-01
            • 2017-05-04
            • 1970-01-01
            • 2015-02-06
            • 1970-01-01
            • 2020-07-24
            • 1970-01-01
            相关资源
            最近更新 更多