在python中设计猜测数字列表的游戏时遇到的问题
今天我想设计一个游戏来猜我设定的数字列表。玩家应打印所有5个数字以赢得游戏。不允许打印重复编号。代码如下:在python中设计猜测数字列表的游戏时遇到的问题,python,python-2.x,Python,Python 2.x,今天我想设计一个游戏来猜我设定的数字列表。玩家应打印所有5个数字以赢得游戏。不允许打印重复编号。代码如下: def guess(): print "Please input a number smaller than 10, let's see if it is in my plan." print "You should figure out all the 5 numbers." n = int(raw_input('>')) my_list = [1,
def guess():
print "Please input a number smaller than 10, let's see if it is in my plan."
print "You should figure out all the 5 numbers."
n = int(raw_input('>'))
my_list = [1, 3, 5, 7, 9]
your_list = []
count = 0
while count < 5:
if n in my_list:
if n not in your_list:
your_list.append(n)
count = count + 1
print "Good job! You have got %d numbers!" % count
n = int(raw_input('>'))
else:
print "You have already typed that. Input again!"
n = int(raw_input('>'))
else:
print "That is not what I want."
n = int(raw_input('>'))
print "Here you can see my plan:", my_list
print "You are so smart to guess out, you win!"
guess()
当我输入4时,它应该打印“这不是我想要的”,而不是表示“赢”。为什么出错?您刚才在
原始输入的顺序中出现了一些逻辑错误
您在代码的最开始有一个额外的
在每个条件结束时,您有一个原始输入。最好在while循环的开头有一个raw_输入
,并根据该输入显示一条消息
根据您的逻辑,即使在您找到所有5个猜测之后,仍然有一个输入等待,并且无论该输入是什么,它都将显示win消息。因此,即使您在最后键入4
,您也会收到结束消息,因为我这边的count起作用了。唯一一点不起作用,我需要在中断while循环之前输入第6个元素。我将为您提供可能的改进和不同实现的概述
此外,您正在使用Python2。你应该考虑移动到Python 3,尤其是如果你刚开始学习它的话。
改进:
whilecount<5:
在排序时可以变成(我的列表)!=已排序(您的列表):
- 嵌套语句可以简化
- 检查一下输入就好了
实施:
def guess_2():
print ("Please input a number smaller than 10, let's see if it is in my plan.")
print ("You should figure out all the 5 numbers.")
# Parameters
my_list = [1, 3, 5, 7, 9]
your_list = []
count = 0
while sorted(my_list) != (sorted(your_list)):
# Input:
not_valid = True
while not_valid:
try:
n = int(input('>'))
not_valid = False
except:
print ("Please input a number.")
if n in my_list and not n in your_list:
your_list.append(n)
count = count + 1
print ("Good job! You have got %d numbers!" % count)
elif n in my_list and n in your_list:
print ("You have already typed that. Input again!")
else:
print ("That is not what I want.")
print ("Here you can see my plan:", my_list)
print ("You are so smart to guess out, you win!")
截至2018年7月24日,相关代码给出缩进错误。第3、5、7和8行缩进3个空格,而不是4个空格
更正后,代码将运行(在Python2.7中),但“raw_input()”位于错误的位置。在代码中:
while count < 5:
# do some stuff
# If previous guess correct, increment count
n = int(raw_input('>')) # This prompts the user again after the 5th success
另一个问题是,如果“我的清单”有一个或多个重复的数字,游戏就不可能完成
一个可能的解决办法是;不必为“您的列表”构建第二个列表,您只需从“我的列表”中删除元素,直到没有元素为止:
def guess():
print "Please input a number smaller than 10, let's see if it is in my plan."
print "You should figure out all the 5 numbers."
my_list = [1, 3, 5, 3, 9]
my_plan = str(my_list)
while len(my_list) > 0:
guess = int(raw_input('>'))
try:
int(guess)
except ValueError:
print "'%s' is not a number." % str(guess)
continue
if guess < 0 or guess > 9:
print "ONE digit 0 to 9 please."
else:
if guess in my_list:
my_list.remove(guess) # Remove ONE instance of 'guess'
# or remove ALL instances of 'guess'
# my_list = [num for num in my_list if num != guess]
print "Good job! You have got %d numbers!" % (5 - len(my_list))
else:
print "That is not what I want."
print "Here you can see my plan:", my_plan
print "You are so smart to guess out, you win!"
guess()
def guess():
打印“请输入一个小于10的数字,让我们看看它是否在我的计划中。”
打印“你应该算出所有5个数字。”
我的清单=[1,3,5,3,9]
我的计划=str(我的清单)
而len(我的列表)>0:
guess=int(原始输入('>'))
尝试:
整数(猜测)
除值错误外:
打印“'%s'不是数字。”%str(猜测)
持续
如果猜测<0或猜测>9:
打印“请输入一位数字0到9。”
其他:
如果在我的列表中猜测:
my_list.remove(guess)#删除一个'guess'实例
#或删除“猜测”的所有实例
#my_list=[num for num in my_list if num!=guess]
打印“干得好!您有%d个数字!”%(5-len(我的列表))
其他:
打印“这不是我想要的。”
打印“在这里你可以看到我的计划:”,我的计划
打印“你很聪明,能猜出来,你赢了!”
猜()
()寻求调试帮助的问题(“为什么此代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建一个最小、完整且可验证的示例。我无法复制该输出。即使当计数=5时,它也会要求一个额外的输入,但计数为4时它不会停止。我只是复制并粘贴了你的代码,当我点击4时,我收到“这不是我想要的”。所以它在python 2上对我有效。7@Carlo1585是的,我又试了一次,结果和你的一样。这很有趣。但当我尝试输入其他数字时,代码仍然出错。所以这不是Python版本的错,@AdamJaamour是的!Thx:)更正,你刚刚修复了他的代码,没有解释什么,我已经删除了,现在你已经解释了你的更改:),更好的答案now@NickA可以理解,我通常在回答^^Thx之后添加我的解释!你的代码运行得很好!现在我明白为什么我错了。THX:)我刚刚开始学习Python,我从来没有考虑过用你的方式来编码。这真的激励了我!感谢您对Python版本的建议。然而,我正在使用《艰苦地学习Python》一书,作者建议我使用Python2而不是Python3来做书中的练习。所以我不知道该怎么办…@FrancesWu我的观点是,开始学习一种(很快)被弃用的编程语言是不明智的。您可以继续使用相同的课程,但使用Python3。你将得到的错误也会告诉你在不同版本之间发生了什么变化。很抱歉缩进:(我在iPhone上发送了代码,所以出现了一些错误…谢谢你的建议!这真的很有帮助!
def guess_2():
print ("Please input a number smaller than 10, let's see if it is in my plan.")
print ("You should figure out all the 5 numbers.")
# Parameters
my_list = [1, 3, 5, 7, 9]
your_list = []
count = 0
while sorted(my_list) != (sorted(your_list)):
# Input:
not_valid = True
while not_valid:
try:
n = int(input('>'))
not_valid = False
except:
print ("Please input a number.")
if n in my_list and not n in your_list:
your_list.append(n)
count = count + 1
print ("Good job! You have got %d numbers!" % count)
elif n in my_list and n in your_list:
print ("You have already typed that. Input again!")
else:
print ("That is not what I want.")
print ("Here you can see my plan:", my_list)
print ("You are so smart to guess out, you win!")
while count < 5:
# do some stuff
# If previous guess correct, increment count
n = int(raw_input('>')) # This prompts the user again after the 5th success
while count < 5:
n = int(raw_input('>'))
# If previous guess correct, increment count
guess = raw_input('>')
try:
value = int(guess)
except ValueError:
# Handle the error
def guess():
print "Please input a number smaller than 10, let's see if it is in my plan."
print "You should figure out all the 5 numbers."
my_list = [1, 3, 5, 3, 9]
my_plan = str(my_list)
while len(my_list) > 0:
guess = int(raw_input('>'))
try:
int(guess)
except ValueError:
print "'%s' is not a number." % str(guess)
continue
if guess < 0 or guess > 9:
print "ONE digit 0 to 9 please."
else:
if guess in my_list:
my_list.remove(guess) # Remove ONE instance of 'guess'
# or remove ALL instances of 'guess'
# my_list = [num for num in my_list if num != guess]
print "Good job! You have got %d numbers!" % (5 - len(my_list))
else:
print "That is not what I want."
print "Here you can see my plan:", my_plan
print "You are so smart to guess out, you win!"
guess()