Python-创建一个联赛表格,该表分别显示线路和提取某些线条的函数



我正在尝试编写三个Funcitons,创建一个联赛表(我使用3个团队使其更容易测试,但实际上将有16个(,计算这些团队的要点(获胜3分,平局1分,损失为0(和一个功能,鉴于团队的名称将返回其所有信息。SaveInfo函数工作和CreateLeague函数的作用,但是它仅返回第一行而不是后续2,还返回GetTeam函数返回(无(。

def saveInfo():
    myFile=open("league.txt","w")
    for i in range(3):
        team=input("Enter name of team: ")
        matchesPlayed=input("Enter number of matches played: ")
        matchesWon=int(input("Enter number of matches won: "))
        matchesDrawn=int(input("Enter number of matches drawn: "))
        matchesLost=int(input("Enter number of matches lost: "))
        return team,"",matchesPlayed,"",matchesWon,"",matchesDrawn,"",matchesLost
     myFile.close()
def createLeague():
    myFile=open("league.txt","r")
    points=0
    for info in myFile:
        for i in range(3):
            info=info.rstrip("/n")
            team_info=info.split()
            team_info[2]=int(team_info[2])
            team_info[3]=int(team_info[3])
            points=(team_info[2]*3)+(team_info[3]*1)
            team_info.append(points)
            return team_info   
     myFile.close()
def getTeam():
    name="Enter name of team"
    myFile=open("league.txt","r")
    team_info=""
    for line in myFile:
        info=info.rstrip("/n")
        team_info=info.split()
        if team_info[0]==name:
            print(team_info)
    myFile.close()
saveInfo()
team_info=createLeague
print(createLeague()) # only displays first line not the other 2

根据您的代码是准确的。 create_league 输入循环,处理第一个项目,然后返回该团队的信息而不回到循环的顶部。您告诉它要尽快返回到这一点。您需要允许其完成循环,以您使用的任何形式编写和收集内容。

类似地, getTeam 没有返回任何内容:没有返回语句。这就是为什么返回值为

我无法向您展示如何解决此问题,因为您尚未指定发帖中的功能,但这就是您出错的地方。与您在函数和调用例程之间进行通信的内容相比

您的代码有错误

  1. 您将返回循环放在循环中,这就是为什么它只循环一次,然后离开循环
  2. 您没有写任何文件

可能是您的意思是这样的东西

  def save_info():
        with open("league.txt","w") as myFile:
            for i in range(3):
                team=input("Enter name of team: ")
                matchesPlayed=input("Enter number of matches played: ")
                matchesWon=int(input("Enter number of matches won: "))
                matchesDrawn=int(input("Enter number of matches drawn: "))
                matchesLost=int(input("Enter number of matches lost: "))
                myFile.write("{0},{1},{2},{3},{4}n".format(team,matchesPlayed,matchesWon,matchesDrawn,matchesLost))

    def create_league():
        with open("league.txt","r") as myFile:
            contents = myFile.readlines()
            for content in contents:
                content.rstrip('n')
                team_info=content.split(",")
                team_info[2]=int(team_info[2])
                team_info[3]=int(team_info[3])
                points=(team_info[2]*3)+(team_info[3]*1)
                print(points)
save_info()
create_league()

同样适用于get_team方法,您应该使用蛇盒代替骆驼盒进行python方法

最新更新