私は学校に問題があり、それを理解できないようです。基本的には、私はオブジェクト指向のプログラミングクラスの紹介に入っているので、私はまだ学んでいないことを何も使用せずに、できるだけ基本的なものとしてこれを完了する必要があります。現在、辞書とセットについて学ぶには、長い文字列を1行に含む文書を暗号化するためのコードが書かれた辞書を使用する必要があります。特定のキーを使用したファイルの暗号化と復号化
したがって、辞書を読み、その文字列を含むテキストファイルを開くには、1つの部分が必要です。
"The course Introduction to Object Oriented Programming uses the Python programming language."
私はそれを暗号化し、encrypt.txt
と呼ばれる別のテキストファイルに文字列の暗号化されたバージョンを書くために、この辞書からコードを使用する必要があります。
CODE = {'A': ')', 'a': '0', 'B': '(', 'b': '9', 'C': '*', 'c': '8',
'D': '&', 'd': '7', 'E': '^', 'e': '6', 'F': '%', 'f': '5',
'G': '$', 'g': '4', 'H': '#', 'h': '3', 'I': '@', 'i': '2',
'J': '!', 'j': '1', 'K': 'Z', 'k': 'z', 'L': 'Y', 'l': 'y',
'M': 'X', 'm': 'x', 'N': 'W', 'n': 'w', 'O': 'V', 'o': 'v',
'P': 'U', 'p': 'u', 'Q': 'T', 'q': 't', 'R': 'S', 'r': 's',
'S': 'R', 's': 'r', 'T': 'Q', 't': 'q', 'U': 'P', 'u': 'p',
'V': 'O', 'v': 'o', 'W': 'N', 'w': 'n', 'X': 'M', 'x': 'm',
'Y': 'L', 'y': 'l', 'Z': 'K', 'z': 'k', '!': 'J', '1': 'j',
'@': 'I', '2': 'i', '#': 'H', '3': 'h', '$': 'G', '4': 'g',
'%': 'F', '5': 'f', '^': 'E', '6': 'e', '&': 'D', '7': 'd',
'*': 'C', '8': 'c', '(': 'B', '9': 'b', ')': 'A', '0': 'a',
':': ',', ',': ':', '?': '.', '.': '?', '<': '>', '>': '<',
"'": '"', '"': "'", '+': '-', '-': '+', '=': ';', ';': '=',
'{': '[', '[': '{', '}': ']', ']': '}'}
これはこれまでのコードです。どのような助けも大いにありがたくなり、素人の言葉での説明も大いに評価されます。
CODE = {'A': ')', 'a': '0', 'B': '(', 'b': '9', 'C': '*', 'c': '8',
'D': '&', 'd': '7', 'E': '^', 'e': '6', 'F': '%', 'f': '5',
'G': '$', 'g': '4', 'H': '#', 'h': '3', 'I': '@', 'i': '2',
'J': '!', 'j': '1', 'K': 'Z', 'k': 'z', 'L': 'Y', 'l': 'y',
'M': 'X', 'm': 'x', 'N': 'W', 'n': 'w', 'O': 'V', 'o': 'v',
'P': 'U', 'p': 'u', 'Q': 'T', 'q': 't', 'R': 'S', 'r': 's',
'S': 'R', 's': 'r', 'T': 'Q', 't': 'q', 'U': 'P', 'u': 'p',
'V': 'O', 'v': 'o', 'W': 'N', 'w': 'n', 'X': 'M', 'x': 'm',
'Y': 'L', 'y': 'l', 'Z': 'K', 'z': 'k', '!': 'J', '1': 'j',
'@': 'I', '2': 'i', '#': 'H', '3': 'h', '$': 'G', '4': 'g',
'%': 'F', '5': 'f', '^': 'E', '6': 'e', '&': 'D', '7': 'd',
'*': 'C', '8': 'c', '(': 'B', '9': 'b', ')': 'A', '0': 'a',
':': ',', ',': ':', '?': '.', '.': '?', '<': '>', '>': '<',
"'": '"', '"': "'", '+': '-', '-': '+', '=': ';', ';': '=',
'{': '[', '[': '{', '}': ']', ']': '}'}
def main():
#Open the file you want to encrypt.
infile = str(input('Enter the name of the input file: '))
#read its contents
dtext = open(infile, 'r')
#read the line from the file
dtext = dtext.readlines()
#strip the newline
#dtext = dtext.rstrip('\n')
#call the encryptText function
encryptText(dtext)
def encryptText(dtext):
#enter the name of the file to write to
outfile = str(input('Enter the name of the output file: '))
#open the file to send encrypted text to
etext = open(outfile, 'w')
#set accumulator value
count = 0
#create a for loop to read each separate character
for line in dtext:
wordList = line.split()
print(dtext, CODE[dtext])
count += 1
main()
何が問題になりましたか?私は 'TypeError:unhashable型: 'list''を得ました...それはあなたが見るものですか? – tdelaney
ええ、私はその部分を分かったと思うけど、今は新しいエラーが出てきています... KeyError: '' – Classicalclown
私の解決方法では、私は 'dict.get(c、c)'を使ってマップを取得しようとしましたそれ以外の場合はオリジナルを通します。 ASCIIテキストの場合、タブや改行などのエンコードは行いません。 – tdelaney