Home > Software design >  How can I add a restriction so that the # of forward steps won't overshadow the # of backward s
How can I add a restriction so that the # of forward steps won't overshadow the # of backward s

Time:11-27

I have to create a program that tracks a person's motion. F represents forward B represents backward. The values are to be randomly generated between 2-20 as well as the total # of steps being randomly generated from 10-85 (total will decide when the steps will stop). The # of forward steps has to be greater than the # of backwards steps (always). My problem is that if my total is a number that's not so far from the # of steps forward, my # of backwards steps aren't even fully generated once. For example, I generated my program and it gave me an output of this: FFFFFFFFFFFFFFFFFFBBBBB 13 steps from start Forward: 18 Backward: 14 Total: 23 But the backwards steps weren't even able to be completed. How can I make it so this won't occur? Do I have to add a restriction?

Here's my code:

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

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

            else:
               f = fwd


        b = 0

        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
    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:", steps_taken )

Here are my instructions: 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

CodePudding user response:

If I would not have to complicate things, I would just set the total steps taken random generation from the sum of fwd and bkwd. And as per the requirements mentioned, the sum will not be greater or equal to 85.

total=random.randint(fwd bkwd,85)

CodePudding user response:

I would tackle it like this:

import random

max_steps = 85
total_steps = random.randint(10, max_steps)

x = max_steps
while x > total_steps:
    fwd = random.randint(3,20)
    bkwd= random.randint(2,fwd-1)
    x = fwd   bkwd   #  <------------ensure that the sum of fwd   bkwd is not > total steps
    
print("Total_steps=", total_steps, ", fwd=", fwd, ", bkwd=", bkwd)

step_pattern = ""
steps = 0
while total_steps > 0:
    for i in range(fwd):
        step_pattern  = "F"
        steps  = 1
        total_steps -= 1
        if total_steps < 1:
            break;
        
    if total_steps > 0:
        for j in range(bkwd):
            step_pattern  = "B"
            steps -= 1
            total_steps -= 1
            if total_steps < 1:
                break;

print(f"{step_pattern} = {steps} steps from the start")


Example OUTPUTs:

Total_steps= 45 , fwd= 5 , bkwd= 2
FFFFFBBFFFFFBBFFFFFBBFFFFFBBFFFFFBBFFFFFBBFFF = 21 steps from the start
Total_steps= 14 , fwd= 6 , bkwd= 5
FFFFFFBBBBBFFF = 4 steps from the start
  • Related