简体   繁体   English

Python-卡在猜谜游戏的程序上了吗?

[英]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. 您正在设置ans = start ,这就是错误。 Since you want to solve this yourself, I will not further explain things. 由于您想自己解决这个问题,因此我将不做进一步解释。 This is what causes your program to never decrease below 25: 这就是导致您的程序永不减少到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. 首先,您永远不会在循环内更改ans You probably want to have ans contain the number that is the current guess. 您可能希望让ans包含当前猜测的数字。 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. 因此,在循环的开头移动ans = (start + end) / 2行。

The second problem, is that you set end = end - ans when the guess was too high. 第二个问题是,您将end = end - ans设置end = end - ans当猜测值太高时end = end - ans 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; 如果要在[start, end]范围内进行搜索,则应将end设置为仍可进行猜测的最高数字; that would be ans - 1 when you guessed too high. 那将是ans - 1当您猜得太高时为ans - 1

Lastly, you probably want to catch the situation where start == end . 最后,您可能想抓住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. 例如,您可以在循环的顶部放置一个print(start, end) ,以查看每次猜测时要查看的范围。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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