1

我只是在学习 python,我写了这个,但我想展示所有的猜测,也许它们是太高还是太低。“responseList”部分是我需要帮助的地方。谢谢!

    import random, easygui

    secret = random.randint (1, 100)
    guess = 0
    tries = 0

    easygui.msgbox ("""Guess the secret number.
    It is from 1 to 99. You have five tries.  Get Guessin' !""")

    while guess != secret and tries < 5:
        user_response = guess = easygui.integerbox ("C'mon...GUESS!!! ")

        if not guess: break
        if guess <= (secret + 5) and guess > secret:
            easygui.msgbox(str(guess) + " is too HIGH... but you're close!")
        if guess >= (secret - 5) and guess < secret:
            easygui.msgbox(str(guess) + " is too LOW... but you're close!")        
        if guess < (secret - 5):
            easygui.msgbox(str(guess) + " is too LOW... Guess higher")        
        if guess > (secret + 5):
            easygui.msgbox (str(guess) +  " is too HIGH...Guess lower")

        tries = tries + 1

        responseList = [user_response]
        easygui.msgbox (responseList)

    if guess == secret:
        easygui.msgbox ("Darn!  You got it!")

    else:
        easygui.msgbox ("Ha, Ha, Ha!  No more guesses!  To the firin' squad with ya!")
        easygui.msgbox (str(secret) + " was the secret number")
4

3 回答 3

3

I'm guessing you want responseList to contain a list of all user's responses. You didn't write it. :)

You'll need to set responseList to empty list on the start and than append each new response to it.

responseList = [user_response] just sets it to one-element list every time. Obviously you'll end up with a one-element list with just the last response.

于 2011-09-01T00:44:20.097 回答
1

在循环responseList之前初始化。while guess != secret and tries < 5:在循环中,您可以append元组来responseList包含猜测,如果猜测太高或太低(使用变量,例如where,存储值'HIGH''LOW')。然后在 while 循环之外,显示格式化的结果,其中easygui.msgbox

responseList = []
while guess...:
    user_response = ...
    if not...
    if guess <=...
        where = 'HIGH'
    if guess >=...
        where = 'LOW'
    if guess <...
        where = 'LOW'
    if guess >...
        where = 'HIGH'


    tries...
    responseList.append((guess, where))

responseString = ', '.join([ '%d (%s)' % (guess, where)
                             for guess, where in responseList])
easygui.msgbox(responseString)

带有 的那一行responseStringList Comprehension,您可以阅读或在此处询问。

于 2011-09-01T01:31:19.187 回答
1

EasyGUI 不是标准 Python 发行版的一部分。您可以在http://easygui.sourceforge.net/从 SourceForge 下载它。它在第一次尝试时安装到 Python(x,y) 安装中,仅使用“setup.py install”。要使您的列表按预期运行,请尝试以下版本:

import random, easygui

secret = random.randint (1, 100)
guess = 0
tries = 0

easygui.msgbox ("""Guess the secret number.
It is from 1 to 99. You have five tries.  Get Guessin' !""")

responseList = []

while guess != secret and tries < 5:
    user_response = guess = easygui.integerbox ("C'mon...GUESS!!! ")

    if not guess: break
    if guess <= (secret + 5) and guess > secret:
        easygui.msgbox(str(guess) + " is too HIGH... but you're close!")
    if guess >= (secret - 5) and guess < secret:
        easygui.msgbox(str(guess) + " is too LOW... but you're close!")        
    if guess < (secret - 5):
        easygui.msgbox(str(guess) + " is too LOW... Guess higher")        
    if guess > (secret + 5):
        easygui.msgbox (str(guess) +  " is too HIGH...Guess lower")

    tries = tries + 1

    responseList.append(user_response)
    easygui.msgbox (",".join(["%d"%x for x in responseList]))

if guess == secret:
    easygui.msgbox ("Darn!  You got it!")

else:
    easygui.msgbox ("Ha, Ha, Ha!  No more guesses!  To the firin' squad with ya!")
    easygui.msgbox (str(secret) + " was the secret number")

将 responseList 初始化为循环外的列表,然后将每个数字附加到它上面。我添加了一些逗号来分隔 msgbox 中的数字以获得奖励。;)

于 2011-09-01T01:32:37.623 回答