Write a function named walkingMan() that simulates a person walking a specified distance, taking steps
of varying length. The length of the steps varies at random within a specified range. (Use the function in
the Python random module, random.randint(a, b), which returns a random integer N such that a <= N <=
b.)
Input:
Parameter 1: minStep, an integer >= 0 that is the minimum length of a single step
Parameter 2: maxStep, an integer > 0 that is the maximum length of a single step
Parameter 3: distance, an integer > 0 that is the total distance to be walked
Return
An integer that is the number of steps taken to walk the specified distance
For example:

>>> print(randomSteps(2, 5, 1000)) 
>>> 281

I am not sure how to do this but I do know that a while loop will be used somewhere:

def walkingMan(minStep, maxStep, distance)

Recommended Answers

All 7 Replies

steps=0
while distance:
    steplen=random.randint(minStep,maxStep)
    steps+=steplen
    distance-=steplen
return steps

Even this is not robust code in Python as the number of recursion calls is limited, here for comparison the recursive way:

def randomSteps(minStep, maxStep, distance):
	randomstep = random.randint(minStep, maxStep)
	return 1 if distance <= randomstep else 1 + walkingMan(minStep, maxStep, distance - randomstep)

this doesnt seem to work:

import random
>>> def randomSteps(minStep, maxStep, distance):
	steps = 0
	while distance:
		steplen = random.randint(minStep, maxStep)
		steps += steplen
		distance -= steplen
	return steps

>>> print(randomSteps(2, 5, 1000))
steps=0
while distance:
    steplen=random.randint(minStep,maxStep)
    steps+=steplen
    distance-=steplen
return steps
steps += steplen

should be something else, which you should find easily enough. Additionally you should fix the termination condition for the while loop. We do not generally want to give ready answers, even this was maybe unintended bugs due to hurried posting.

should it be:

step += minStep

??

You don't test for distance becoming negative (which is also a "True" condition-only zero is False) and you want to count the number of steps. As it is now, "steps" will always be slightly greater than 1000 since you add steplen every time. Figure it out by printing where necessary.

""" This is NOT a correct solution
"""
import random

def randomSteps(minStep, maxStep, distance):
    steps = 0
    while distance > steps:  ## changed but not what you want to do
        steplen = random.randint(minStep, maxStep)
        steps += steplen
#        distance -= steplen
        print steplen, steps, distance

    return steps
 
print(randomSteps(2, 5, 1000))

should it be:
step += minStep

Random guessing doesn't count. Figure it out one part at a time and then code it, so first get the while() loop to exit properly before you declare any other variables, then continue on.

I did not write to my code, that it is not tested. It is just a first attempt.
After reading the requirement more carefully, I would suggest you to write down the solution in your own words, than code it.

like this:
1. Set stepcount to 0.
2. Take a random distance in the given interval.
3. Increase stepcount by 1
4. Decrease sum distance by the random distance
5. If sum distance is not greater then 0 return stepcount
6. Repeat from 2.

Do this algorithm solve the problem? Try it on paper with corner cases...

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.