我只是在学习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")
我猜你想让responseList包含所有用户响应的列表。不是你写的。:)
您需要在开始时将
responseList
设置为空列表,然后append
对其进行每个新的响应。在responseList = [user_response]
每次只将其设置为一个元素列表。显然,您将得到一个只有最后一个响应的元素列表。在EasyGUI不是标准Python发行版的一部分。您可以从SourceForge下载它http://easygui.sourceforge.net/。只在第一次安装时安装到Python(y)设置.py安装”。若要使列表按预期运行,请尝试以下版本:
将responseList初始化为循环外的列表,然后在执行操作时将每个数字追加到它。我加了一些逗号来分隔你在msgbox中的号码作为奖励。;)
在})。然后在while循环之外,用
while guess != secret and tries < 5:
循环之前初始化responseList
。在循环中,您可以将append
元组转换为包含猜测的responseList
,如果它太高或太低(使用一个变量,比如where
,来存储值'HIGH'
或{easygui.msgbox
显示格式化结果:带有
responseString
的那一行是List Comprehension,您可以在这里阅读或询问。在相关问题 更多 >
编程相关推荐