允许用户从python列表中恢复已删除的项目

时间:2014-05-08 20:18:17

标签: 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'? 

有什么想法吗?

0 个答案:

没有答案