PYTHON : Simple random generation driving if/else

inick picture inick · Apr 10, 2012 · Viewed 11.6k times · Source

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 ?

Answer

jamylak picture jamylak · Apr 10, 2012

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)