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 Python Basics (2015) Number Game App Number Game Takeaways

Jonathan Whelchel
Jonathan Whelchel
2,096 Points

Reverse Numbers game help

I need help with the reverse numbers game. Below is the code I have so far

import random

secret_num = random.randint(1, 10)
def second_choice():
    second_choice = random.randint(1, 10)


def game():
    guesses = []
    while len(guesses) < 5:
        print(secret_num)
        guesses.append(secret_num)
        guess_again = input("Did I get your number right? Y/n. ")
        if guess_again == 'y':
            print("You got it")
            break
        elif guess_again == 'too high':
            print('I will choose a higher number')
            second_choice()

I cannot figure out how to get my computer to guess again and guess a different number and not repeat one he has already guessed.

Jonathan Whelchel
Jonathan Whelchel
2,096 Points

So now I am at a point where I can't get the computer to guess a different number each try. But I am having trouble limiting the computer to 5 tries. Also getting it to guess higher or lower than the previous guess.

4 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

I very simple way would be to add an input question "do you want to continue" after the while loop. If Yes, then simply call game() to restart.

This has the minor drawback that this is a recursive call (game() calling game()) which does have a compiler limit of ~1000 so after 1000 games it might crash with a RecursionError: maximum recursion depth exceeded. This is acceptable for this level of coding.

Jonathan Whelchel
Jonathan Whelchel
2,096 Points

so I've gotten to that point. but the computer keeps guessing the same number. How can I tell it to guess higher or lower

Jonathan Whelchel
Jonathan Whelchel
2,096 Points
import random

secret_num = random.randint(1, 10)
def second_choice():
    second_choice = random.randint(1, 10)


def game():
    guesses = []
    while len(guesses) < 5:

        print(random.randint(1, 10))


        guess_again = input("Did I get your number right? Y/n. ")

        if guess_again.lower() == 'y':

            print('You got it!')
            play_again = input("Do you want to play again? Y/n ")

        elif guess_again.lower() == 'n':
            game()


game()  
Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

It looks like you're not saving the guesses.

  • Save randint result in variable guess
  • add `guesses.append(guess)
  • print guess
  • adjust the guess by replacing the arguments of the randint with (last_too_low, last_too_high)
  • add code to track last_too_low and last_too_high
  • you will need to adjust the feedback from the user to know if guess was too high or low
Jonathan Whelchel
Jonathan Whelchel
2,096 Points

Ok That sounds great! But I don’t think I learned how to do any of that so far. Mind walking me through?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

It might be better to revisit this after getting farther along in the coursework.