我有这个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 :(得分: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)))