#🔒 Code review - match simulator

30 messages · Page 1 of 1 (latest)

trim night
#

Hey guys! I got a python assignment at school, and I just competed the program. It works nicely, but can someone review my code and tell me if something can me made smarter? Thanks!
The code is here: https://paste.pythondiscord.com/FDUQ

carmine troutBOT
#

@trim night

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

eternal ingot
#

hey

#
def creer_equipe(nom: str) -> dict:
    equipe = dict();
    equipe["nom"] = nom;
    equipe["joueurs"] = list();
    equipe["Score"] = 0;
    equipe["victories"] = 0;
    equipe["vif"] = False;
    return equipe;
``` you can do ```py
def creer_equipe(nom: str) -> dict:
    return {
        "nom": nom,
        "joueurs": [],
        # res of your keys here
    }
``` for one
trim night
#

True!

eternal ingot
#
        if equipe1["vif"] == True or equipe2["vif"] == True:
            if equipe1["vif"] == True and equipe2["vif"] == True:
``` can be ```py
        if equipe1["vif"] or equipe2["vif"]:
            if equipe1["vif"] and equipe2["vif"]:
brazen mesa
#

why even repeat the same exact check?

trim night
#

oh right :D

#

why even repeat the same exact check?
It's the difference of or and and

#

oh right the and can be moved out

brazen mesa
#

ah, didn't notice

eternal ingot
#
    buff = (1.0, 1.0, 1.0);
    match meteo:
        case "ensoleillé": 
            buff = (1.1, 1.1, 1.1);
        case "nuageux": 
            buff = (1.0, 0.9, 1.0);
        case "pluvieux": 
            buff = (0.9, 0.6, 0.8);
        case "venteux": 
            buff = (0.8, 0.7, 0.9);
        case "brumeux":
            buff = (0.6, 0.5, 0.6);
``` you can use a dict here instead of match case
#
buffs = {
  "ensoleillé": (1.1, 1.1, 1.1),
  "nuageux": (1.0, 0.9, 1.0),
  # rest of the buffs
}

buff = buffs.get(meteo, (1.0, 1.0, 1.0))
#

also i noticed you're looping over range(len(equipe["joueurs"])) many times just to access equipe["joueurs"][i], instead of that you can simply loop over equipe["joueurs"]

#

so for example

#
def diminuer_force(equipe: dict):
    for i in range(len(equipe["joueurs"])):
        if equipe["joueurs"][i]["force"] != 0:
            equipe["joueurs"][i]["force"] -= 1;
``` becomes
```py
def diminuer_force(equipe: dict):
    for elem in equipe["joueurs"]:
        if elem["force"] != 0:
            elem["force"] -= 1;
#

thats kind of all i see

trim night
#

Thxxx

trim night
brazen mesa
#

huh?

#

!e

vals = ["a", "b", "c"]
for elem in vals:
    print(elem)
carmine troutBOT
eternal ingot
#

oops

#

!e

vals = [{'a': 0}, {'a': 2}, {'a': 4}]
for val in vals:
    val['a'] += 1

print(vals)
carmine troutBOT
eternal ingot
#

so yes elem would be a reference

brazen mesa
#

as opposed to what?

carmine troutBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.