猜测列表Python

2024-09-22 20:35:09 发布

您现在位置:Python中文网/ 问答频道 /正文

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

    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")

Tags: andthenumbersecretifisrandomtoo
3条回答

我猜你想让responseList包含所有用户响应的列表。不是你写的。:)

您需要在开始时将responseList设置为空列表,然后append对其进行每个新的响应。在

responseList = [user_response]每次只将其设置为一个元素列表。显然,您将得到一个只有最后一个响应的元素列表。在

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

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中的号码作为奖励。;)

while guess != secret and tries < 5:循环之前初始化responseList。在循环中,您可以将append元组转换为包含猜测的responseList,如果它太高或太低(使用一个变量,比如where,来存储值'HIGH'或{})。然后在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,您可以在这里阅读或询问。在

相关问题 更多 >