2015-11-28 34 views
0

例如:如何在一個列表中隨機打印可變

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

事情是這樣的:

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

阿卡不起作用

我希望它打印出來使用的是什麼功能如:

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

在python中,我使用隨機生成的變量在列表中進行遊戲,但並不知道如此。 –

回答

1

對不起你是否希望程序隨機放置強盜還是有它的兩個組合之間的隨機選擇我不能工作了?爲了兩人中的第一人,但如果你想讓後來的人說。希望這會有所幫助:)

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

如何在打印輸出時向列表中添加另一個「B」 –

0

ia我不太清楚爲什麼要使用隨機函數,但是因爲你知道你想讓'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',''] 
+0

和[https://docs.python.org/3.5/tutorial/datastructures.html](https://docs.python.org/3.5/tutorial/datastructures.html),指向python教程的鏈接 - 數據結構 - 列表 – DannyH