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 59,572 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