4

new to programmation, im learning and here is probably a very simple problem for you guy.

import random

def run_stair_yes():
    print "\nRunning in stairs is very dangerous!"
    print "Statistique shows that you have 70% chance of falling"
    print "\nroll the dice!"


    for i in xrange(1):
        print random.randint(1, 100)

    if i <= 70 :
        print "\nWell, gravity is a bitch. You fell and die."

    elif i >= 71 :
        athlethic()

    else: 
            print "im boned!"
            exit(0)

my problem is that, whatever number is generated, it's always giving me the same "gravity is a bitch. You fell and die".

where do i go wrong ?

Óscar López
  • 232,561
  • 37
  • 312
  • 386
inick
  • 33
  • 1
  • 1
  • 5
  • tanxs all, i took all your comments in account and made a simpler code. It's working. – inick Apr 10 '12 at 04:27
  • the `else` bloc is unnecessary as your function well never not evaluate true for both i <= 70 and i >= 70 IF you assign i to a random number. – Joel Cornett Apr 10 '12 at 04:48

3 Answers3

6

You never actually set i to the random.randint()

You say

for i in xrange(1):

Where i takes the value of 0 as you iterate through the xrange(1) and then you just print out the result of random.randint(1, 100), not assigning it to i.

Try this

i = random.randint(1, 100)
jamylak
  • 128,818
  • 30
  • 231
  • 230
6

Additionally to jamylak's advice, some general pointers to improve your code:

  • Multi-line prompts are better written using the triple-quoted string syntax instead of multiple print statements. That way you only need to write print once, and you don't need all those extra newline characters (\n)

Example:

print """
Running on the stairs is dangerous!

You have a 70% chance to fall.

Run on the stairs anyway?
"""
  • Your probability calculation uses random integers in the range [1-100], but it's probably more natural to use a floating point number. (Either way will work.)

  • You don't need to check if the number is <= 70 and then check if it's >= 71. By definition (for integers!) only one of these conditions will be true, so you don't actually need to check both of them.

Example:

random_value = random.random() # random number in range [0.0,1.0)
if random_value < 0.7:
    pass #something happens 70% of the time
else:
    pass #something happens the other 30% of the time

Or more compactly:

if (random.random() < 0.7):
    pass #something happens 70% of the time
else:
    pass #something happens 30% of the time
Li-aung Yip
  • 12,320
  • 5
  • 34
  • 49
  • Keep in mind the compact version will only work for if/else, elif will behave unpredictably since each call to random will gen a new value. Basics I know, but worth mentioning. – Paul Kenjora Jan 16 '17 at 01:01
0

Maybe if you actually assigned something to i...

i = random.randint(1, 100)

And another thing: the else part is never, ever going to be executed. Every integer number is either <= 70 or >= 71, so the else will never be reached.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Whoops, disregard my previous comment. Anyway, the final `else` clause can only possibly be reached if `i` happens to be a floating point number in the range `(70.0-71.0)` - but since `i` is an integer, this will never happen and the final `else` clause will never be reached. – Li-aung Yip Apr 10 '12 at 04:37