chadybear
chadybear

Reputation: 119

selecting numbers from a list in python

I am working on a program that generates a single elimination tournament. so far my code looks like this ( i have just started)

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

now i am stuck. I want to randomly pick 2 numbers that will select the teams facing eachother. the numbers cannot repeat at all, and have to range from 1 to "amount". What is the most efficient way to do this?

Upvotes: 3

Views: 1598

Answers (3)

BenniMcBeno
BenniMcBeno

Reputation: 2445

I'm not entirely sure what you are asking however to chose random number you can use for example

random.randint(1,10)

this will give you a random number between 1 to 10

Note: you need to import the random module

import random

Upvotes: 0

Jimmy Lee
Jimmy Lee

Reputation: 1011

team1 = random.choice(teams)
teams.remove(team1)
team2 = random.choice(teams)

I think that should work.

Upvotes: 2

TerryA
TerryA

Reputation: 59974

Take a look at the random module.

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

Upvotes: 11

Related Questions