简体   繁体   中英

Python (idle) Encode using ASCII

Converting each plaintext character to its ASCII (integer) value and store in a list. 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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