Python 2.7 当字符串不应为[Python]时被修改的问题

Python 2.7 当字符串不应为[Python]时被修改的问题,python-2.7,Python 2.7,我一直在为一个爱好写一个游戏/把它作为最后一个项目提交,很酷的东西。但我最近遇到了一个令人难以置信的问题,我不知道它是如何发生的,也不知道为什么会发生 这种方法是为游戏绘制一个方框,但每当它运行控制台输出时,似乎会向脚本添加越来越多的角色。我试图调试它,但到目前为止,我不是一个很好的调试器 def drawModule(selections, selected): '''Draws a text box based on the inputs given''' str1 = "" str2 =

我一直在为一个爱好写一个游戏/把它作为最后一个项目提交,很酷的东西。但我最近遇到了一个令人难以置信的问题,我不知道它是如何发生的,也不知道为什么会发生

这种方法是为游戏绘制一个方框,但每当它运行控制台输出时,似乎会向脚本添加越来越多的角色。我试图调试它,但到目前为止,我不是一个很好的调试器

def drawModule(selections, selected):
'''Draws a text box based on the inputs given'''
str1 = ""
str2 = ""
str3 = "  "
str4 = ""
str5 = ""
modList = selections
print id(modList)
print id(selections)
for i in range(len(modList)):
    if i == selected:
        modList[i] = "| " + modList[i] + " |  "
    else:
        modList[i] = "# " + modList[i] + " #  "
    str3 += modList[i]
print selections
print modList
for i in range(len(modList)):
    str1 += "  "
    str2 += "  "
    str4 += "  "
    str5 += "  "
    for letter in range(len(modList[i]) - 2):
        if letter == 0 or letter == (len(modList[i]) - 3):
            if i == selected:
                str1 += "|"
                str2 += "|"
                str4 += "|"
                str5 += "|"
            else:
                str1 += "#"
                str2 += "#"
                str4 += "#"
                str5 += "#"
        else:
            if i == selected:
                str1 += "-"
                str2 += " "
                str4 += " "
                str5 += "-"
            else:
                str1 += "#"
                str2 += " "
                str4 += " "
                str5 += "#"
sys.stdout.write(str1 + "\n" + str2 + "\n" + str3 + "\n" + str4 + "\n" + str5)
调用此方法的脚本用于我游戏中战斗系统的开始。这是一个像艾萨克一样的流氓,就像他用ASCII文本做的一样,这并不重要

UNFINISHED
def combat():
    '''Runs combat seperately from the selection menu'''
    inCombat = True
    print "You've started combat!"
    time.sleep(1)
    options = ["attack", "guard", "inventory", "flee"]
    position = 0
    clear()
    while inCombat:
        if msvcrt.kbhit():
            clear()
            key = msvcrt.getch()
            print options
            if ord(key) == 100:
                position +=1
                drawModule(options, position)
            elif ord(key) == 97:
                position -= 1
                drawModule(options, position)
            elif ord(key) == 27:
                break
我还在为这件事发疯,任何解决办法都会令人惊讶。另外,我不擅长这个,所以如果你发现我可以做得更好,我想知道更多

多次运行时的控制台输出:

138650760
138650760
['# attack #  ', '| guard |  ', '# items #  ', '# flee #  ']
['# attack #  ', '| guard |  ', '# items #  ', '# flee #  ']
  ##########  |-------|  #########  ########
  #        #  |       |  #       #  #      #
  # attack #  | guard |  # items #  # flee #  
  #        #  |       |  #       #  #      #
  ##########  |-------|  #########  ########
-------------------------Second Time----------------------
138650760
138650760
['# # attack #   #  ', '| | guard |   |  ', '# # items #   #  ', '# # flee #   #  ']
['# # attack #   #  ', '| | guard |   |  ', '# # items #   #  ', '# # flee #   #  ']
  ################  |-------------|  ###############  ##############
  #              #  |             |  #             #  #            #
  # # attack #   #  | | guard |   |  # # items #   #  # # flee #   #  
  #              #  |             |  #             #  #            #
  ################  |-------------|  ###############  ##############
这一趋势持续了一段时间。

在第一线:

modList = selections
您已将两个列表等同起来。那么后来呢,

    modList[i] = "| " + modList[i] + " |  "

实际上是在修改
选择
修改列表

是因为我在代码的前面将列表彼此相等,才导致了问题。我使用复制模块中的deepcopy功能来解决我的问题。

可能与See重复,奇怪的是它实际上编辑了战斗功能中的列表选项。这是同一类型的事情吗?请看一下,两天后再来,并根据回答进行检查。