Question: Create a text file named team.txt and store 8 football team names and their best player, separate the player from the team name by a comma. Create a program that reads from the text file and displays a random team name and the first letter of the player’s first name and the first letter of their surname.
import random
teamList = open("team.txt", "__")
data = teamList._______()
randomChoice= random._______(____)
teamName =[]
player =[]
for lines in data:
split = lines._____(',')
teamName.______(split[0])
player._______(_____[1])
teamName = teamName[_______]
letters = _______[randomChoice]
print("\nThe team is ",______)
splitLetters = letters._____(' ')
print("And the first letter of the player’s firstname and surname is")
for x in range(_____(splitLetters)):
print((______[x][_]).upper())
I think this should do it:
import random
teamList = open("team.txt", "r")
data = teamList.readlines()
randomChoice = random.choice(range(8))
teamName = []
player = []
for lines in data:
split = lines.split(',')
teamName.append(split[0])
player.append(split[1])
teamName = teamName[randomChoice]
letters = player[randomChoice]
print("\nThe team is ", teamName)
splitLetters = letters.split(' ')
print("And the first letter of the player’s firstname and surname is")
for x in range(len(splitLetters)):
print((splitLetters[x][0]).upper())
This is a bit of an awful script but since you have to just fill in the blanks and not come up with your own way of doing it here it is. For example, they never even close the text file, and they add all players and teams to lists just to pick randomly from the lists anyway.
So the file is read, and it goes through each line adding the team name to teamName
and adding the player name to player
. Then, random is used to select a random integer, and that index of both player
and teamName
are used to print the desired output.
Let me know if you want a longer explanation I can go through specific lines if you're having a problem with something in particular.