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

Nigel Chia
Nigel Chia
909 Points

reverse number game help

import random 

def isnt_a_num():
    print('that isnt a number')

def computer_guess(low,high):
    return random.randint(low,high)

def game():
    guesses=[]
    try:
        tries=int(input('how many tries do i have?: '))
    except ValueError:
        isnt_a_num()
    while len(guesses)<tries:
        try:
            the_answer=int(input('what shall the number be?: '))
            low= int(input('what is the minimum?: '))
            high= int(input('what is the maximum?: '))

            guess= computer_guess(low,high)            
        except ValueError:
            isnt_a_num() 
        else:
            print('The number i guessed is {} and the answer was {}'.format(guess,the_answer))
            yes_no=input('Was my guess right?: y/n')
            if yes_no.lower()=='n':
                print('Bummer')
                guesses.append(guess)                
            else:
                print('YAY')
                yes_no_two= input ('can we play again?: y/n')
                if yes_no_two.lower()=='n':
                    print('okay goodbye then :(')
                elif yes_no_two.lower()=='y':
                    print('YAYYYY')
                    game()
                else:
                    print('i dont understand')
                break 

game()

my game isnt functioning like i want to, it doesnt keep the same answer i've provided instead does the

            the_answer=int(input('what shall the number be?: '))

            low= int(input('what is the minimum?: '))

            high= int(input('what is the maximum?: '))


            guess= computer_guess(low,high)

again until the amount of tries is up

[MOD: added ```python for matting -cf]

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

If you wish the the_answer, low, and high to remain unchanged during the guessing loop, then those statements need to be move outside of the while loop so they are only asked at the beginning of the game.

Post back if you need more help. Good luck!

Nigel Chia
Nigel Chia
909 Points

thanks it worked