Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Number Game Refinement.

Hello everyone,

So I've managed to do most of the challenge on my own but I'm having certain issues that I can't seem to solve on my own.

1 - If my first input isn't an int, I get an error.

2 - When I make my 5 mistakes, my code will print whether the input is higher/lower than the secret number instead of going straight to: print('That wasn't it.. My number was {}'.format(secret_num))

Also please give me feedback on the cleanliness of the code or any other advice or comment that could help.



import random

def num_game():

    secret_num = random.randint(1, 25)
    attempt = []
    print('Welcome to the number game! You only have 7 attempts!')
    print('Guess a number that\'s between 1 and 25:')

    while len(attempt) < 7:

            guess = int(input())

        except ValueError:
            print('{} is not a number!'.format(guess))

            if guess == secret_num:
                print('Congratulations! That\'s it! You\'ve found my number!')

            elif guess < secret_num:
                print('You\'re number is higher than {}!'.format(guess))

                guess > secret_num
                print('You\'re number is lower than {}!'.format(guess))


        print('That wasn\'t it.. My number was {}!'.format(secret_num))

    play_again = input('Do you want to play again? Y/N')

    if play_again == 'Y':


        print('Got it. See you next time!')


1 Answer

Hi Mohamed

I did this game too, but didnt realise the bug regards the trying to convert the input to an integer, it looks like when it hits that ValueError the variable guess doesnt get assigned, hence when you try and print it out it goes all crazy. In regrads to your program exiting at 5 tries the reason for this is you have set the length of attempt to less than 7,

change the following lines of code and it should work.

    while len(attempt) < 5:

            guess = int(input())

        except ValueError:
            print('Only whole numbers are allowed !)