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

Finn Gray
Finn Gray
3,009 Points

Minigame stopping partway through?

Hi everyone, this is my first time posting and is my first mini-project:

#This is a very basic number guessing game created to practice boolians

name = input("Hi there, what's your name?   ")
game_question = input("Hi {}, would you like to play a game? yes/no   " .format(name))
if game_question == "yes":
    answer_1 = input("Brilliant stuff! I'll think of a number and you have 5 attempts to guess what it is. Still want to play!? yes/no   ")
    if answer_1 == "yes":        
        guess_1 = input("Ok then {}, I'm thinking of a number between 1 and 50.. Go on take a guess...   " .format(name))
        if int(guess_1) == 42:
            print("Well done! You've guessed right! And you now know that 42 is the meaning of life.")
        elif int(guess_1) < 42:
            guess_2 = input("Nope, try a bit higher...   ")
        else:
            guess_2 = input("Nope, you're a bit too high...   ")

            if int(guess_2) == 42:
                print("Well done! You've guessed right! And you now know that 42 is the meaning of life.")
            elif int(guess_2) < 42:
                guess_3 = input("Still not right, try a bit higher...   ")
            else:
                guess_3 = input("Still not right, try lower...   ")

                if int(guess_3) == 42:
                    print("Well done! You've guessed right! And you now know that 42 is the meaning of life.")
                elif int(guess_3) < 42:
                    guess_4 = input("You're not very good at this are you, higher...   ")
                else:
                    guess_4 = input("You're not very good at this are you,lower...   ")

                    if int(guess_4) == 42:
                        print("Well done! You've guessed right! And you now know that 42 is the meaning of life.")
                    elif int(guess_4) < 42:
                        guess_5 = input("Last try, a little higher...   ")
                    else:
                        guess_5 = input("Last try, a little lower...   ")

                        if int(guess_5) == 42:
                            print("Well that took you long enough... at least you now know that 42 is the meaning of life.")
                        else:
                            print("YOU LOSE! But thanks for playing my game anyway :) ")

    else:
        print("That's a shame... please come and see me again when you feel like playing :( ")
else:
    print("That's a shame... please come and see me again when you feel like playing :( ")

For some reason, while playing my little number search minigame the console simply stops asking for inputs and ends the code. Any thoughts and what may be causing it?

Thanks in advance,

Finn

1 Answer

With the nested if statements, your code exits if any of the elif statements are true. You should try this project with a while statement instead.

Soon you will learn how to use loops and it will actually make things easier. I recommend putting this project on hold and actually using it later as good practice while you are learning how to loop. :)

Finn Gray
Finn Gray
3,009 Points

Thanks both of you! As suggested, I'll put the project on hold for a bit and come back to it once I'm a little further in the Python basics course :)