简体   繁体   English

我怎样才能添加一个限制,以便前进步骤的数量不会掩盖后退步骤的数量?

[英]How can I add a restriction so that the # of forward steps won't overshadow the # of backward steps?

I have to create a program that tracks a person's motion.我必须创建一个程序来跟踪一个人的动作。 F represents forward B represents backward. F代表前进 B代表后退。 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).这些值将在 2-20 之间随机生成,步数总数将在 10-85 之间随机生成(总数将决定步数何时停止)。 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.例如,我生成了我的程序,它给了我一个 output: FFFFFFFFFFFFFFFFFFBBBBB 13 steps from start Forward: 18 Backward: 14 Total: 23 但是向后的步骤甚至无法完成。 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.随机步数在 2 到 20 之间 向前的步数总是大于向后的步数 向前/向后随机步的运动一次又一次地重复 运动是一致的(向前步数始终保持不变动作,并且在整个动作中后退的步数保持不变)在完成特定数量的总步数后,该人被告知停止并从他们开始的地方向前走一定数量的步数。

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.总步数是随机生成的,介于 10 到 85 之间 您正在编写一个程序来模拟人采取的动作。

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如果程序生成的前向步数为 4,后向步数为 2,总步数为 13,则程序将显示: FFFFBBFFFFBBF = 5 Steps from the start 如果程序生成的前向步数为5,向后步数为 3,总步数为 16,您的程序将显示 FFFFFBBBFFFFFFBBB = 4 Steps from the start

I would tackle it like this:我会这样处理:

import random

total_steps = random.randint(10, 85)
fwd = random.randint(3,(20, total_steps-1)[total_steps<21])
bkwd= random.randint(2,fwd-1)

if (fwd+bkwd) > total_steps: 
    bkwd = total_steps-fwd

print("Total_steps=", total_steps, ", fwd=", fwd, ", bkwd=", bkwd)

# Initialise step pattern to a blank string, and steps to zero.
step_pattern = ""
steps = 0
while total_steps > 0:
    for i in range(fwd):
        step_pattern += "F"
        steps += 1
        total_steps -= 1
        
    if total_steps > 0:
        for j in range(bkwd):
            step_pattern += "B"
            steps -= 1
            total_steps -= 1

# Use f-strings to insert (step_pattern) and (steps) into string
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

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 .如果我不必使事情复杂化,我只需设置从fwdbkwd的总和中随机生成的total步数。 And as per the requirements mentioned, the sum will not be greater or equal to 85.并且根据提到的要求,总和不会大于或等于 85。

total=random.randint(fwd+bkwd,85)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何打印前进、后退和总步数? - How can I print the number of steps forward, backward, and total? 如何在 python 列表中向前和可能向后移动指定数量的步骤? - How to move forward and possibly backward a specified number of steps in a python list? 如何在 AWS EMR 中一起添加 2 个(pyspark、scala)步骤? - How can I add 2 (pyspark,scala) steps together in AWS EMR? 发布API-创建代码后需要遵循哪些步骤,以便我可以通过此API将数据添加到txt文件中 - Post API— what are the steps that need to be followed once the code is created so that I can add data into a txt file through this API 如何有效地向前/向后填充数据帧中的间隙? - How can I efficiently half forward/backward fill a gap in a dataframe? 如何在保持所述步骤之间的关系的同时将长函数拆分为单独的步骤? - How can I split a long function into separate steps while maintaining the relationship between said steps? 如何使用固定步骤定义自定义 sklearn 管道? - How can I define a custom sklearn Pipeline with fixed steps? 如何选择随机海龟来移动随机数量的步数? - How can I choose a random turtle to move a random amount of steps? 二叉树遍历功能依赖于“步骤”,因此不能多次运行它吗? - Binary tree traversing function replies on Steps, so can't run it more than once? 将&#39;pip install&#39;分解为较小的步骤,这样我就可以在安装之前编辑它 - Breaking 'pip install' to smaller steps, so I can edit the package before it is installed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM