2011-09-01 57 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秒内找到它。 –