Hi there, I am in a first year programming class at Dalhousie University (Halifax, Nova Socita, Canada), and I need a little help with an assignment. The exercise is as follows:
"[10 Points] Create a number guessing game. First, ask the user for some positive integer, n. Ask the user to select any number between 0 and n and have your program try to guess it. After each guess, ask the user if the guess is correct. If incorrect, your program should ask the user if the user's chosen number is higher or lower than the guess. Have the program keep track of the number of guesses it needed to find the answer. Try to find the answer in as few guesses as possible! An excellent implementation should be able to guess a number between 0 and 1000 within 10 tries. Remember to add descriptive comments to your code."

Heres what I have:

import random

n = input("Hi! Please enter a positive integer!")
print "Thanks! Now, pick a number between 0 and your number!"

guess = random.randint(0, n)
attempts = 0

print "Is", guess, "your number?"
answer = input ("1 = yes, 2 = no")

while answer == 2:
    highlow = input ("Is the number higher or lower? 1 = higher 2 = lower")
    if highlow == 1:
        guess1 = random.randint(guess, 50)
        print "Is", guess1, "your number?"
        answer = input ("1 = yes, 2 = no")
        attempts += 1
    if highlow == 2:
        guess1 = random.randint(0, guess)
        print "Is", guess1, "your number?"
        answer = input ("1 = yes, 2 = no")
        attempts += 1

if answer == 1:
    print "I WIN!!!!!"
    print "I guessed your number in", attempts, "guesses!"

The program works, however, is not very smart. As you can see, I set it to guess a number between 0 and 50 rather than 1000 and it still cannot guess the number is a decent number of tries. I know whats wrong with it, its that I am having the program guess random numbers between 0 and the initial guess or between the initial guess and 1000, which can keep the computer guessing for ages. How can I make the program smarter so that it keeps track of its guesses and makes it next guess based on that, rather than just guessing another random number? Would Appreciate any help!

Recommended Answers

All 5 Replies

Actually, this program doesn't entirely work. If its first guess is over 50 and the real answer is higher than that, it will raise a ValueError.

What you want to do is have a variable to keep track of the lowest number that could possibly be the answer, and another to keep track of the highest. Update one of these variables after every guess, then use them to make your next guess. (An "excellent implementation" wouldn't actually guess randomly at all; it would try to narrow the range as much as possible on each try.)

First of all, your code has some minor problems, for example try changing:

n = input("Hi! Please enter a positive integer!")

with this:

n = raw_input("Hi! Please enter a positive integer: ")

Secondly, Try to familiarise yourself with lists, tuples and dictionaries in Python. In this case, if you use a list, you can simply keep track of those numbered guessed before.

And lastly, let's get down to your algorithm; off the top of my head, I suggest you to divide the interval to 2 parts and get the middle number and ask the user, if it's greater than their number, get lower interval and cut it in half, otherwise get the lower half. Confused??? Let me explain by example:

let's say, the user chooses 50. So you have all integers from 0 to 50(Let's assume user has chosen 17 as their number). So get the middle number, which in this case, is 25. Ask them if 25 is their number, if no, then is it bigger than their number?? yes!!! So get from 0 - 25, cut in half and get 12. Ask user again!! smaller, now, get from 12 - 25, get the middle number which is 18. Still larger, so get from 12 to 18, get middle number, 15 which is smaller than 17, get 15 to 18, get middle, you'll get 16, now 16 - 18, and finally you get 17.

It may seem too much hassle, but it's not as I tried to choose the worst case scenario in this example. In other words, you try to tighten your interval each time you cut it in half. So I can say, you most probably will get the number in less than 7, 8 tries in 1000 integers.

Try and report back. good luck!! ;)

i've added the variable guessed=[0] and have added guessed.append(guess) to the end of each if statment to create a list of numbers the program has already guessed, however, it is still guessing the same numbers twice, how do I prevent it from doing so.

You could maybe use set as it is more effective. Use not in condition to check the guesses. The moving limits are not hard to implement also and more effective. If user says too big, that value becomes value of 'less_than', otherwise our guess comes 'bigger_than', our next guess is average of those, until we hit it.

The brute force way to guess would be to generate a list of 50, or 1000, numbers, and delete the guess from the list. Then use random.choice(numbers_list) to generate the next number. You would also want to slice off part of the list when you change the lower or upper number boundaries. But it would indeed be easier to divide (higher-lower) by 2 and guess that until you reach some low number like 5 or 10 for a difference. Then just guess them in order.

it is still guessing the same numbers twice, how do I prevent it from doing so.

## NOT tested
    if highlow == 1:
        lowest = guess
    if highlow == 2:
        highest = guess

    not_new = True
    while not_new:                              ## keep trying until one is found
        guess = random.randint(lowest, highest)
        if guess not in already_guessed_set:
            not_new = False
            already_guesed_set.add(guess)   ## I would suggest a set instead of a list
    print "Is", guess, "your number?"
    answer = input ("1 = yes, 2 = no")
    attempts += 1
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.