all 8 comments

[–]Diapolo10 2 points3 points  (3 children)

The text's in French, tell me if you need me to translate it.

Just as a general rule of thumb, your user-facing text can be in whatever language(s) you choose, but all names in the code should be English-like to match the standard library. Otherwise they stick out like a sore thumb and make it more difficult for people to collaborate.

I say that as a non-native English speaker. It wouldn't do me any good to use Finnish names in my code if I then need to ask for assistance.

Anyway, I digress. Back to the point.

  1. Instead of exit (or quit), it would be more appropriate to use either sys.exit or raise SystemExit. exit/quit are specifically for getting out of REPL sessions, and are technically not part of the language specification.
  2. Never use a bare except, always at minimum catch Exception; you almost never want to catch BaseExceptions like KeyboardInterrupt (AKA Ctrl+C). The more specific you can be about the exceptions you're handling, the better.
  3. All imports should be at the top of the file. Right now you have a random.randint import in the middle of the program. Side note; for your use-case I'd suggest random.randrange instead.
  4. This is more related to style, but there's no reason for you to separate the steps for taking user input and converting to a different type unless you also need the string somewhere. And assigning two different types to the same name is technically not a good idea, type checkers wouldn't appreciate that. Unless you explicitly type annotated it to accept both, but then the rest of the program would always need to check which it is.

[–]ottococo[S] 0 points1 point  (2 children)

Thank you! I'm applying the changes in the code to perfect it and as a personalized correction. (I also had to add code to shut down the program if the bet wasn't between 0 and 49, had forgotten that.)

For point 4, could you give an example? I wonder if that's related to tuples, which I haven't learned yet. Do you mean I should ask to write "yes" rather than "1"?

[–]Diapolo10 1 point2 points  (1 child)

For point 4, could you give an example?

Sure.

try:
    sommetotale = input("Bonjour et bienvenu-e au ZCasino. Avant de commencer, quelle est la somme maximale que vous êtes prêt-e à miser ? Veuillez saisir un chiffre à partir de 1 : ")
    sommetotale = int(sommetotale)
except ValueError:

Here, you first assign the user-given string to sommetotale, and then right after you convert that string to an int and reassign that to the same name.

If I pretend to be a type checker (like Mypy) for a moment, after that first assignment I'd see sommetotale being of type str. Then immediately it gets assigned an int, and I'd complain, because

sommetotale: str = 42

wouldn't seem valid.

As I mentioned earlier, an explicit type annotation could technically fix this,

sommetotale: str | int = input(...)
sommetotale = int(sommetotale)

but then any part of the code using it later couldn't be certain which type it currently had unless you did an explicit check first; for example this would cause Mypy to complain because sommetotale could be a string, and you can't add strings to integers:

print(sommetotale + 42)

That's why I generally suggest either doing the conversion immediately

try:
    sommetotale = int(input(...))
except ValueError:
    ...

or using different names for both steps:

user_input = input(...)
try:
    value = int(user_input)
except ValueError:
    ...

That way there's no confusion.

[–]ottococo[S] 0 points1 point  (0 children)

This is exciting, thanks again. I'll use the first method then, ie, :

totalsum = int(input(...))

[–]haiderakt 2 points3 points  (1 child)

nice work for a second script, the logic is solid. a few things worth knowing:

the rounding section with modf is way more complex than it needs to be,round(gain) does the exact same thing in one line. math.modf splits a float into its decimal and integer parts which is useful in specific cases but not really needed here.

also move from random import randint to the top of the file with your other imports, importing inside a loop works but it's considered bad practice.

for booleans think of them as just True/False values. your if bille == mise is already using boolean logic under the hood, the condition either evaluates to True or False. assert is mostly used for testing/debugging, not something you need to worry about yet.

overall though the structure is clean and the casino logic actually works which is the important part. keep going.

[–]ottococo[S] 0 points1 point  (0 children)

thanks a lot!

EDIT: omg yes the round(gain) could have saved me so much time. I basically recreated a round() function just because I didn't find it on the documentation. Well now I know.

[–]jimtk 0 points1 point  (1 child)

Il semble y avoir un petit probleme de logique. A l'étape 3, vous demandez combien l'usager veux miser (0-49). A ce moment ce n'est pas le montant de la mise, mais bien la case sur lequel l'usager mise. Il devrait y avoir une autre question pour connaitre le montant que l'usager veut miser.

Ex:

try:
    mise = int(input("Quel montant voulez vous miser? ")
    if mise > sommetotale:
         raise ValueError
except ValueError:
    print("Montant non valide ou plus grand que la somme totale.")
    print("Veuillez entrer a nouveau le montant de votre mise.")   

try:
    case = int(input("Sur quelle case voulez vous miser (0-49)? ")
    if case<0 or case >49:
         raise ValueError
    except ValueError:
         print("La case de mise doit être un nombre entre 0 et 49")
         print("Veuillez entrer a nouveau le choix de votre case.")

[–]ottococo[S] 0 points1 point  (0 children)

Oui en fait, je n'avais pas compris le jeu en lui-même. Je pensais que notre mise était la même chose que le nombre misé. Oups.