2016-07-27 149 views
0

我有這個print語句選擇隨機指數和我想選擇從內部設置字符串如何在打印語句蟒蛇

print("{randint(1, 10)}".format(player_r1, player_r2, player_r3, player_r4, player_r5, player_r6, player_r7, player_r8, player_r9, player_r10)) 
+0

的randint(1,10)不工作 –

+0

這意味着你需要從列表中獲取隨機數? –

+0

random.choice()可以在這裏使用 –

回答

1

代碼表達式列表中的隨機指數不評估,它只是文本。

import random 

print("{}".format(random.choice([player_r1, player_r2, player_r3, player_r4, player_r5, player_r6, player_r7, player_r8, player_r9, player_r10]))) 

或者更具可讀性:

players = [player_r1, player_r2, player_r3, player_r4, player_r5, player_r6, player_r7, player_r8, player_r9, player_r10] 
print("{}".format(random.choice(players))) 
+0

我是否需要導入任何東西才能工作?它不識別「隨機」 –

+0

顯然,您需要導入「隨機」模塊。 –

+0

工作,非常感謝 –