简体   繁体   English

Python(idle)使用ASCII编码

[英]Python (idle) Encode using ASCII

Converting each plaintext character to its ASCII (integer) value and store in a list. 将每个纯文本字符转换为其ASCII(整数)值并存储在列表中。 I have done like this: 我这样做是这样的:

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)

But I have an error: 但是我有一个错误:

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'

How can I fix this? 我怎样才能解决这个问题?

I want the result of: 我想要以下结果:

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

In order to convert each plain text character to integer and store it into a list you need something simple like this: 为了将每个纯文本字符转换为整数并将其存储到列表中,您需要这样的简单操作:

//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)

I've added inline comments to the code to help you as I can see from this and your previous question that you are struggling a bit. 我已经在代码中添加了内联注释,以帮助您,正如从此以及上一个问题中所看到的那样,您会遇到一些麻烦。

EDIT 编辑

In order for the key to be added and then turned back into a character you have to use the following code. 为了添加密钥,然后将其转换回字符,您必须使用以下代码。 I've added inline comments only to the new lines. 我仅在新行中添加了行内注释。

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)

function ascii has no attribute called append, you have it confused with a list try the following : 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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM