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

responseListにすべてのユーザーの応答のリストを含める必要があると思います。あなたはそれを書きませんでした。:)

responseList最初に空のリストに設定する必要がありappend、それに対する新しい応答ごとに設定する必要があります。

responseList = [user_response]毎回1要素リストに設定するだけです。明らかに、最後の応答だけを含む1つの要素のリストになります。

于 2011-09-01T00:44:20.097 に答える
1

ループresponseListの前に初期化します。ループでは、推測を含むようにタプルwhile guess != secret and tries < 5:できます。また、推測が高すぎたり低すぎたりした場合は ( などの変数を使用して、値またはを格納します)。次に、while ループの外側で、フォーマットされた結果を次のように表示します。appendresponseListwhere'HIGH''LOW'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 ディストリビューションには含まれていません。SourceForge のhttp://easygui.sourceforge.net/からダウンロードできます。「setup.py install」だけで、最初の試行でPython(x、y)インストールにインストールされました。リストを期待どおりに動作させるには、次のバージョンを試してください。

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 をリストとして初期化し、各番号をそれに追加します。おまけとして、メッセージボックスで番号を区切るためにカンマを追加しました。;)

于 2011-09-01T01:32:37.623 に答える