开发者

List of guesses Python

I am just learning python and I wrote this, but I want to show all the guesses and maybe whether they are too high or low. The "responseList" part is where I need help. Thanks!

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


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.


Initialize responseList before the while guess != secret and tries < 5: loop. In the loop, you can append tuples to responseList containing the guess, and if it was too high or low (use a variable, say where, to store the value 'HIGH' or 'LOW'). Then outside the while loop, show the formatted results, with 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)

that line with the responseString is a List Comprehension, which you can read up on, or ask about here.


EasyGUI is not part of the standard Python distribution. You can download it from SourceForge here http://easygui.sourceforge.net/. It installed into a Python(x,y) installation on the first try with only "setup.py install". To get your list to behave as you expect, try this version:

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

initialize responseList as a list outside the loop, then append each number to it as you go. I added some commas to separate your numbers in the msgbox for a bonus. ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜