so im trying to simulate a walk where the user will randomly take either a step forward or backwards, hence using the probability of a coin flip. i think the program is pretty much self explanatory. i just need help debugging it because it doesn't seem to work properly yet. when i run it, the user is prompted to enter the number of steps twice, which it shouldn't, after that it does nothing but the program is still running. any help or advice is appreciated, thanks!

# PURPOSE: to simulate a random walk of (n) steps; steps can be taken forwards
#           or backwards
#
# INPUT(S): (n) the number of steps to be taken(forward or backwards)
#
# OUTPUT(S): how many steps away from the starting point were taken
#
# EXAMPLES: input: 20; output: 6 steps away from starting point
#           
#
################################################################################
import random


def randWalk(n):
    n = raw_input("Enter the number of steps to be taken: ")
    steps = 0
    h = 0
    t = 0

    while (h + t ) < n:
        coinFlip = random.randrange(2)
        if coinFlip == 0:
            h = h + 1
            steps = steps + 1
        else:
            t = t + 1
            steps = steps - 1
    while (h + t) ==n:
        break
    return h, t, steps

def main():
    n = raw_input("Enter the number of steps to be taken: ")
    walk = randWalk(n)
    print "You ended up", steps, "from the starting point."
    
main()

Recommended Answers

All 2 Replies

Here's are the main problems in your code,
explanations in comments.
I fixed it so that it runs.

import random


def randWalk(n):
    #n = raw_i...  <- (Don't need that!)
    n = int(n)  #Str to int conversion
    steps = 0
    h = 0
    t = 0

    while (h + t ) < n:
        coinFlip = random.randrange(2)
        if coinFlip == 0:
            h = h + 1
            steps = steps + 1
        else:
            t = t + 1
            steps = steps - 1
    #return h, t, steps  <- Not the best way
    return steps

def main():
    n = raw_input("Enter the number of steps to be taken: ")
    steps = randWalk(n)
    print "You ended up", steps, "from the starting point."
    
main()

I'm going to rewrite it, so you can look at it for studying.
Since the code can be made more concise.

import random

def randWalk(n):
    steps = 0  #start out at 0
    for x in range(n):
        steps += random.choice((-1,1))  #x += 1 means x = x + 1
    return steps  #look down at line 12

def main():
    n = raw_input("Enter the number of steps to be taken: ")
    try:  #Since the user might type "ASDF" at the above prompt
        result = randWalk(int(n))  #You have to convert str to int
        print "You ended up %s from the starting point." % result
        #Or, if I was using Python 2.6+, I might write
        #print "You ended up {0} from the starting point.".format(steps)
    except ValueError:  #Catch an error
        print "Please input a valid number next time."

if __name__ == "__main__":  #common practice
    main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.