-1

Here's my code:

import math
import random

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

    if bkwd > fwd:
        break

    while total > 0:
        f = 0

        while fwd > f:
            if total > 0:
                print("F", end="")
                f=f+1
                t=t+1
                total=total-1

            else:
               f = fwd


        b = 0

        while bkwd > b:
            if total > 0:
                print("B", end="")
                t=t-1
                b=b+1
                total=total-1
            else:
                b = bkwd
    if f > total:
        break

print(" ",t, "steps from the start")
#I need help here printing the right amount of total steps
print("Forward:", f, "Backward:", b, "Total:", )

My instructions are: A person walks a random amount of steps forward, and then a different random number of steps backwards.

The random steps are anywhere between 2 and 20 The number of steps forward is always greater than the number of steps backwards That motion of forward / backward random steps repeats itself again and again The motion is consistent (the number of forward steps stays the same throughout the motion, and the number of backwards steps stays the same throughout the motion) After making a specific amount of total steps the person is told to stop and will be a certain amount of steps forward from where they started.

The total number of steps is generated randomly and will be between 10 and 85 You are writing a program to simulate the motion taken by the person.

Display that motion and the number of steps he ends away from where he started. 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 If the program generated the forward steps to be 5, and the backward steps to be 3, and the total steps to be 16, your program would display FFFFFBBBFFFFFBBB = 4 Steps from the start

2
  • What do you mean, "modify my code so that it is still in my style?" Commented Nov 26, 2022 at 5:36
  • I'm a noob so I don't know much about lists or other functions. If corrections are made, I would really be grateful for them to be as simple as my code lol Commented Nov 26, 2022 at 5:46

1 Answer 1

0

For one thing, bkwd = random.randint(2,fwd) will generate a number between 2<=n<=fwd, when instead you want 2<=n<=fwd-1.

To answer your question, you just need to add a new variable to keep track of total steps taken. Maybe call it steps_taken? You should increment this counter once for every step taken.

Sign up to request clarification or add additional context in comments.

3 Comments

I have a quick question: so I generated the program and it generated random integers for everything, but my generated steps for F was 18 and B was 14 and the Total was 34. But, the program stopped even before it could generate all the backwards steps even once because it reached the total due to the forward steps being too big. Is there a restriction I can add for this or can code be this way?
@kora Plugging in fwd=18, bkwd=14, total=34 runs normally for me, I get FFFFFFFFFFFFFFFFFFBBBBBBBBBBBBBBFF 6 steps from the start. I'm not encountering the issue you're describing.
Sorry I meant forward = 18, backward = 14, total =23. I was looking at the wrong spot. So the output looks like this FFFFFFFFFFFFFFFFFFBBBBB

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.