Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python-while循环中的变量不重新分配_Python_Python 2.7_Loops_While Loop_Infinite Loop - Fatal编程技术网

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

python-while循环中的变量不重新分配,python,python-2.7,loops,while-loop,infinite-loop,Python,Python 2.7,Loops,While Loop,Infinite Loop,我的朋友和我正在使用Python2.7创建一个简单的基于文本的游戏 为了启动游戏,我创建了一个系统,其中有三个保存文件(例如save1.txt),其中包含不同游戏状态(即位置、库存)的信息。要加载这个文件,我首先希望用户决定加载一个有效的文件(否则他们将启动一个新游戏),然后加载文件。在任何一个选项之后,提示的循环都应该停止 while loadchoice == 0: savefile = raw_input('Would you like to load a savefile? Y/

我的朋友和我正在使用Python2.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!"
不管我怎么调整,我都不能换房间。我相信这个问题与前面代码片段的问题是一样的。如果是这种情况,我应该在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!"
这一尝试的成功:

  • 不会导致无限循环
  • 位置
    从1更改为11,从1更改为12
  • description
    place
    更改时更改
未解决的问题:

  • 即使我为那些
    if
    语句输入了相应的命令,我也无法访问任何三级房间,即111或112
  • 我不能回房间了。也就是说,即使我输入了
    if
    语句中列出的命令,我也无法通过
    place==place[:-1]
    返回
这是向前迈出的一步!然而,我不知道最重要的问题是什么。有时变量会在while循环中重新分配,但有时不会。
谢谢@chris sc对您的帮助!我会继续调试

我对您的代码做了一些修改,使其可以作为python3使用。我刚刚注释掉了player类的用法和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!"
一般来说,在调试此类问题时减少冗余代码是一个好主意

在您的案例中,不同房间(或位置)背后的逻辑是相同的,因此为什么不首先测试一条可能的路线(从1到11到111)。如果此管线按预期工作,则可以添加分支


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

循环时调试
的一种直接方法是添加
打印
语句并检查输出(如果您按照预期进入/退出循环)。如果没有关于类的详细信息(可能太复杂,无论如何都无法显示),就很难分析您的问题。你能试着把它分解成一些测试用例吗?(这也将帮助您进行调试)这是一条非常棒的建议!我不知道如何将load函数更改为testcase,所以我编写了一个更简单的room changing while循环版本,结果作为我编辑的文章的一部分发布。非常感谢。我看看我能从那里做些什么!哦,哇,谢谢你指出那些台词!我查看了游戏的原始代码,犯了相同的错误,因此解决了一个问题。对于其他两个问题,我将遵循您的建议,保持调试测试代码的简单性并缓慢构建,直到找到问题所在。非常感谢你的帮助!我得到了工作的初始保存。现在,如果用户输入'n'或'n',他们通过进入while循环开始一个新游戏。如果他们的答案没有意义,那么它会再次提示。如果他们的答案是“y”或“y”,那么他们必须给出一个“1”、“2”或“3”的数字。此号码已记录。从那里,我可以加载号码并读取保存文件,以便加载适当的数据并根据需要更改
player.location
。然而,这就是问题所在。虽然变量已更改,但我没有被带到嵌套while循环中循环所在的正确位置:我不确定这是否是由于最小的示例,但请检查代码中是否混合了
player.location
place
。无论如何,我建议提出一个新问题,因为这似乎是一个不同的问题。在新问题中,尝试添加相关代码(尽可能紧凑);我需要练习。谢谢你的帮助!
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!"