Sorry guys! Last time I posted, I didn't know about code tags! I put them in.

SO, I'm trying to write a python program that guesses a number that the user chooses between 1 and 1000 in no more than 10 guesses and I can not figure it out for my life! I know it's super beginner and there are a lot of problems, but please be patient with me.

SO HERE'S MY PROGRAM SO FAR.... :

#print instructions to the user
print 'Guess a number from 1 to 1000 and I will guess what it is in less than 10 tries! After each guess, tell me either "correct", "lower", or "higher".'

#here's the guesser...

def guess():
    guesses = 1
    low = 0
    high = 1000
    trick = high-low           
    while guesses < 11:
        lower = trick/2+low    #originally 250...
        higher = trick+lower   #originally 750...
        print trick
        correct = trick
        
        question = "Is this your number? "
        user_answer = input(question)

        if user_answer == lower:
            high = trick
            trick = lower
            guesses = guesses + 1
                                    
        elif user_answer == higher:
            low = trick
            trick = higher
            guesses = guesses + 1
            
        elif user_answer == correct:
                print "Sweet! That took me", guesses, "tries!"
                guesses = 10
        
        else:
            print 'Please enter "correct", "lower", or "higher"'

    while not guesses < 11:
        print "I'm a failure..."
        guesses = 10

guess()

OK, well there are a few problems with your error checking/input validation. Also your loops are a bit wierd, the program continues after it guesses correctly. And the number guessing algorithm looks a bit dodgy too!

Here's a slightly improved version of your program.
Changes made:
I've used a very simple binary search algorithm to guess the users number. Hopefully the comments in the code will clarify the logic used!

I've fixed your main while loop, so it loops until the CPU makes a correct guess or the CPU runs out of guesses.

I've also strengthened the input validation by adding an additional nested while loop. So the user is repeatedly asked if a guess is correct until they provide a valid answer..

I'll post the code twice. The first version I've commented heavily so you can see what's going on in my added code. The 2nd will be without the comments, so the actual code should be a bit more readable!

Here's the heavily commented version:

#print instructions to the user
print 'Guess a number from 1 to 1000 and I will guess what it is in less than 10 tries! After each guess, tell me either "correct", "lower", or "higher".'

#here's the guesser...

def guess():
    guesses = 0
    low = 0
    high = 1000

    isCorrect=False # boolean sentinel flag..Has the user said that a guess was correct?

    # while guesses are < 10 and user hasn't said that a guess was correct
    while guesses < 10 and not isCorrect:
        # make a guess...
        # this is a simple binary search style algorithm
        # guess = lowest value + (range/2)
        # where range = highest value - lowest value.
        trick = low + ((high-low)/2) # first guess will be 500
                                     # 0 + ((1000-0)/2) => 0 + (1000/2) => 0 + 500

        question = "Is your number " + str(trick) + "? "
        isValidReply=False        # boolean sentinel flag - is user input ok?
        
        while not isValidReply:   # keep asking the question until you get a valid reply
            user_answer = raw_input(question) # NOTE: using raw_input instead of input

            if user_answer == "lower":
                # users number is lower,
                # so the current guess must be the upper bound of the range
                high = trick
                isValidReply=True # this is a valid reply
                
            elif user_answer == "higher":
                # users number is higher,
                # the current guess must be the lower bound of the range
                low = trick
                isValidReply=True # this is a valid reply
                
            elif user_answer == "correct":
                isCorrect=True
                isValidReply=True # this is a valid reply
            
            else: # invalid reply... so we'll end up repeating the question!
                print 'Please enter "correct", "lower", or "higher!"\n'

        guesses += 1

    # now we're out of the main loop, were we correct?
    if isCorrect:
        print "Sweet! That took me", guesses, "tries!"
    else:
        print "I'm a failure..."

guess()

And here it is again without all of the extra comments:

#print instructions to the user
print 'Guess a number from 1 to 1000 and I will guess what it is in less than 10 tries! After each guess, tell me either "correct", "lower", or "higher".'

#here's the guesser...

def guess():
    guesses = 0
    low = 0
    high = 1000

    isCorrect=False 

    while guesses < 10 and not isCorrect:
        trick = low + ((high-low)/2) 
                                     
        question = "Is your number " + str(trick) + "? "
        isValidReply=False        
        
        while not isValidReply:    
            user_answer = raw_input(question)

            if user_answer == "lower":
                high = trick
                isValidReply=True
                
            elif user_answer == "higher":
                low = trick
                isValidReply=True
                
            elif user_answer == "correct":
                isCorrect=True
                isValidReply=True 
            
            else: 
                print 'Please enter "correct", "lower", or "higher!"\n'

        guesses += 1

    if isCorrect:
        print "Sweet! That took me", guesses, "tries!"
    else:
        print "I'm a failure..."

guess()

Hope that helps you a little. If you need any of it to be explained further, don't hesitate to ask!

Cheers for now,
Jas.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.