我有這個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))
我有這個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))
代碼表達式列表中的隨機指數不評估,它只是文本。
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)))
我是否需要導入任何東西才能工作?它不識別「隨機」 –
顯然,您需要導入「隨機」模塊。 –
工作,非常感謝 –
的randint(1,10)不工作 –
這意味着你需要從列表中獲取隨機數? –
random.choice()可以在這裏使用 –