猜测列表Python

时间:2011-09-01 00:18:00

标签: python arrays list random easygui

我只是在学习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")

3 个答案:

答案 0 :(得分:3)

我猜你希望responseList包含所有用户响应的列表。你没有写它。 :)

您需要在开始时将responseList设置为空列表,而不是append每个新响应。

responseList = [user_response]每次只将其设置为单元素列表。显然,你最终会得到一个只包含最后一个响应的单元素列表。

答案 1 :(得分: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)

responseString的那一行是List Comprehension,您可以阅读或在此处询问。

答案 2 :(得分:1)

EasyGUI不是标准Python发行版的一部分。您可以在SourceForge http://easygui.sourceforge.net/下载它。它在第一次尝试时安装到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中的数字以获得奖励。 ;)