因此,对于我的任务,我要做一个游戏,我必须做一个游戏,允许用户使用字典对编码信息进行加密。我需要程序做的是,当用户输入一个字母时,他们必须键入一个符号来进行配对,然后我需要根据字母将该配对替换到十字列表中。
这是(线索)文本文件:
A#
M*
N%
这是名为( words )的编码单词文本文件:
#+/084&"
#3*#%#+
8%203:
,1$&
!-*%
.#7&33&
#*#71%
&-&641'2
#))85
9&330*最后,它们应该是未编码(已解决)的样子:
已获得
年历
侮辱
笑话
赞美诗
小羚羊
亚马逊
眉毛
词缀
牛皮纸
这是我到目前为止的代码:
import csv
def Menu():
print("Welcome to aqa puzzle code, please choose an option below")
choice = input("""
1. To look at the clues
2. Ten word list and pairings
3. To enter pairings
4. Delete pairings
5. Check if code is right
6. Press 999 to quit """)
if choice == '1':
Clues()
elif choice == '2':
WordlistAndPairings()
elif choice == '3':
EnterPairings()
elif choice == '4':
DeletePairings()
elif choice == '5':
Solution()
elif choice == '6':
input("")
def Clues():
with open('Clues.txt','r') as f:
f = f.readlines()
for line in f:
print(f)
Menu()
def WordlistAndPairings():
print("Ten word list with substitutions made")
file = open("Words.txt","r")
openfile = file.read()
print(openfile)
file.close()
Menu()
def EnterPairings():
pairings = {'letters: [A:Z]', 'symbols: [!],[-],[$],[)],[+],["],[\'],[.],'}
letters = input("Enter letter")
if letters is in pairings:
print("You've already paired this wih another symbol")
EnterPairings()
elif letters is not in pairings:
symbols = input("Enter symbol to go with letter symbol")
pairings [letters] = symbols
print (pairings)
Menu()
def DeletePairings():
print("")
print("Removing a pairing")
rename = input("Enter the letter you want to remove")
print(pairings)
del pairings[rename]
print(pairings)
Menu()
Menu()对于第二个选项(2),我不需要,我需要它来刷新它自己,因为所做的更改。因此,如果用户要进行(F')的配对,我需要程序返回菜单,如果用户选择(2),我基本上需要用用户的字母替换符号。此外,在该列表下,我想要一个由用户进行的所有替换的列表,如果他们删除了配对,那么如果他们要返回到列表,则需要更新。我将非常感谢任何能帮助我解决这个问题的人。
发布于 2014-12-10 08:50:40
我不认为我完全理解你的问题,我也不想只为你做作业,但这里有一个例子,说明如何编写一个函数,根据字典中的配对替换字符串中的字符:
pairings = {'#':'A',
'%':'N',
'*':'M',
}
def try_substitution(coded_word, pairings):
# Create an empty list to hold the decoded letters
decoded_letters = []
# Loop over each symbol in the word
for symbol in coded_word:
# 'try' is a Python keyword for catching 'exceptions',
# which are errors that can happen in your code.
try:
# Look up this symbol in the pairings dictionary
decoded_letter = pairings[symbol]
# KeyError is the exception that happens when you try
# to look up something that doesn't exist in a dictionary.
except KeyError:
# We handle this error case by just keeping the original
# coded symbol.
decoded_letters.append(symbol)
# 'else' means no exception was triggered.
else:
# So we can add the decoded letter to our list.
decoded_letters.append(decoded_letter)
# Now we join all the decoded letters together.
# this uses '' (empty string) because we don't want
# anything separating these letters.
decoded_word = ''.join(decoded_letters)
# And return our completed word from the function
return decoded_word上面定义了一个基于“线索”文件将符号映射到字母的字典和一个接受两个参数的函数:一个编码的单词和一个替换字典。
如果您对其中一个编码单词调用该函数:
>>> try_substitution('#3*#%#+', pairings)
'A3MANA+'您将得到您的单词的部分解码版本。
编写上述函数的一种更短、更花哨的方法是使用Python's generator expressions,但这是一个更高级的功能,如果您只是学习编程或只是学习Python语言,则不太容易理解:
def try_substitution(coded_word, pairings):
return ''.join(pairings.get(symbol, symbol) for symbol in coded_word)我认为如果你能理解第一个例子,并对你的代码做一些修改,你应该能够得到一个可以工作的程序。
https://stackoverflow.com/questions/27391123
复制相似问题