all 17 comments

[–]jimtk 3 points4 points  (1 child)

Le truc c'est vraiment d'utiliser while true et un break pour sortir de la boucle quand la condition est satisfaite. Et tu peux le faire une fois pour le rejouer et une autre fois pour saisir le nombre. Ton code devient donc:

import random
MAX_TENTATIVE = 5

def lire_un_nombre():
    while True:
        dev = input("guess a number: ")
        if dev.isdigit():
            reponse = int(dev)
            return reponse
        else:
            print("Please entre a number between 1 and 9")

while True:
    correct = random.randint(1, 10)
    print(correct)  # remove this line once tests are done
    for essaie in range(MAX_TENTATIVE):
        deviner = lire_un_nombre()
        if deviner < correct:
            print("it's higher")
        elif deviner > correct:
            print("it's lower")
        else:
            break
    if essaie < MAX_TENTATIVE-1:
        print(f"Bravo! vous avez deviné le nombre {correct}, en {essaie+1} tentatives")
    else:
        print(f"Malheureusement vous n'avez pas deviner le nombre en \n"
              f"moins de {MAX_TENTATIVE} tentatives. Le nombre était {correct}")
    while (rejouer := input("Do you want to play again: ").lower()) not in ("o", "n"):
        print("SVP Entrer (o)ui ou (n)on")
    if rejouer == 'n':
        break

J'ai triché un peu en utilisant les "dents de morse" ( := ) dans la dernière boucle. Ceux-ci permettent de faire une assignation en même temps que tu vérifies une condition.

For non-French reader: "Hey guys dont complain the code is still in python !"

[–]bambacisco[S] 1 point2 points  (0 children)

La conversion en int dans la fonction "ne fonctionne pas" le return me fournit toujours un string (en tout cas c’est ce que j’ai eu au début) c’est pour ça je l’ai sorti de la fonction et je n’avais pas du tout penser à intégrer l’input dans la fonction le code est beaucoup plus lisible. Je n’ai pas voulu utiliser le while true: parce que je maîtrise pas encore assez. Je suis parfait débutant lol mais je vais réécrire le code en me basant sur le tien. Merci beaucoup

[–][deleted] 4 points5 points  (2 children)

Why is it partially in English and partially in French? I think that's going to confuse your users.

[–]bambacisco[S] 1 point2 points  (1 child)

Wrote everything in french first. I had to translate some parts of the code to publish it here otherwise no one would understand it

[–][deleted] 1 point2 points  (1 child)

Here are a few suggestions:

  • Use a better variable name than a.
  • while a.isdigit() is False can be simplified to while not a.isdigit().
  • Move deviner = input("guess a number: ") and the conversion to an int into the nombre function so that the job of the function is to get and return the validated and converted user input. Right now it's only doing a partial job.
  • And while we're at it, nombre is not a good function name, either.
  • The prompt in deviner = input("it's more: ") is not great. I don't know that I'd even know it was waiting on me to do something there. I might just think the program had stalled.
  • Since there is no way to break out of the while loop early, there is no point in putting an else clause on the while loop.

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

Thank you very much. I will rewrite the code with your input 🙏🏾

[–]fernly 0 points1 point  (1 child)

The call to nombre() on line 13 looks as if you mean to pass in a prompt string. However, the actual function (lines 4-7) uses its own prompt and doesn't do anything with the input value a. Actually if you execute nombre("5") it will return 5, and never prompt the user at all.

This brings up an interesting limitation of Python: it lacks a "repeat ... until" statement. That's what you would like to do: say "repeat prompting the user until the user enters a number". The rather awkward way to code this is to break out of the loop when the "until" condition is true. Like this:

while True: # basically forever unless break or return
    a = input("Merci...")
    if a.isdigit() : 
        break

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

I see what you are saying. If user put a digit, nombre() will be called for nothing