[Python] How can I use a "for _ in range()" to count guesses?

Discussão em 'Python' iniciado por Stack, Janeiro 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm just starting coding with Python and have no programming experience prior.

    I tried to make a for ... in range function to count the guesses until 3 incorrect ones were guessed, but found guesses += 1 with if guesses == 3: worked a lot easier.

    import random

    # - Write a program where the computer picks a random number between 1 and 100.
    # - The user has to guess the number, with the program providing hints ("too high" or "too low").
    # - Use loops to allow multiple guesses and conditionals to check user inputs.
    # - Bonus: Add a scoring system based on the number of guesses.
    def guess_game():
    get_guess = random.randint(1, 100)
    guesses = 0

    while True:
    guess = input("Guess number: ")
    guess = int(guess)

    guesses += 1
    if guesses == 3:
    print(f"You are out of guesses! The number is {get_guess}")
    break

    if guess < get_guess:
    print("Too low")
    elif guess > get_guess:
    print("Too high")
    elif guess == get_guess:
    print(f"yes {guess} is the correct number!")
    break

    guess_game()

    Continue reading...

Compartilhe esta Página