-2
votes

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 random
from math import ceil, log

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

# random() returns a number in [0, 1). The log() function does not
# like 0.
U = 1.0 - random.random()

# Find the corresponding geometric variate by inverting the uniform variate.
G = int(ceil(log(U) / log(1.0 - p)))
return G

p=1.0/2.0
for i in range(10):
print geometric(p)

When I try to run it tells me the following error:

    File "test.py", line 8
    if (p <= 0.0) or (p >=1.0):
     ^
IndentationError: expected an indented block

What is the error and how I can fix it?

2
Im using python 2.6 for windows - franvergara66
An indented block means you must actually indent your code with spaces or tabs. What part of "indented" is confusing you with this code? What tutorial did you start with? Have you looked at any other python code to see how it's indented? - S.Lott
please first try to learn the basics before asking these kinds of questions - steabert
Oh I'm sorry, maybe their 28 and 55 years old vs my 18 years make a difference. Likewise, thanks =) I'm watching the python for dummies book. - franvergara66
I would say that SO does not care for questions that go i found this code on Internet but isn´t work (sic) - njzk2

2 Answers

6
votes

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.

2
votes

Correct syntax (indent for each block. Most of those start after lines ending in ":"):

import random
from math import ceil, log

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

  # random() returns a number in [0, 1). The log() function does not
  # like 0.
  U = 1.0 - random.random()

  # Find the corresponding geometric variate by inverting the uniform variate.
  G = int(ceil(log(U) / log(1.0 - p)))
  return G

p=1.0/2.0
for i in range(10):
  print geometric(p)