7.4 - Tournament
Problem
An esports tournament needs a program to process results.
The program should:
- Store the team names, countries and scores in parallel arrays
- Use a function to find and return the position of a team name from user input
- Use a procedure to display the team name, country and score from the found position
Example run
Enter team name: Stormbreakers Team Information Country - Japan Score - 190
Arrays
teams = ["Shadow Vipers", "Neon Titans", "Iron Wolves", "Phantom Blaze", "Stormbreakers"] countries = ["South Korea", "Brazil", "United States", "Sweden", "Japan"] scores = [150, 175, 160, 180, 190]
| 1 | Find the position of a team from a name entered by the user | IN | teams |
| OUT | team_pos | ||
| 2 | Display the team details of the team at team_pos | IN | countries, scores, team_pos |
| OUT |
- Linear search to find the team name position (each team name is unique)
# Main teams = ["Shadow Vipers", "Neon Titans", "Iron Wolves", "Phantom Blaze", "Stormbreakers"] countries = ["South Korea", "Brazil", "United States", "Sweden", "Japan"] scores = [150, 175, 160, 180, 190] team_pos = find_position(teams) display_details(countries, scores, team_pos)
def find_position(teams):
team_name = input("Enter a team name: ")
found = False
team_pos = 0
while found == False and team_pos< len(teams):
if teams[team_pos] == team_name :
found = True
else:
team_pos = team_pos + 1
return team_pos
def display_details(countries, scores, team_pos):
print()
print("Team information")
print("Country -", countries[team_pos])
print("Score -", scores[team_pos])
# Main
teams = ["Shadow Vipers", "Neon Titans", "Iron Wolves", "Phantom Blaze", "Stormbreakers"]
countries = ["South Korea", "Brazil", "United States", "Sweden", "Japan"]
scores = [150, 175, 160, 180, 190]
team_pos = find_position(teams)
display_details(countries, scores, team_pos)
def find_position(teams):
team_name = input("Enter a team name: ")
found = False
team_pos = 0
while found == False and team_pos < len(teams):
if teams[team_pos] == team_name:
found = True
else:
team_pos = team_pos + 1
return team_pos # return the position two be used with the parallel arrays
def display_details(countries, scores, team_pos):
print()
print("Team information")
print("Country -", countries[team_pos])
print("Score -", scores[team_pos])
# Main
teams = ["Shadow Vipers", "Neon Titans", "Iron Wolves", "Phantom Blaze", "Stormbreakers"]
countries = ["South Korea", "Brazil", "United States", "Sweden", "Japan"]
scores = [150, 175, 160, 180, 190]
team_pos = find_position(teams) # Pass in the team names so the position can be found
display_details(countries, scores, team_pos) # Pass in the two other arrays and the position to be displayed
Extension
Update the program so that it will also display the differen in points between the top scoring team and the searched team.
Enter team name: Shadow Vipers Team Information Country - South Korea Score - 150 Distance - 40 points from top score