Reputation: 119
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
?
Upvotes: 2
Views: 5034
Reputation: 9584
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.
Upvotes: 3
Reputation: 99630
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})
Upvotes: 2