2011-09-01 72 views
1

我剛學習python,我寫了這個,但我想顯示所有的猜測,也許他們是否過高或過低。 「responseList」部分是我需要幫助的地方。謝謝!猜測列表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") 
+0

那麼,你希望'responseList'包含什麼? –

回答

3

我猜你想要responseList包含所有用戶的響應列表。你沒有寫。 :)

您需要將responseList設置爲開始時的空白列表以及每個新響應的append

responseList = [user_response]只是每次都將其設置爲一元列表。很明顯,你最終會得到一個只有最後一個響應的單元列表。

1

while guess != secret and tries < 5:循環之前初始化responseList。在循環中,您可以包含猜測的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,你可以在閱讀時,或詢問有關在這裏。

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爲外循環列表,然後每個數字追加到它,當您去。我添加了一些逗號來分隔你的數字在msgbox中的獎金。 ;)

+0

你可能想提一下,easygui不是標準庫的一部分,從哪裏可以得到它 –

+0

好點。這花了我大約10秒鐘的時間才找到,但也許下一個人可以在2秒內找到它。 –