【问题标题】:how can I display the other elements of my code?如何显示代码的其他元素?
【发布时间】:2022-11-19 05:15:08
【问题描述】:

这是我的代码:

def formater_les_parties(parties):
    from datetime import datetime
    i =  f'{(len(parties[:-1]))} : {parties[0].get("date")},  {parties[0].get("joueurs")[0]} {"vs"} {parties[0].get("joueurs")[1]}, {"gagnant"}: {parties[0].get("gagnant")} \n'
    for w in range((len(parties))):
        i += str(w)
        return i

这是我做的测试:

test1 = formater_les_parties([
    {
        "id": "5559cafd-6966-4465-af6f-67a784016b41",
        "date": "2022-09-23 11:58:20",
        "joueurs": ["IDUL", "automate"],
        "gagnant": None
    },
    ...
    {
        "id": "80a0a0d2-059d-4539-9d53-78b3f6045943",
        "date": "2022-09-24 14:23:59",
        "joueurs": ["IDUL", "automate"],
        "gagnant": "automate"
    }
])
print(test1)

这是我的结果:

1 : 2022-09-23 11:58:20,  IDUL vs automate, gagnant: None 
0

但这是应该的:

1 : 2022-09-23 11:58:20, IDUL vs automate
...
20: 2022-09-24 14:23:59, IDUL vs automate, gagnant: automate

我试图将我的所有派对数量添加到i,但我不知道我应该怎么做?

【问题讨论】:

  • 你的return i缩进太多了。它目前在第一次通过后停止循环。

标签: python python-3.x


【解决方案1】:

不仅是@quamrana 所说的,而且您只使用 parties[0];这是你想做的:

def formater_les_parties(parties):
    from datetime import datetime
    i = ''
    for w in range((len(parties))):
        i += f'{w} : {parties[w].get("date")},  {parties[w].get("joueurs")[0]} {"vs"} {parties[w].get("joueurs")[1]}, {"gagnant"}: {parties[w].get("gagnant")} 
'
    
    return i

【讨论】:

  • 请注意,您没有使用日期时间,但也许它是针对您的代码中某些尚未实现的部分?哦,你不需要.get(),你可以直接写parties[w]["date"]。
猜你喜欢
  • 1970-01-01
  • 2021-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
  • 2021-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多