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

My post-Python Basics project exercise

Hello Coders!

I went through Python Basic Course and I've found an exercise to do on the Internet. The project assumes a simple program where user is guessing the number. I did it and it is working, but anyway I feel I missed something important :P Also I think it's too simple but I cannot get an Idea how do it more complex.

If anyone could take a look and give a feedback would be so awesome!

import random

name = input("What is your name? ")
name = name.capitalize()
wanna_play = input('hello, {}! Do you want to play "Guess the number"? Y/N '.format(name))

while wanna_play.lower() == "y":
    number = random.randint(1, 20)
    try:
        guess = int(input("So! Guess the number between 1 and 20!  "))
        if guess <  number:
            wanna_play = input("Your number was too low! Do you want to take another chance? Y/N ")
        elif guess > number:
            wanna_play = input("You passed too high number! Do you want to take another chance? Y/N ")
        else:
            wanna_play = input("Great! You guessed correct number! Do you want to try once more? Y/N ")
    except ValueError:
        print("Sorry! This value is not valid!")
print("Arrivederci, {}!".format(name))

3 Answers

You need to think some more about what is happening within the while loops. Suppose someone starts by entering a high number. Your code will skip the first loop, because the number isn't lower than the correct answer. It will then run this loop:

        while guess > number:
            guess = int(input("You passed too high number! Try once more: "))
        else:
            guess = input("Great! You guessed correct number!")

It will print the message that your number is too high, and ask you to enter a new number. Great! But suppose the next number they enter is too low. The while loop will see that your number is NOT too high, and will go to the else clause, claiming you entered the correct number.

You want it to actually go back to the original while loop, but it will never go back to that.

Why don't you try structuring the code a little differently. Just have one while loop, which you only exit when the user enters the correct number. Something like:

while guess != number:

Then, within the loop, check if the number was too large or too small, and print the appropriate message. After that, ask for a new guess. If they get out of the while loop, they must have entered the correct number, so print the correct message.

Does that help?

Thank you for a great reply. I am still confused why it actually is like that, doesn't make sense to me but I get what you are talking about. So I made a correction which looks like this and I think it's really working now:

if wanna_play.lower() == "y":
    number = random.randint(1, 20)
    try:
        guess = int(input("So! Guess the number between 1 and 20!  "))
        while guess !=  number:
            if guess < number:
                print("You passed too low number! Try once more: ")
            elif guess > number:
                print("You passed too high number! Try once more: ")
            else:
                print("Great! You guessed correct number!")
            guess = int(input("Try once more: "))
    except ValueError:
        print("Sorry! This value is not valid!")

That looks much better!

You may notice that you never get the message that you guessed the right number. That is because as soon as you guess the right number, you will exit from the loop. So you you might want to move that message outside the loop.

Since the random number is created inside the loop it changes each time through. Therefore the high and low messages don't help.

Thank you Kris for a message! And is this code OK, if I wanted just to finish the game after each draw and give a user just an information if his number was too high, too low or correct, and then ask him if he wants to play again, from the beginning?

edit: I have also modified the code to run randomly only one number and then ask the user for it giving a hint if it was too high or low. How does it look?

import random

name = input("What is your name? ")
name = name.capitalize()
wanna_play = input('hello, {}! Do you want to play "Guess the number"? Y/N '.format(name))
if wanna_play.lower() == "y":
    number = random.randint(1, 20)
    try:
        guess = int(input("So! Guess the number between 1 and 20!  "))
        while guess <  number:
            guess = int(input("Your number was too low! Try once more: "))
        while guess > number:
            guess = int(input("You passed too high number! Try once more: "))
        else:
            guess = input("Great! You guessed correct number!")
    except ValueError:
        print("Sorry! This value is not valid!")
elif wanna_play.lower() == "n":
    print("That's a pitty you don't want to play :(")
else:
    print("Sorry, but I don't understand... :-/")
print("Arrivederci, {}!".format(name))