允许用户从python列表还原已删除的项目

允许用户从python列表还原已删除的项目,python,arrays,list,enumeration,Python,Arrays,List,Enumeration,我正在编写一个程序,允许用户解码从文本文件导入的单词(编码的足球队)。另一个文本文件包含解码的足球队。用户将被允许在解码单词时进行猜测,并在单词中选择要替换的字母,直到他正确地猜到它们为止(然后游戏结束) 多亏了我在这里得到的一些帮助,我能够修改一些代码,允许我记录用户通过枚举将新旧字母附加到索引列表中所做的每个字符交换 我需要用户能够选择删除以前的字符交换,这就是我在这一分钟下跌。我知道如何撤消以前的更改(多亏了这里的一些帮助),但我希望用户能够一次性查看列出的以前的交换,然后选择一个字母恢复

我正在编写一个程序,允许用户解码从文本文件导入的单词(编码的足球队)。另一个文本文件包含解码的足球队。用户将被允许在解码单词时进行猜测,并在单词中选择要替换的字母,直到他正确地猜到它们为止(然后游戏结束)

多亏了我在这里得到的一些帮助,我能够修改一些代码,允许我记录用户通过枚举将新旧字母附加到索引列表中所做的每个字符交换

我需要用户能够选择删除以前的字符交换,这就是我在这一分钟下跌。我知道如何撤消以前的更改(多亏了这里的一些帮助),但我希望用户能够一次性查看列出的以前的交换,然后选择一个字母恢复到解码字母中的原始位置。以下是迄今为止代码的主要功能:

def play():

    global encoded
    global plaintext

    x = 40
    for i in range(x):#give the user 40 goes maxiumum



        print("\nThe encoded team is {0}\n".format("".join(encoded)))
        choose = input("What character would you like to replace?")

        indices = []
        for i, j in enumerate(encoded):
            if j == choose:
                indices.append(i)         

        replace = input("What character would you like to replace it with")

        for i in indices:
            encoded[i] = replace
        changes.append((choose, replace, indices))

        for choose, replace, indices in changes:
            print("Replaced '{0}' with '{1}'".format(choose, replace))
            undo = input("Would you like to undo any changes - type 'undo'? ").lower()
            if undo == "undo":
                print("Here are the previous letters you have swapped ")
                    for i , j in enumerate (changes):
                        for c in changes:
                             for i in indices:
                                 print(choose, replace)
以下是我的文本文件调用和列表定义:

with open("football.txt","r") as file_one:
    Encoded_Team = file_one.read()

with open("football_Solved.txt","r") as file_two:
    Team = file_two.read()


encoded = list(Encoded_Team)
plaintext = list(Team)
changes = []


print("\nThe encoded team is {0}\n".format("".join(encoded)))
print("You will have 15 goes maxium")
menu()
这是菜单:

def menu():

        play_game = print("1. Play the game")
        instruc = print("2. Instructions")
        question = input("Enter choice")

        if question == "2":
                print("You will given a list of coded words, you have to replace the     symbols to letters to get the word")
                print("\n")
                menu()
        else:
                play()
我正试图从main函数中开发以下代码,这样用户就可以选择要撤消的前一个字符交换-我知道我需要枚举,但我无法将其拼凑在一起。有什么想法吗

  undo = input("Would you like to undo any changes - type 'undo'? ").lower()
      if undo == "undo":
           print("Here are the previous letters you have swapped ")
               for i , j in enumerate (changes):
                   for c in changes:
                          for i in indices:
                               print(choose, replace)
我添加了以下代码以显示以前选择的配对:

 if undo == "undo":

                for index, change in enumerate(changes):
                        chosen, replaced, indices = change
                        pairs.append(change)

                for change in pairs:
                        print(chosen, replaced)
但它不会显示最后两个配对,而是会显示最后一个配对两次

What character would you like to replace?M
What character would you like to replace it withL
Replaced 'M' with 'L'
Would you like to undo any changes - type 'undo'? 

The encoded team is Ljwfsqppm

What character would you like to replace?j
What character would you like to replace it withi
Replaced 'M' with 'L'
Would you like to undo any changes - type 'undo'? undo
j i
j i
Replaced 'j' with 'i'
Would you like to undo any changes - type 'undo'? 

有什么想法吗?

我发现您多次使用变量
I
,因此将使用最接近的变量(索引中的I),而更改中的
I
将永远不会使用。谢谢。我修正了变量。我的主要问题是打印掉以前交换的字符,并允许用户选择一个要删除的字符。您是否尝试过在任何地方输入
print
,以查看实际迭代的内容<代码>对于枚举中的i,j(更改):对于更改中的c:似乎特别可疑;你期望它做什么?也许可以将其分离为一个函数
def undo(encoded,changes)
,以简化独立开发和测试。是的,我正在考虑这个问题。我会试一试的。我认为我需要枚举更改,以便显示每对交换的字符。乔恩,如果你不介意我问的话,你会如何开始这段代码?我猜类似于
的索引,枚举中的更改(更改):选择,替换,索引=更改