【问题标题】:List of guesses Python猜测列表 Python
【发布时间】:2011-11-08 01:25:20
【问题描述】:

我只是在学习 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 包含什么?

标签: python arrays list random easygui


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    while guess != secret and tries &lt; 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,您可以在此处阅读或询问。

    【讨论】:

      【解决方案3】:

      EasyGUI 不是标准 Python 发行版的一部分。您可以在此处http://easygui.sourceforge.net/ 从 SourceForge 下载它。它在第一次尝试时安装到 Python(x,y) 安装中,仅使用“setup.py install”。要使您的列表按预期运行,请尝试以下版本:

      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 中的数字以获得奖励。 ;)

      【讨论】:

      • 您可能想提一下,easygui 不是标准库的一部分以及从哪里获取它
      • 好点。我花了大约 10 秒才找到,但也许下一个人可以在 2 内找到它。
      猜你喜欢
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多