Python(空闲)使用ASCII编码

时间:2017-09-27 16:55:35

标签: python ascii

将每个明文字符转换为其ASCII(整数)值并存储在列表中。我这样做了:

print("This program uses a Caesar Cipher to encrypt a plaintex message using the encrytion key you provide")
name = input("Enter the message to be encryted:")       
key = input("Enter an intefer for an encryption key:")

name = name.upper()

x =""
for x in name:                       
    name = ascii.append(chr(ord(name[x])+key))               
print(name)

但我有一个错误:

Traceback (most recent call last):
  File "C:\Users\Heera\Downloads\NameScore (1).py", line 10, in <module>
    name = ascii.append(chr(ord(name[x])+key))
AttributeError: 'builtin_function_or_method' object has no attribute 'append'

我该如何解决这个问题?

我想要结果:

This program uses a Caesar Cipher to encrypt a plaintext message using the encryption key you provide.
Enter the message to be encrypted: CS Rox my Sox
Enter an integer for an encrytion key: 177
The fully encoded message is: DZ'SV_!T`!ZVY

2 个答案:

答案 0 :(得分:1)

为了将每个纯文本字符转换为整数并将其存储到列表中,您需要这样简单的事情:

//Take the user input and stores it in a string variable called 'name'
name = input("Enter the message to be encrypted:")

//Convert all the characters in 'name' to upper case
name = name.upper()

//Create an empty list which will contain the ascii values
values = []

//For every character in the string 'name' assign it to x and run the loop
for x in name:

    //append the numeric value of the current character stored in 'x' to the list 'values'
    values.append(ord(x))

//When all characters have been appended to the list display the list.
print(values)

我已经在代码中添加了内联注释,以帮助您,因为我可以从此以及您之前的问题中看到您正在努力解决这个问题。

修改

为了添加密钥然后将其转换回字符,您必须使用以下代码。我已将内联评论仅添加到新行中。

print("This program uses a Caesar Cipher to encrypt a plain text message using the encryption key you provide")
name = input("Enter the message to be encrypted:")

//Take the user input for the encryption key (The key here is saved as string)
key = input("Enter an intefer for an encryption key:")

name = name.upper()

values = []

for x in name:

    //int(key) parses the key to an integer, then it is added to the ascii value of the current letter and is saved in the variable 'encrypted'
    encrypted = ord(x) + int(key)

    //chr(encrypted) parses the number value of 'encrypted' into the corresponding ascii character and then it appends it to the list 'values'
    values.append(chr(encrypted))

print(values)

答案 1 :(得分:0)

函数ascii没有名为append的属性,你把它与列表混淆了 尝试以下方法:

print("This program uses a Caesar Cipher to encrypt a plaintex message using the encrytion key you provide")
name = input("Enter the message to be encryted:")       
key = input("Enter an intefer for an encryption key:")

name = name.upper()
name = ''.join([chr(ord(x)+key) for x in name])