Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stuck in a while loop while using if statement

I'm new to python and I'm trying to make a simple Guess the number game, and I'm stuck in an if statement in a while loop. here is the code.

I'm experiencing it at the Your guess it too high and the low one. I tried breaking it, but it simply stops the whole things

def guess_the_number():
    number = random.randrange(20)

    guessesMade = 0

    print('Take a guess')
    guess = input()
    guess = int(guess)

    while guessesMade < 6:
        if guess < number:
            print('Your guess is too low.')

        if guess > number:
            print('Your guess is too high.')

        if guess == number:
            break

    if guess == number:
        print'You got it in', guessesMade, 'guess(es)! Congratulaions!'
    else:
        print'I\'m sorry, the number was', number
like image 636
GTAirpline Avatar asked Jun 03 '15 12:06

GTAirpline


1 Answers

You never increment guessesMade so guessesMade < 6 will always be True. You need to modify this value within your loop. You also need to move your prompt for user input into the loop

while guessesMade < 6:
    guess = int(input('Take a guess'))

    if guess < number:
        print('Your guess is too low.')
        guessesMade += 1

    elif guess > number:
        print('Your guess is too high.')
        guessesMade += 1

    else:
        break
like image 89
Cory Kramer Avatar answered Sep 28 '22 20:09

Cory Kramer