我的代码错了吗?我似乎找不到答案
My code is wrong? I can't seem to find the answer
问题是:
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", "r")
data = teamList.readlines()
randomChoice = random.choice(range(8))
teamName =[["arsenal"],["tottenham"],["chelsea"],["westham"],["city"],["united"],["barcelona"],["liverpool"]]
player =[["kane"],["messi"],["ronaldo"],["ronaldino"],["ibrahimovic"],["neymar"],["salah"],["hazard"]]
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.append('')
print("And the first letter of the player’s firstname and surname is")
for x in range(len(splitLetters)):
print((splitLetters[x][0]).upper())
这一行有问题:
splitLetters = letters.append('')
问题是 .append() 没有 return 任何值,所以 splitLetters 是 None
,因此没有长度。要使用 .append(),您需要直接附加到字符串(即 letters.append('t') 会将字符串 't' 附加到字母,但不会 return一个值)
但是,这不是必需的,因为您要将空字符串 '' 附加到字母。尝试删除行 splitLetters = letters.append('')
将倒数第二行更改为:
for x in range(len(letters)):
问题是:
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", "r")
data = teamList.readlines()
randomChoice = random.choice(range(8))
teamName =[["arsenal"],["tottenham"],["chelsea"],["westham"],["city"],["united"],["barcelona"],["liverpool"]]
player =[["kane"],["messi"],["ronaldo"],["ronaldino"],["ibrahimovic"],["neymar"],["salah"],["hazard"]]
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.append('')
print("And the first letter of the player’s firstname and surname is")
for x in range(len(splitLetters)):
print((splitLetters[x][0]).upper())
这一行有问题:
splitLetters = letters.append('')
问题是 .append() 没有 return 任何值,所以 splitLetters 是 None
,因此没有长度。要使用 .append(),您需要直接附加到字符串(即 letters.append('t') 会将字符串 't' 附加到字母,但不会 return一个值)
但是,这不是必需的,因为您要将空字符串 '' 附加到字母。尝试删除行 splitLetters = letters.append('')
将倒数第二行更改为:
for x in range(len(letters)):