python - while循环中的变量不会重新分配

时间:2015-08-12 06:46:07

标签: python python-2.7 loops while-loop infinite-loop

我的朋友和我正在使用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

然而,当我运行它并尝试输入文件时,我得到一个无限循环。看起来我可以加载文件并开始新游戏,但是,我不认为变量loadchoiceplayer.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!"

无论我如何调整,我都无法更换房间。我认为这个问题源于前一段代码所具有的相同问题。如果是这种情况,我应该在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
  • {li> descriptionplace更改时发生变化

未解决的问题:

  • 即使我为那些if语句输入适当的命令,我也无法访问任何三级房间,即111或112
  • 我无法回到房间。也就是说,即使输入place == place[:-1]语句中列出的命令,我也无法通过if返回

这是向前迈出的一步!但是,我不知道最重要的问题是什么。有时变量会在while循环中重新分配,但有时则不会。 谢谢@ chris-sc为您提供帮助!我会继续调试!

2 个答案:

答案 0 :(得分:0)

我稍微修改了你的代码,以python3的形式工作。我只是注释掉了播放器类的用法和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

这使我们有了这样的假设:必须在load() - 函数内部或更改player.location属性时更改loadchoice-variable。所以继续看看那里。

这是调试代码的常用方法,只需使用注释减少代码直到它工作,并逐步检查注释的代码部分,直到它再次中断。这将指出你的错误所在。

答案 1 :(得分:0)

在更新的示例中,您有一些逻辑错误。让我们进行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标记的行。

类似地,在房间111112中,您再次进行比较,而不是分配您想要的新房间号。

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)。如果此路由按预期工作,则可以添加分支。

通过这种方式,您可以限制错误的可能位置,并且还可以更容易地看到解释器不会向您显示的逻辑错误。