Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7410787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:17:11+00:00 2026-05-29T06:17:11+00:00

I am a newbie to Python and would like to genereate some numbers according

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:17:12+00:00Added an answer on May 29, 2026 at 6:17 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a newbie in boost.python and i'm getting this error that I would like
I'm a newbie to python and the app engine. I have this code that
I'm newbie in Python. I can't understand why this code does not work: reOptions
I'm a newbie to Python and I'm looking at using it to write some
python newbie here. I'm writing the code to control an experiment that has multiple
I found this post: Python: finding an element in an array and it's about
I am Python newbie, so maybe don't knew if this is obvious or not.
I'm not new to Python but a complete newbie with regular expressions (on my
My brother would like to learn some programming to find out if he'd enjoy
I have another newbie Python question. I have the following piece of code that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.