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

How do you make program loop from a specific line?

Im making this simple guess a number game, and i want it to be able to detect if you have already guessed a number. On line 9 ive made it so that it can detect that however i want it to restart the if the number has already been guessed. How would i go about doing that?

import random
def game():
    random_num = random.randint(1, 100)
    print("Im thinking of a number between 1 and 100, you only have 10 guesses")
    guesses = []
    while True:
        try:
            guess = int(input("Guess a number "))
            if guess in guesses:
                print("You have already guessed that number! ")
        except ValueError:
            print("You can only guess numbers")
        if guess == random_num:
            print("Congratulations you guessed it! My number was {}".format(random_num))
            play_again = input("Do you want to play again? Y/n ")
            if play_again.lower() == "y":
                game()
            else:
                break
        elif guess < random_num:
            print("That guess was too low!")
            guesses.append(guess)
            print("Guesses {}/10".format(len(guesses)))
            if len(guesses) >= 10:
                print("You have run out of guesses!")
                play_again = input("Do you want to play again? Y/n ")
                if play_again.lower() == "y":
                    game()
        else:
            print("That guess was too high!")
            guesses.append(guess)
            print("Guesses {}/10".format(len(guesses)))
            if len(guesses) >= 10:
                print("You have run out of guesses!")
                play_again = input("Do you want to play again? Y/n ")
                if play_again.lower() == "y":
                    game()


game()

Why don't you just call game() again, "if guess in guesses" resolves to true? Right after your print statement maybe?

3 Answers

Hi Behar!

Thanks for the more detailed explanation!

As I told you in the last answer, I said that you should make a variable named guesses, and set it to 10:

num_of_guesses = 10

For the while loop:

#Instead of **while True:**
while num_of_guesses > 10:

In the if statement where you add the guess to the guesses list:

elif guess < random_num:
            print("That guess was too low!")
            guesses.append(guess)
            num_of_guesses -=1    #decrement the number of guesses by one.
            print("Guesses {}/10".format(len(guesses)))
            if len(guesses) >= 10:
                print("You have run out of guesses!")
                play_again = input("Do you want to play again? Y/n ")
                if play_again.lower() == "y":
                    game()

Then:

#Make an if statement that says "if the guess is in guesses: print the statement **but do not decrement it by one!**"
if guess in guesses:
                print("You have already guessed that number! ")
#You do not need the rest of the code seen as you will automatically break out of the loop when the num_of_guesses #variable is equal to zero.

I hope that helps!

Thanks! :)

Ah thats smart! Ty so much for taking the time to explain that!

Hi behar!

I would do the same thing that you are doing except a few things:

Instead of making a list, I would give the guesses variable a value of 10, and decrement it by one when the user gets it wrong.

In terms of your question, all you need to do is when the user's number is equal to the random_num variable, print out a good job message, and then use the break keyword to break out of the loop.

I hope that helps!

Thanks! :)

Ty for the responses guys, however im not sure you understand what i mean. If the user has already guessed the number in question, i want to print the message "You have already guessed that number" which i am already doing. However seeing as the program just continues after that, it counts that as a guess, so you can run out of guesses by just guessing x 10 times. I want it to be so that when you guess a number that has already been guessed the game prints the message and asks you to guess a number again, untill you guess one that has not already been guessed. Btw i cant just call game() again seeing as it would restart everything