【发布时间】:2015-11-04 15:03:12
【问题描述】:
我和我的朋友正在使用 Python 2.7 创建一个简单的基于文本的游戏。
为了开始游戏,我创建了一个系统,其中包含三个可用的保存文件(例如 save1.txt),其中包含不同游戏状态的信息(例如位置、库存)。要加载这个文件,我首先希望用户决定加载一个有效的文件(否则他们将开始一个新游戏)然后加载文件。在任一选项之后,提示的循环应该停止。
while loadchoice == 0:
savefile = raw_input('Would you like to load a savefile? Y/N ')
if savefile.lower() == 'y':
which = raw_input("1, 2, or 3?")
if which == '1' or which == '2' or which == '3':
loadchoice = 1
load(which)
print "You awaken"
else:
loadchoice = 0
if savefile.lower() == 'n':
loadchoice = 1
print "You have started a new game."
player.location = 0
但是,当我运行它并尝试输入文件时,我得到一个无限循环。看起来我可以加载文件并开始一个新游戏,但是,我认为变量 loadchoice 和 player.location(对我创建的类实例的引用)没有相应更改。
这个问题不仅会影响无限循环,而且如果没有player.location,我以后在游戏中将无法切换区域。
例如,这是三个开始房间的代码:
#Every time the room level branches out, a new digit is added to player.location
while player.location != '-1':
while player.location == '0':
print "You awaken."
print "Press Enter to continue"
print "You must find them."
print "\n"
print "\n"
player.location = '1'
#1s
while player.location == '1':
act = raw_input(">")
options()
roomitems = []
#Paths
if act.lower() == 'n' or act.lower() == 'north':
player.location = '11'
if act.lower() == 's' or act.lower() == 'south':
player.location = '12'
elif act.lower() not in possible_actions and act.lower() in possible_directions:
print "That's not a way you can go!"
#10s
while player.location == '11':
act = raw_input(">")
options()
roomitems = []
#Paths
if act.lower() == 'northeast' or act.lower == 'ne':
player.location == '111'
if act.lower() == 'northwest' or act.lower == 'nw':
player.location == '112'
if act.lower() == 'south' or act.lower == 's':
back()
elif act.lower() not in possible_actions and act.lower() in possible_directions:
print "That's not a way you can go!"
无论我如何调整它,我都无法更换房间。我相信这个问题与之前的sn-p代码有同样的问题。如果是这种情况,我应该在我的 while 循环中修复什么?如果这是两个不同的错误,我可以就这两个错误提供建议吗?
感谢您提供的任何帮助!
按照@chris-sc 的建议,我为更改房间的代码创建了以下测试用例:
------------------------ 第一次尝试解决问题 ----- ----
place = '0'
description = ' '
possible_actions = []
possible_directions = ['n', 'north', 's', 'south', 'e', 'east', 'w', 'west', 'ne', 'northeast', 'se', 'southeast', 'nw', 'northwest', 'sw', 'southwest', 'd', 'down', 'u' 'up']
while place != '-1': #The overarching condition that continues the game
while place == '0':
print "The has started."
place = '1'
#1s
while place == '1':
act = raw_input(">")
desciption = "FIRST ROOM"
print desciption
#Paths
if act.lower() == 'n' or act.lower() == 'north':
place = '11'
if act.lower() == 's' or act.lower() == 'south':
place = '12'
elif act.lower() not in possible_actions and act.lower() in possible_directions:
print "That's not a way you can go!"
#10s - down any path from the first room. These are all the possible 2nd rooms, hence two digits.
while place == '11': #You went North
act = raw_input(">")
description = "YOU WENT NORTH"
print description
#Paths - the ways you can go from this room
if act.lower() == 'northeast' or act.lower == 'ne':
place == '111' #A third room
if act.lower() == 'northwest' or act.lower == 'nw':
place == '112' #Another possibile choice for a third room
if act.lower() == 'south' or act.lower == 's':
place = place[:-1] #Going back up the path by removing the last digit
elif act.lower() not in possible_actions and act.lower() in possible_directions:
print "That's not a way you can go!"
while place == '12': #You went South
act = raw_input(">")
description = "YOU WENT SOUTH"
print description
#Paths
if act.lower() == 'north' or act.lower == 'n':
place = place[:-1] #Going back up the path by removing the last digit
elif act.lower() not in possible_actions and act.lower() in possible_directions:
print "That's not a way you can go!"
#100s
while place == '111':
description = "NORTHEAST"
print description
act = raw_input(">")
if act.lower() == 'southwest' or act.lower == 'sw':
place == place[:-1]
elif act.lower() not in possible_actions and act.lower() in possible_directions:
print "That's not a way you can go!"
while place == '112':
description = "NORTHWEST"
print description
act = raw_input(">")
if act.lower() == 'southeast' or act.lower == 'se':
place == place[:-1]
elif act.lower() not in possible_actions and act.lower() in possible_directions:
print "That's not a way you can go!"
这次尝试的成功:
- 不会导致无限循环
-
place从 1 变为 11,从 1 变为 12 -
description随place变化而变化
未解决的问题:
- 我无法访问任何三级房间,即 111 或 112,即使我为这些
if语句输入了适当的命令 - 我无法返回房间。也就是说,即使我输入了
if语句中列出的命令,我也无法通过place == place[:-1]返回
这是一个进步!但是,我无法说出最重要的问题是什么。有时变量会在 while 循环中重新分配,但有时则不会。 谢谢@chris-sc 的帮助!我会继续调试的!
【问题讨论】:
-
调试
while循环的直接方法是添加print语句并检查输出(如果您按预期进入/退出循环)。如果没有关于类的详细信息(无论如何可能太复杂而无法显示),就很难分析您的问题。您可以尝试将其分解为一些测试用例吗? (这也将帮助您进行调试) -
这是一个很棒的建议!我不确定如何将加载函数更改为测试用例,因此我编写了一个更简单的房间更改 while 循环版本,并将结果作为我编辑的帖子的一部分发布。 .
标签: python python-2.7 loops while-loop infinite-loop