python3:每一行只是在上一行的基础上添加的。我怎样才能让每一行都是新的数字呢?

2024-10-01 02:38:33 发布

您现在位置:Python中文网/ 问答频道 /正文

import random

number_of_balls = int(input("Enter number of balls: "))
number_of_slots = int(input("Enter number of slots: "))
path = list ()
slots = list (range(0, number_of_slots))
direction = ["L", "R"]
i=0



def BallPath ():
    balls = 0
    while balls < number_of_balls:
        balls += 1
        order_of_path = random.randint(0, (len(direction) -1))
        path.append(direction[order_of_path])

    for x in path:
        print (x, end=' ')
    print("\n")

for i in range(0, number_of_slots):
    BallPath()

这是我的密码

输出结果如下:

Enter number of balls: 2
Enter number of slots: 2
L L 

L L L R 

假设它在两行中做两个随机数,但是在第一行之后它从上一行加上。我该怎么修?你知道吗


Tags: ofpathnumberforinputorderrangerandom