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.

Python Python Basics (2015) Number Game App Squared

wesley jackson
wesley jackson
2,436 Points

Number Game Refinement

After this section of code was added the game does not ask for input it just does not run.

play_again = input("Do you want to play again? Y/n") if play_again.lower() != 'n': game() else: print("bye!") even after removing .format(guess) form the try block

:-( Much appreciation for any pointers.

Thank you. Aplogies but this seems the only way I can get the code to be readable as I have tried to use markup but without success

squared.py
import random
def game():
    # generate a random number between 1 and 10
    secret_number = random.randint(1,10)
    guesses = []
    while len(guesses) < 5:
        try:
            # get a number guess from the player
            guess = int(input("Guess a number between 1 and 10 :"))
        except ValueError:
            print(" That isn't a number")
        else:
            # compare to secret number
            if guess == secret_number:
                print("You got it! My number was {}".format(secret_number))
                break
            elif guess < secret_number:
                print("Number is higher than {}".format(guess))
            else:
                print("Number is lower than {}".format(guess))
            guesses.append(guess)
    else:
        print("You did not get it my number was {}".format(secret_number))
    play_again = input("Do you want to play again? Y/n")
    if play_again.lower() != 'n':
        game()
    else:
        print("bye!")

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,989 Points

What calls game() to start play? The code as written defines a game() function but does not call it.

:point_right: add call to game at end of file:

game()

:point_right: add Python standard hook at end of file to execute if module executed:

if __name__ == "__main__":
    game()
wesley jackson
wesley jackson
2,436 Points

Hi Chris, Thank you but alas it is still not working and after adding the changes and snippet of code the last 'else' statement is said to be out of indentation. I will try and have another go wth it.

Thank you