Python 中的猜测列表
我刚开始学习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")
3 个回答
在 while guess != secret and tries < 5:
这个循环之前,先初始化一下 responseList
。在这个循环里,你可以往 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
的代码是一个 列表推导式,你可以去了解一下,或者在这里问相关问题。
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 初始化为一个列表,然后在循环中把每个数字添加到这个列表里。我还加了一些逗号来分隔你的数字,这样在消息框中显示会更好看哦。;)
我猜你是想让 responseList
里包含所有用户的回复,但你没有这样做。 :)
你需要在开始的时候把 responseList
设为空列表,然后每次新回复进来时用 append
方法把它加进去。
responseList = [user_response]
这样写每次都会把它变成一个只有一个元素的列表,结果你最后只会得到一个只包含最后一次回复的列表。