-2

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())
cod3r
  • 9
  • 1

1 Answers1

0

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.

Super short explanation of the answer

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.

Rolv Apneseth
  • 2,078
  • 2
  • 7
  • 19
  • 1
    Rather than hardcoding value 8 I would suggest either 1) `randomChoice = random.choice(range(len(data))` or even better 2) `randomChoice = random.randrange(len(data))` – DarrylG Jan 13 '21 at 13:05
  • Thank you so much, there were small syntax errors but I fixed them, there is one error that I'm stuck on where you wrote "for x in range(len(splitLetters)):" it says ' object of 'NoneType' has no len()' I'm not sure – cod3r Jan 13 '21 at 13:09
  • I agree with Darryl I just thought the question hard specified 8 teams only. That's weird about it being a NoneType, can you send an example of how one line of the text file looks? I just made a dummy file to test this with but I may have formatted it differently – Rolv Apneseth Jan 13 '21 at 19:42
  • My guess is there is a space after the comma in which case a simple `.strip()` before the split should do the trick but show me one line. – Rolv Apneseth Jan 13 '21 at 19:43