2

I am working on a tournament program for class. The program is supposed to have the user input all of the team names, select 2 teams, and ask the user which team won, and the winner will move on. I want to do this all in one array by using boolean values. I want all of the values in the array to start off as false, and if they win that team name turns to true.

So far I have this

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, how can I make the whole list false?

2 Answers 2

3

Using a dictionary instead of a list is a better approach, in my opinion. You just add each team name as a key to the dictionary and set its corresponding value to False or True, respectively:

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

If you want to choose the team that has won a match, you just do a team name lookup in the dictionary and set its value to True. This way, you can keep the team names and the boolean values in one data structure instead of needing two data structures or always replacing team names with boolean values which would not make sense at all.

Sign up to request clarification or add additional context in comments.

Comments

2

Since you already know the number of teams (amount), You can do

team_status = [False]*amount

Here, the index of teams and team_status would be the same, so it would be a simple lookup everytime you want the status of a particular team.

OR

You could use a dictionary

amount = int(raw_input('How many teams are playing in this tournament?   ')
teams = {}
for i < range(amount):
    team_name = raw_input("please enter team name:   ")
    teams.update({team_name: False})

5 Comments

You sure? You'd still have to calculate amount or change your while exit condition
Why is that? Is'nt amount a user's input ?
is there a way to do this in the same array? like assign the elements in my original list to true or false?
A list of dictionaries may be ?
oh, I see, it looks like a dictionary is what i was looking for. Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.