如何在lis中随机打印变量

2024-06-16 11:26:35 发布

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

例如:

import random
Bandit="B"
List=["","","",""]

像这样:

import random
List=["","","",""]
List[i]='B'
for i in range(0,2):
    print(random.randint(List[i]))

Aka不起作用

我希望它打印出来的函数如下:

print["B","","B",""] or ["","B","","B"] and all the other combinations

Tags: orandthe函数inimportforrange
2条回答

抱歉,我无法确定您是希望程序随机放置bandit还是让它在这两种组合中随机选择?两个中的第一个,但如果你想后一个就说。希望这有帮助:)

import random
numb = random.randint(0,3)
bandit = "B"
list = ["","","",""]
list[numb] = bandit
print(list)

我不太清楚你为什么要用随机函数,但既然你知道B在哪里出现,你可以

List[i] = 'B'    # i is an integer, and it is where you want your thing to be

就像

List[0] = 'B'    # 0 is where a list starts 
List[2] = 'B'    

结果

['B','','B','']

相关问题 更多 >