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
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
Hee Ra Choi
  • 113
  • 1
  • 1
  • 10
  • `ascii` is a [built-in function](https://docs.python.org/3/library/functions.html#ascii). Did you forget to create a list named `ascii`? – Martijn Pieters Sep 27 '17 at 17:00
  • @ponach I changed it but still have the same problem. – Hee Ra Choi Sep 27 '17 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: https://stackoverflow.com/questions/46412370/caesar-cipher-in-python-unexpected-error/46412769#46412769 – DobromirM Sep 27 '17 at 17:02
  • Possible duplicate of [Caesar Cipher in Python (unexpected error)](https://stackoverflow.com/questions/46412370/caesar-cipher-in-python-unexpected-error) – DobromirM Sep 27 '17 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`. – Martijn Pieters Sep 27 '17 at 17:10

2 Answers2

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)
DobromirM
  • 2,017
  • 2
  • 20
  • 29
  • 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? – Hee Ra Choi Sep 27 '17 at 17:30
  • See my edit for the solutions with additional functionality. – DobromirM Sep 27 '17 at 17:54
  • 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? – Hee Ra Choi Sep 27 '17 at 18:06
  • 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. – DobromirM Sep 27 '17 at 18:11
  • 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. – Hee Ra Choi Sep 27 '17 at 18:27
  • 4. Encryption Key: The encryption key may include one or more digits, but it will only use one at a time. For example: if the encryption key is “536”, your program will encrypt the first character with a right-shift of 5, the second character with a right-shift of 3 and the third with a right-shift of 6. The algorithm should continue using the values – Hee Ra Choi Sep 27 '17 at 18:27
  • Surely if we use that algorithm for "CS Rox my Sox" with the key 177 will result in something different than "CWOTFZ&]QCHICA" because the first letter should be shifted left by 1 resulting in 'D' – DobromirM Sep 27 '17 at 18:39
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])
rachid el kedmiri
  • 2,376
  • 2
  • 18
  • 40
  • If i try this, the error comes up: Traceback (most recent call last): File "C:/Users/Heera/Desktop/help.py", line 9, in name = ''.join([chr(ord(x)+key) for x in name]) File "C:/Users/Heera/Desktop/help.py", line 9, in name = ''.join([chr(ord(x)+key) for x in name]) TypeError: unsupported operand type(s) for +: 'int' and 'str' – Hee Ra Choi Sep 27 '17 at 17:17