Python expected an indented block

后端 未结 2 1996
耶瑟儿~
耶瑟儿~ 2020-12-22 14:06

I am a newbie to Python and would like to genereate some numbers according to geometric distribution. i found this code on Internet but isn´t work:

  import          


        
2条回答
  •  误落风尘
    2020-12-22 14:12

    In Python, indentation is significant. PEP 8 covers good indentation style.

    To take one of your functions as an example, it should look like this:

    def geometric(p):
        # p should be in (0.0, 1.0].
        if ((p <= 0.0) or (p >=1.0)):
            raise ValueError("p must be in the interval (0.0, 1.0]")
        elif p == 1.0:
            # If p is exactly 1.0, then the only possible generated value is 1.
            # Recognizing this case early means that we can avoid a log(0.0) later.
            # The exact floating point comparison should be fine. log(eps) works just
            # dandy.
            return 1
    

    If it's not indented properly, it's not valid Python code.

提交回复
热议问题