Python中的MY代码有什么问题?

时间:2015-09-25 10:07:21

标签: python python-3.x

我的代码由于某种原因无法正常运行。我必须编写的任务是:

英足总已指派你记录英超联赛的统计数据。为了证明你有能力,你要记录两个俱乐部的统计数据 - 曼城和切尔西足球俱乐部。你应该记录球员的名字,他们所参加的球队以及他们本赛季到目前为止的进球数。您需要为每个团队保留单独的文件。为了简化任务,您最初应该只为每个团队记录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: 阿圭罗 英镑 德布鲁因 席尔瓦 帕尼

ManCityGoals: 1 1 0 1 2

我提出的问题是,当我在程序提示时搜索'Costa',而他是切尔西球员名单中的第一个元素,因此根据代码,输出应该是“科斯塔本赛季已经打入1球”(或者沿着那条线上的东西),确实出现的信息是“这个好人不在迷失中。抱歉!'这没有任何意义!

请注意,我已尝试输入其他玩家,这是相同的结果,我尝试输入带空格但没有空格的名称,仍然无效。

1 个答案:

答案 0 :(得分:0)

这是质量差的代码,你让这个问题真的很难回答。正如其他评论者所建议的那样,您应该尽量减少代码来展示问题,同时使用更少的代码来更容易回答。

主要问题是您将文件添加到列表中的代码不起作用。您应该首先打印出列表,以检查您是否正在搜索正确的数据。我重写了文件打开逻辑,以基于分隔空格而不是新行添加。我也摆脱了&#34;&#34; &#34; as&#34; as&#34;你正在使用并用常规变量替换它并打开。

您的代码现在执行并且似乎返回正确的数据。

如果您正在使用python 3,则以下是python 2的代码(替换&#34; raw_input&#34;使用&#34;输入&#34;)。此代码在您搜索时打印出以下内容对于&#34; Costa&#34;:(&#39; Costa&#39;,&#39;谁为切尔西队效力得分:&#39;,&#39; 1&#39;,&#39;目标本赛季&#39;)

#BPL Stats
UserSearch = raw_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

    PlayerFileLabel = open(PlayerFileName,mode = 'r')
    properListPlayer = PlayerFileLabel.read().split()
    for line in properListPlayer:
        Players.append(line)

    GoalFileLabel = open(GoalFileName,mode = 'r')
    properListGoal = GoalFileLabel.read().split()
    for line in properListGoal:
        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