Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Enzo Cnop
Enzo Cnop
5,647 Points

Why does my number guessing game fail to start?

I created the numbers game on my own, then reworked it to nearly exactly what Kenneth did. However, I made it in Atom and test it in my terminal shell (rather than Workspaces) to simulate a 'real' development process. I can't figure out why the program won't start. I get no errors, but when I launch the program, it simply acts as though I've pressed 'return' on a blank line. The code is below:

import random

def game():
    # Generate random numer between one and ten.
    secret_num = random.randint(1, 10)
    guesses = []

    while len(guesses) < 5:
            try:
                guess = int(input("Guess a number between one and ten!"))
            except ValueError:
                print("{} isn't a number!".format(guess))
            else:
                # compare guess to secret number.
                if guess == secret_num:
                    print("You got it! My number was {}".format(secret_num))
                    break
                elif guess < secret_num:
                    print("That's too low!")
                elif guess > secret_num:
                    print("That's too high!")
                else:
                    print("That's not it!")
                guesses.append(guess)
    else:
        print("You didn't get it! My number was {}".format(secret_num))
    play_again = input("Do you want to play again? y/n ")
    if play_again.lower() != 'n':
        game()
    else:
        print("Bye!")
'''

1 Answer

You don't call the game function within the code, you only define it. Defining the function won't actually cause any of the code to run. If you add a call to the game function on the last line like this:

import random

def game():
    # Generate random numer between one and ten.
    secret_num = random.randint(1, 10)
    guesses = []

    while len(guesses) < 5:
            try:
                guess = int(input("Guess a number between one and ten!"))
            except ValueError:
                print("{} isn't a number!".format(guess))
            else:
                # compare guess to secret number.
                if guess == secret_num:
                    print("You got it! My number was {}".format(secret_num))
                    break
                elif guess < secret_num:
                    print("That's too low!")
                elif guess > secret_num:
                    print("That's too high!")
                else:
                    print("That's not it!")
                guesses.append(guess)
    else:
        print("You didn't get it! My number was {}".format(secret_num))
    play_again = input("Do you want to play again? y/n ")
    if play_again.lower() != 'n':
        game()
    else:
        print("Bye!")
game() # Call the game function

Then your code should work.

You misspelled the game() command. It should be game() not game(); with a semicolon at the end.

Ops you are correct, i mainly program in languages like JavaScript, C#, Java etc where a semicolon is either required or recommended so it's a habit of mine.

Though in my defence I did test the code before I posted it and it runs fine with the semicolon included. Semicolons are allowed in Python, they just aren't recommended since they don't serve any purpose when used in the way I did in the above example.