【问题标题】:For-loop with two variables at once一次有两个变量的for循环
【发布时间】:2013-12-28 17:44:57
【问题描述】:

背景: 我有这个程序,用户在其中编写两个彼此面对的团队及其得分,我应该相应地更新表格。我将所有团队存储为具有名称、比赛、胜利、进球等参数的对象。

我的问题: 我有什么办法可以同时处理两个团队的输入并更新他们各自的统计数据吗?现在我正在使用 2 个不同的 for 循环,它们基本上做同样的事情,只是做了一些小的调整。

我现在的程序(部分): 我为 homeTeam 使用这个 for 循环,而对另一个团队使用一个非常相似的循环。

for i in self.teams:

        if homeTeam.lower() == i.name.lower():
            i.games += 1

            goal_dif_Split = i.goal_dif.split("-")
            goal_dif_Split[0] = int(goal_dif_Split[0])+ scoreH
            goal_dif_Split[1] = int(goal_dif_Split[1])+ scoreA

            i.goal_dif = str(goal_dif_Split[0])+"-"+str(goal_dif_Split[1])

            if scoreH > scoreA:
                i.win += 1
                i.points += 3
            elif scoreH < scoreA:
                i.loss += 1
            else:
                i.tie += 1
                i.points += 1

根据 cmets 的要求:

1.这就是团队输入的样子

        homeTeam = input ("Hometeam: ")
        awayTeam = input ("Awayteam: ")
        score = input ("Score: ")

2。这是第二个 for 循环:

     for j in self.teams:

        if awayTeam.lower() == j.name.lower():
            j.games += 1

            goal_dif_Split = j.goal_dif.split("-")
            goal_dif_Split[0] = int(goal_dif_Split[0])+ scoreA
            goal_dif_Split[1] = int(goal_dif_Split[1])+ scoreH

            j.goal_dif = str(goal_dif_Split[0])+"-"+str(goal_dif_Split[1])

            if scoreA > scoreH:
                j.win += 1
                j.points += 3
            elif scoreA < scoreH:
                j.loss += 1
            else:
                j.tie += 1
                j.points += 1

【问题讨论】:

  • 您能否也发布第二个循环来澄清一下?
  • 1.团队输入是什么样的? 2. 你可以删除除 1 个更新的属性之外的所有属性吗?为了更清楚。谢谢!
  • 按要求添加第二个循环和团队输入!

标签: variables object if-statement for-loop python-3.x


【解决方案1】:

一个简单的解决方案 - 为什么不创建一个完全执行 for 循环的函数

然后 - 只需复制另一个团队的函数调用。

【讨论】:

    【解决方案2】:

    将两个循环变成一个循环并不难:

    game_teams = (home_team, away_team)
    game_score = score.split("-")
    
    for t in self.teams:
        if t in game_teams:
            if t == home_team:
                scored, allowed = game_score
            else: # t == away_team, so reverse the scores
                allowed, scored = game_score
    
            # now update t using scored and allowed, rather than the "raw" scores
            t.games += 1
    
            if scored > allowed:
                t.win += 1
                t.points += 3
            elif scored == allowed:
                t.tie += 1
                t.points += 1
            else:
                t.loss += 1
    
            # etc
    

    【讨论】:

    • 试过了,但它似乎没有更新我的表。它不会给我一个错误或任何东西。有什么想法可能是错的吗? (我当然已经调整了变量名等,所以它适合我的程序)
    • 嗯,我不知道出了什么问题。尝试将一些诊断代码放入循环中,例如print(t, game_teams) 就在 for 之后?这可能会帮助您找出问题所在。
    • 它似乎更新了所有球队的统计数据,因为“如果得分 > 允许:”等被排除在“t == home_team”之外。您有什么想法我应该如何放置 if 和 for 以使其仅适用于预期的团队?
    • @user3142412:没有看到你的代码,我不确定我能提供多少帮助。也许仔细检查您的缩进和if 条件?
    【解决方案3】:

    模拟现实。最初,统计表是空的。让它成为一个字典,其中名称是键,统计对象是值。

    当您获得name1 - name2 ... 3:2 时,您必须准确更新这两条记录。您不应该遍历所有团队。该字典旨在按名称搜索值(记录)。比如:

    table = {} # initially empty
    ...
    
    # parse the result to get...
    homeTeam = 'red onions'
    homeResult = 3
    
    guestTeam = 'blue shits'
    guestResult = 2
    
    # Get the stats objects for the teams.
    # The Stats() will create the initialized
    # statistics object if it does not exist
    # for the team, yet.
    hs = table.get(homeTeam,Stats())
    gs = table.get(guestTeam,Stats())
    
    hs.games += 1
    gs.games += 1
    
    # Now make the comparisons and modify the
    hs.win += 1
    gs.loss += 1
    ... etc.
    

    更新:如果您想使用列表,那么Bkkknght's solution 可能是最接近您的初始方法的方法。由于团队数量少,寻找最有效的解决方案不是这里的目标。

    【讨论】:

    • 感谢您的回复!我已经围绕将对象存储在列表中构建了我的程序,从而避免使用字典。
    猜你喜欢
    • 2019-01-25
    • 2023-03-08
    • 2013-09-10
    • 2014-06-21
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    • 2016-10-31
    • 2021-12-20
    相关资源
    最近更新 更多