在python中从列表中选择数字

2024-10-01 09:29:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个程序,产生一个单一的淘汰赛。到目前为止,我的代码是这样的(我刚刚开始)

amount = int(raw_input("How many teams are playing in this tournament?  "))
teams = []
i = 0
while i<amount:
    teams.append(raw_input("please enter team name:  "))
    i= i+1

现在我被卡住了。我想随机选择2个数字,将选择面对面的球队。数字不能重复,必须在1到“金额”之间。最有效的方法是什么?在


Tags: 代码in程序inputraw数字thisamount
3条回答
team1 = random.choice(teams)
teams.remove(team1)
team2 = random.choice(teams)

我想那应该行得通。在

看一下^{}模块。在

>>> import random
>>> teams = ['One team', 'Another team', 'A third team']
>>> team1, team2 = random.sample(teams, 2)
>>> print team1
'Another team'
>>> print team2
'One team'

我不完全确定你要的是什么,但是你可以选择随机数,例如

random.randint(1,10)

这将给你一个介于1到10之间的随机数

注意:您需要导入随机模块

^{pr2}$

相关问题 更多 >