我的代码由于某种原因不能正常运行。我必须编码的任务是:
你被足总指派去记录英超联赛的数据。为了证明你的能力,你需要记录两个俱乐部的数据——曼城和切尔西足球俱乐部。你应该记录下球员的名字,他们效力的球队,以及他们本赛季迄今为止的进球数。您需要为每个团队保留单独的文件。为了使任务简单,你应该只记录每个队5名球员的统计数据。你应该能够搜索任何球员和显示他们的记录,一旦数据已保存。当搜索特定的播放器时,记录不可用,则应显示相应的消息。你知道吗
我写的任务代码是:
#BPL Stats
UserSearch = input("What player would you like to search for?")
Players = []
Goals = []
counter = 0
def StatProcessing(PlayerFileName,PlayerFileLabel,PlayerTeamAttributeLabel,GoalFileName,GoalFileLabel,GoalTeamAttributeLabel):
global Players
global Goals
with open(PlayerFileName,mode = 'r',encoding = 'utf-8') as PlayerFileLabel:
PlayerTeamAttributeLabel = PlayerFileLabel.readlines()
for line in PlayerTeamAttributeLabel:
Players.append(line)
with open(GoalFileName,mode = 'r',encoding = 'utf-8') as GoalFileLabel:
GoalTeamAttributeLabel = GoalFileLabel.readlines()
for line in GoalTeamAttributeLabel:
Goals.append(line)
StatProcessing('ChelseaPlayers.txt','ChelseaNamesFile','ChelseaName','ChelseaGoals.txt','ChelseaGoalsFile','ChelseaGoals')
StatProcessing('ManCityPlayers.txt','ManCityNamesFile','ManCityName','ManCityGoals.txt','ManCityGoalsFile','ManCityGoals')
while (counter != 10) or (UserSearch != Players[counter]):
if UserSearch == Players[counter]:
if counter <= 4:
print (UserSearch,"who plays for Chelsea has scored:", Goals[counter], " goals this season")
if (counter > 4) and (counter <= 9):
print (UserSearch,"who plays for Manchester City has scored:", Goals[counter], " goals this season")
break
else:
counter += 1
if counter > 9:
print ('The player you searched for is not in the list. Sorry!')
break
文件内容如下:
你知道吗ChelseaPlayers.txt文件: 科斯塔 法尔考 雷米 危害 佩德罗
你知道吗ChelseaGoals.txt文件: 1 0 0 0 1个
ManCityPlayers公司: 阿圭罗 标准纯度的 德布鲁因 席尔瓦 孔帕尼
曼奇戈尔: 1 1 0 1 二
我遇到的问题是,当程序提示我搜索“科斯塔”时,虽然他是切尔西球员名单中的第一个元素,因此根据代码,输出应该是“科斯塔本赛季进了1球”(或者类似的东西),这确实是一个信息,这是'这okayer不是在丢失。对不起这没有任何意义!你知道吗
注意,我试过输入其他玩家,结果是一样的,我试过输入有空格和没有空格的名字,仍然不起作用。你知道吗
这是低质量的代码,你让这个问题很难回答。正如其他评论者所建议的,您应该尽量减少代码以显示问题,同时使用较少的代码以使其更易于回答。你知道吗
主要的问题是,用于将文件添加到列表的代码不起作用。您应该先打印出列表,以检查您搜索的数据是否正确。我重写了文件打开逻辑,以便根据分隔的空格而不是新行添加。我还去掉了您正在使用的“with”和“as”,并用正则变量和open替换它。你知道吗
您的代码现在执行并返回正确的数据。你知道吗
如果您使用的是Python3,下面是Python2的代码(将“raw\u input”替换为“input”)。当你搜索“Costa”时,这段代码会打印出以下内容:'科斯塔','谁为切尔西踢球得分:','1','本赛季进球')
相关问题 更多 >
编程相关推荐