Home > Software design >  how can I display the other elements of my code?
how can I display the other elements of my code?

Time:11-19

This is my code:

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

and this is the test I made:

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)

this is my result :

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

but this is what is supposed to be :

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

I tried to add all number of my parties to i, and I don't know how I am supposed to do it?

CodePudding user response:

Not only what @quamrana said, but you only use parties[0]; here's what you wanted to do:

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")} \n'
    
    return i

CodePudding user response:

The return was indented improperly, and the lines weren't generated correctly.

This is a bit simplified using enumerate for less indexing:

def formater_les_parties(parties):
    result = ''
    for i, party in enumerate(parties):
        result  = f'{i:<2}: {party["date"]}, {" vs ".join(party["joueurs"])}, "gagnant": {party["gagnant"]}\n'
    return result

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)

Output:

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

  • Related