Reputation: 67
I need to turn an entered string into ASCII values, add three to them, then print out the new string that got all the letters changed.
I got to the point where I can get the ASCII values in a list, but i'm having trouble turning that back into a string.
This is the code I wrote so far.
def encrypt (x): #function for encrypting
#y=[ord(c) for c in x] #turns strings into values this works
#y=[3+ ord(c) for c in x] #adds 3 to the values of the letters turned to number this also works
y=str([3+ ord(c) for c in x]) # this does not do what I expected it to do. Neither does char
print(y)
'''def decrypt (x):
y=str([-3 + ord(c) for c in x])
print(y)
'''
x=str(input("Enter something to be encrypted: ")) #gets string to encrypted
encrypt (x) #calls function sends the entered sentence
'''
x=str(input("Enter something to be decrypted: ")) #gets string to decrypted
decrypt (x)
'''
I commented out the second part of turning it back, if I can get it to give me back the string with the letters changed, I can probably figure the rest out.
Thanks for any help you can offer.
Upvotes: 0
Views: 1609
Reputation: 129059
str
, applied to a list of integers, as you discovered, gives you a string like [65, 66, 67]
, not ABC
. To convert a single integer back to a string, you can use chr
. Then, to do it for a whole list of integers, use join
:
y = ''.join(chr(3 + ord(c)) for c in x)
Upvotes: 3