samedi 13 novembre 2021

How to fix 'if' statement activating with 'and' arguments when 'else' should be activating in python basic guessing operation?

Neophyte here. I'm trying to apply what I've learned so far in Python to create better understandings of how things work so far. I'm combining someone's number guessing game with freecodecamp's guessing game. When the 'guess limit' is met, I lose correctly. However when the number is entered correctly, the user still 'loses'.

For example in the code below, I call the function with x = 5, and when I 'guess' 5, I still receive the 'if limit' action when I want it to be the 'else' action.

I've tried several variations to see if I could figure it out, but to no avail. Any support would be greatly appreciated. If there's a link or article you could guide me to, that'd be great.

def numguess(x):
    number = x
    guess = 0
    guess_count = 1
    guess_limit = 5
    limit = False

    while guess != number and not(limit):
        guess = int(input("\nGuess the number: "))
        if guess < number and guess_count <= 4:
            print("too low")
            guess_count += 1
        elif guess > number and guess_count <= 4:
            print("too high")
            guess_count += 1
        else:
            limit = True
        
    if limit:
        print("you lose")
    else:
        print("you win")
numguess(5) 

Aucun commentaire:

Enregistrer un commentaire