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

最佳答案

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

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

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

08-18 11:59