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

Number game, computer version

I have the general gist of this running. How would I limit the next higher or lower guess from (1,10) -> (guess, 10) -> (old guess, new guess).

import random

def game():
    comp_guesses=[]
    number = random.randint(1,10)
    print("Think of a number between 1 and 10 and the computer will try and guess it")
    while len(comp_guesses) < 5:


        print("Is your numbner {}?".format(number))
        answer = input("Was my guess correct, too high, or too low? ")
        if answer == "correct":
            print("I am awesome")
            break
        elif answer == "too high":
            print("Darn! I will guess again")
            comp_guesses.append(comp_guesses)
            number = random.randint(1, number)

        elif answer == "too low":
            print("Darn! I will guess again")
            comp_guesses.append(comp_guesses)
            number = random.randint(number, 10)


    else:
        print("I guessed too many times!")
    play_again = input("Do you want to play again? Y/n ")
    if play_again == "Y":
        game()
    else:
        print("Goodbye!")
game()

You just need to change :

number = random.randint(comp_guesses[-1] ,10) 
number = random.randint(1, comp_guesses[-1])

The line before generating a new randint, the previous number put at the end of the guessed numbers list, and we can access that by index -1.

Btw It's a nice program, good job Jonathan !