-1

My instructions are to track a person's motion forward and backward (every step they take). Total steps are generated from 10-85, forward and backward steps max range is between 2-20. Forward steps must always be greater than backward.

So I wrote the code for this program and my colleague corrected my code. I can't seem to figure out what the else statement does? else: f= fwd?? Does it change f=f+1 to f=fwd+1? I just need a clarification on this.

For example: If the program generated the forward steps to be 4, and the backward steps to be 2, and the total number of steps to be 13, your program would display: FFFFBBFFFFBBF = 5 Steps from the start

     import random
while True:
    fwd= random.randint(2,20)
    bkwd= random.randint(2,fwd-1)
    total=random.randint(10,85)
    f= 0
    b = 0
    t= 0
    steps_taken= 0

    if bkwd > fwd:
        break

    while total > 0:
        f = 0
 #Generating forward steps
         while fwd > f:
             if total > 0:
                print("F", end="")
                f=f+1
                t=t+1
                total=total-1
                steps_taken= steps_taken+1
 #I'm having trouble here
            else:
               f = fwd
               t = t
               total = total
               steps_taken= steps_taken

        b = 0
  #Generating backward steps
        while bkwd > b:
            if total > 0:
                print("B", end="")
                t=t-1
                b=b+1
                total=total-1
                steps_taken= steps_taken+1
            else:
               b = bkwd
               t = t
               total = total
               steps_taken = steps_taken
           
     if f > total or b > total:
        break

print(" ",t, "steps from the start")
print("Forward:", f, "Backward:", b, "Total:", steps_taken )
kora
  • 19
  • 4

1 Answers1

0

There are many things that could be improved there, like this:

t = t
total = total
steps_taken= steps_taken

it has no (visible) effect and can be just removed!

But moving to an answer to your question, its main purpose is to exit the loop. Notice that when total is less than steps we need to take, we never exit while fwd > f loop because at some point total == 0. However the correct way should be

else:
    break

And in last print statement use fwd/bkwd instead of f/b.

kosciej16
  • 6,294
  • 1
  • 18
  • 29