简体   繁体   中英

Python- Stuck on a guessing game's program?

So, I've been working on this guessing game problem for a while and I am left scratching my brain for the past 2 hours trying to figure out what's wrong but I can't. I also tried searching for the solution but I don't wanna do copy & paste and I actually want to solve my code by myself.

Here's what I've been able to get so far:

start = 0
end = 100
print 'Please think of a number between 0 and 100!'
user = ''
ans = (start + end) / 2

while user != 'c':
    print ('Is your secret number ' +  str((start + end) / 2) + '?')
    user = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
    if user == 'l':
        start = ans
    elif user == 'h':
        end = end - ans
    ans = start
print 'Game over. Your secret number was: ' + str((start + end) / 2)

What am I doing wrong? Edit: The game should run something like this:

Please think of a number between 0 and 100!
Is your secret number 50?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l
Is your secret number 75?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l
Is your secret number 87?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. h
Is your secret number 81?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l
Is your secret number 84?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. h
Is your secret number 82?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l
Is your secret number 83?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. c
Game over. Your secret number was: 83

You are settings ans = start , thats the mistake. Since you want to solve this yourself, I will not further explain things. This is what causes your program to never decrease below 25:

Please think of a number between 0 and 100!
Is your secret number 50?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. h
Is your secret number 25?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. h
Is your secret number 25?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. h
Is your secret number 25?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. h
Is your secret number 25?

I see two problems with your current code. First of all, you are never changing ans within the loop. You probably want to have ans contain the number that is the current guess. So you should set it whenever you do a guess and use it in the ouput directly. So move the ans = (start + end) / 2 line at the beginning of the loop.

The second problem, is that you set end = end - ans when the guess was too high. While this works most of the times as you are using binary search and always guess in halves, it is not really what you want to do. If you want to search in the range [start, end] then you should set end to the highest number that is still available for guessing; that would be ans - 1 when you guessed too high.

Lastly, you probably want to catch the situation where start == end . In this case, either you have found the number, or the user has entered some wrong stuff.

Also as a general tip, printing out intermediary results can help a lot when debugging. For example you could put a print(start, end) at the top of your loop to see the range you are looking at during each guess. Then you would have easily noticed, that the beginning of the range never changed.

My solution works:

import random
numH = 100
numL = 0
num = random.randint(numL, numH)
print num

x = raw_input('Enter high, low or number 1: ')
while x == 'l' or x == 'h':
    if x == 'l':
        numH = num - 1
        num = random.randint(numL, numH)
        print num
        x = raw_input('Enter high, low or number 2: ')
    if x == 'h':
        numL = num + 1
        num = random.randint(numL, numH)
        print num
        x = raw_input('Enter high, low or number 2: ')  
    else:
        print 'your number is ', num

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM