【问题标题】:python - Variables within while loop do not reassignpython - while循环中的变量不会重新分配
【发布时间】: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

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

无论我如何调整它,我都无法更换房间。我相信这个问题与之前的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
  • descriptionplace 变化而变化

未解决的问题:

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

这是一个进步!但是,我无法说出最重要的问题是什么。有时变量会在 while 循环中重新分配,但有时则不会。 谢谢@chris-sc 的帮助!我会继续调试的!

【问题讨论】:

  • 调试while 循环的直接方法是添加print 语句并检查输出(如果您按预期进入/退出循环)。如果没有关于类的详细信息(无论如何可能太复杂而无法显示),就很难分析您的问题。您可以尝试将其分解为一些测试用例吗? (这也将帮助您进行调试)
  • 这是一个很棒的建议!我不确定如何将加载函数更改为测试用例,因此我编写了一个更简单的房间更改 while 循环版本,并将结果作为我编辑的帖子的一部分发布。 .

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


【解决方案1】:

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

在这里,您有时会混合使用==(比较)和=(分配)。看看我在你的 sn-p 中用# &lt;&lt;&lt;&lt;&lt;===== 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)。如果这条路线按预期工作,您可以添加分支。

这样,您可以限制可能出现错误的位置,也可以更轻松地查看解释器不会向您显示的那些逻辑错误。

【讨论】:

  • 哦,哇,感谢您指出这些行!我查看了游戏的原始代码,我也犯了同样的错误,这样就解决了一个问题。对于其他几个问题,我将听从您的建议,并保持我的调试测试代码简单并缓慢构建,直到找到问题为止。非常感谢您的帮助!
  • 我得到了初始保存。现在,如果用户输入“n”或“N”,他们会通过进入 while 循环开始新游戏。如果他们的回答没有意义,那么它会再次提示。如果他们的答案是“y”或“Y”,那么他们必须给出一个“1”、“2”或“3”的数字。此号码已记录。
  • 从那里,我可以加载号码并读取保存文件,以便加载适当的数据并根据需要更改 player.location。然而,这就是问题所在。虽然变量已经改变,但我没有被带到循环所在的嵌套 while 循环中的正确位置:some room number 代码>。我认为这可能是同一个问题,它不允许我重新走上正轨。你有什么建议吗? @chris-sc
  • 我不确定这是否是由于最小的示例,但请检查您是否在代码中混合了 player.locationplace。无论如何,我建议提出一个新问题,因为这似乎是一个不同的问题。在新问题中,尝试添加相关代码(尽可能紧凑)。
  • 我想我会继续自己调试;我需要练习。感谢您的所有帮助!
【解决方案2】:

我稍微修改了您的代码,以作为 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 属性时更改。所以去那里看看吧。

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

【讨论】:

  • 谢谢!我会看看我能从那里做些什么!
猜你喜欢
  • 2011-10-01
  • 2013-04-07
  • 2019-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 1970-01-01
相关资源
最近更新 更多