2

それで、私はこの推測ゲームの問題にしばらく取り組んできましたが、何が問題なのかを突き止めようとして過去 2 時間頭を悩ませていましたが、できませんでした。私も解決策を探してみましたが、コピー&ペーストをしたくなく、実際に自分のコードを自分で解決したいです

これが私がこれまでに得たものです:

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)

私は何を間違っていますか?編集: ゲームは次のように実行する必要があります。

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
4

3 に答える 3

1

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?
于 2013-10-24T11:03:26.013 に答える
0

私のソリューションは機能します:

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
于 2015-01-20T02:37:38.053 に答える
0

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.

于 2013-10-24T11:03:30.283 に答える