所以我刚刚创建了一个简单的团队生成器
import time
from random import choice
players=input("Name of the player:").split(',')
team1=[]
team2=[]
while len(players)>0:
player1 = choice(players)
team1.append(player1)
players.remove(player1)
if players == []:
break
player2 = choice(players)
team2.append(player2)
players.remove(player2)
print("team1\n", team1)
print("team2\n", team2)
time.sleep(20)
但结果如下:
Team1
['PlayerA', 'PlayerB']
Team2
['PlayerC', 'PlayerD']
我如何使其不显示[]''并使播放器名称垂直显示? 像这样:
Team1
PlayerA
PlayerB
Team2
PlayerC
PlayerD
答案 0 :(得分:1)
您可以将打印语句更改为:
print("Team1", *team1, sep='\n')
print("Team2", *team2, sep='\n')
星号将列表解压缩为单个项目,而不是整体打印列表。通过指定应使用新行而不是默认空格来分隔项目,所有项目最终都换行:
Name of the player: a,b,c,d
Team1
b
a
Team2
d
c