Random draw for a competition.
★☆☆Competitions often include a first round where teams are drawn against each other randomly.
Write a program that allows the user to enter any number of team names. The computer shuffles the teams and then outputs all the team names two at a time. If there are an odd number of teams, the word “bye” is added to the list so that one team gets an automatic place in the second round. You only need to simulate the draw for round one.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
input_teams that:draw_teams that:Enter the name of a team: England
Enter the name of a team: Germany
Add two more teams? y/n :y
Enter the name of a team: Spain
Enter the name of a team: Italy
Add two more teams? y/n :n
The draw is:
Spain v Germany
England v Italy
Enter the name of a team: Brazil
Enter the name of a team: Uraguay
Add two more teams? y/n :y
Enter the name of a team: Peru
Enter the name of a team: Argentina
Add two more teams? y/n :y
Enter the name of a team: Ecuador
Enter the name of a team: bye
Add two more teams? y/n :n
The draw is:
Brazil v Argentina
bye v Ecuador
Uraguay v Peru
# Cup draw program
# -------------------------
# Import libraries
# -------------------------
---
import random
---
# -------------------------
# Subprograms
# -------------------------
# Procedure to add teams to a list in pairs. You can input "bye" for a bye
def input_teams():
---
add_team = "y"
---
# Add teams until the user has no more to add
while add_team == "y":
---
team = input("Enter the name of a team: ")
---
teams.append(team)
---
team = input("Enter the name of a team: ")
---
teams.append(team)
---
add_team = ""
---
# Validate the input
while add_team not in ["y", "n"]:
---
add_team = input("Add two more teams? y/n :")
---
# Draw teams to play each other randomly
def draw_teams():
---
random.shuffle(teams)
---
print()
print("The draw is:")
---
# Draw teams one at a time until none are left
while len(teams) > 0:
---
print(teams.pop(), "v", teams.pop())
---
# -------------------------
# Main program
# -------------------------
teams = []
input_teams()
draw_teams()