python - while 循环中的变量不重新分配
python - Variables within while loop do not reassign
我和我的朋友正在使用 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
(对我创建的 class 实例的引用)没有相应更改.
这个问题不仅会影响无限循环,而且如果没有 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!"
无论我怎么调整,我都无法换房间。我相信这个问题源于与前面的代码片段相同的问题。如果是这种情况,我应该在我的 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
当place
改变时description
改变
未解决的问题:
- 我无法访问任何三级房间,即 111 或 112,即使我为那些
if
语句输入了适当的命令
- 我无法返回房间。也就是说,即使我输入了
if
语句 中列出的命令,我也无法通过 place == place[:-1]
返回
这是向前迈出的一步!但是,我无法说出首要问题是什么。有时变量会在 while 循环中重新分配,但有时则不会。
谢谢@chris-sc 的帮助!我会继续调试!
我稍微修改了您的代码,以作为 python3 工作。我刚刚注释掉了 player class 用法和 load() 函数。此外,我添加了 loadchoice 变量的初始化。通过这些修改,代码运行得很好。
loadchoice=0
while loadchoice == 0:
savefile = input('Would you like to load a savefile? Y/N ')
if savefile.lower() == 'y':
which = input("1, 2, or 3?")
if which in'123':
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 变量必须在 load() 函数内部或在更改 player.location 属性 时更改。那就去那里看看吧。
这是一种常见的调试代码的方法,只需减少带有注释的代码,直到它可以工作,然后逐步检查注释代码部分,直到它再次中断。这将指出您的错误所在。
在您更新的示例中,您有一些逻辑错误。让我们开始 11
:
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' # <<<<<===== CARE
if act.lower() == 'northwest' or act.lower == 'nw':
place == '112' # <<<<<===== CARE
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!"
在这里,您有时会混合使用 ==
(比较)和 =
(赋值)。看看我在您的代码段中用 # <<<<<===== CARE
标记的行。
类似的,在房间111
和112
你再次做一个比较,而不是你想做的新房间号的分配。
while place == '111':
description = "NORTHEAST"
print description
act = raw_input(">")
if act.lower() == 'southwest' or act.lower == 'sw':
place == place[:-1] # <<<<<===== CARE
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] # <<<<<===== CARE
elif act.lower() not in possible_actions and act.lower() in possible_directions:
print "That's not a way you can go!"
一般来说,调试此类问题时减少冗余代码是个好主意。
在您的情况下,不同房间(或地点)背后的逻辑是相同的,那么为什么不先测试一条可能的路线(从 1 到 11 到 111)。如果此路由按预期工作,您可以添加分支。
有了这个,您可以限制可能出现错误的地方,也可以更容易地看到解释器不会向您显示的那些逻辑错误。
我和我的朋友正在使用 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
(对我创建的 class 实例的引用)没有相应更改.
这个问题不仅会影响无限循环,而且如果没有 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!"
无论我怎么调整,我都无法换房间。我相信这个问题源于与前面的代码片段相同的问题。如果是这种情况,我应该在我的 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 的帮助!我会继续调试!
我稍微修改了您的代码,以作为 python3 工作。我刚刚注释掉了 player class 用法和 load() 函数。此外,我添加了 loadchoice 变量的初始化。通过这些修改,代码运行得很好。
loadchoice=0
while loadchoice == 0:
savefile = input('Would you like to load a savefile? Y/N ')
if savefile.lower() == 'y':
which = input("1, 2, or 3?")
if which in'123':
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 变量必须在 load() 函数内部或在更改 player.location 属性 时更改。那就去那里看看吧。
这是一种常见的调试代码的方法,只需减少带有注释的代码,直到它可以工作,然后逐步检查注释代码部分,直到它再次中断。这将指出您的错误所在。
在您更新的示例中,您有一些逻辑错误。让我们开始 11
:
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' # <<<<<===== CARE
if act.lower() == 'northwest' or act.lower == 'nw':
place == '112' # <<<<<===== CARE
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!"
在这里,您有时会混合使用 ==
(比较)和 =
(赋值)。看看我在您的代码段中用 # <<<<<===== CARE
标记的行。
类似的,在房间111
和112
你再次做一个比较,而不是你想做的新房间号的分配。
while place == '111':
description = "NORTHEAST"
print description
act = raw_input(">")
if act.lower() == 'southwest' or act.lower == 'sw':
place == place[:-1] # <<<<<===== CARE
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] # <<<<<===== CARE
elif act.lower() not in possible_actions and act.lower() in possible_directions:
print "That's not a way you can go!"
一般来说,调试此类问题时减少冗余代码是个好主意。
在您的情况下,不同房间(或地点)背后的逻辑是相同的,那么为什么不先测试一条可能的路线(从 1 到 11 到 111)。如果此路由按预期工作,您可以添加分支。
有了这个,您可以限制可能出现错误的地方,也可以更容易地看到解释器不会向您显示的那些逻辑错误。