0

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
5
  • ascii is a built-in function. Did you forget to create a list named ascii? Commented Sep 27, 2017 at 17:00
  • @ponach I changed it but still have the same problem. Commented Sep 27, 2017 at 17:02
  • This is the second time you ask this question. I am pretty sure you have your answer here or at least a pretty good example of something similar: stackoverflow.com/questions/46412370/… Commented Sep 27, 2017 at 17:02
  • Possible duplicate of Caesar Cipher in Python (unexpected error) Commented Sep 27, 2017 at 17:03
  • 3
    Ah, so you already were given code and you broke that by a) removing the ascii_list = [] line, and b) changing ascii_list.append to ascii.append. Commented Sep 27, 2017 at 17:10

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you! It works. But how can I use the ASCII and encrytion key? values.append(ascii(ord(x))+"key") I tried this..seems ascii works but where should I put the key?
See my edit for the solutions with additional functionality.
Your code works! The problem here I have is that, I am having a result of ['ô', 'Ą', 'Ñ', 'ă', 'Ā', 'ĉ', 'Ñ', 'þ', 'Ċ', 'Ñ', 'Ą', 'Ā', 'ĉ'], when I enter name as "CS Rox my Sox" and key as "177". But I need to get the result of "CWOTFZ&]QCHICA". How should I fix this?
Is this a cipher, and if so can you tell me what the rules are. I can see that in your example input/ output(CS Rox my Sox -> CWOTFZ&]QCHICA the first letter stays the same and the spaces get encoded into different characters.
Here are the rules: 1. For the encoding process all characters, including letters, numbers and special characters must be considered. 2. Convert all alphabetic characters ('a' to 'z') to upper case before doing the encryption. 3. Encryption Process: Use a right-shift for encryption – a positive change – of the ASCII value ofevery character in the plaintext.
|
0

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

1 Comment

If i try this, the error comes up: Traceback (most recent call last): File "C:/Users/Heera/Desktop/help.py", line 9, in <module> name = ''.join([chr(ord(x)+key) for x in name]) File "C:/Users/Heera/Desktop/help.py", line 9, in <listcomp> name = ''.join([chr(ord(x)+key) for x in name]) TypeError: unsupported operand type(s) for +: 'int' and 'str'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.