ord() expected a character error - python

I'm writing a program that encodes and decodes a text chosen by the user. My decoder function seems to work fine when I input unicode codes to it to return the character but my encoder function is not working.
Simply put, what I'm trying to accomplish is:
ask the user if they want to encode or decode
if they want to encode, i want to gather a string of phrases until the user enters 'done', append them to the list, then encode them using my encode function, then finally print out the encoded list.
if they want to decode, i want to gather a series of intergers that are actually unicode codes until the user enters 'done', append them to list2, then decode them using my decode function and print out that decoded list.
Here is my code.
def encode(character):
code = ord(character)
new_code = code * 4 + 10 // 2
return new_code
def decode(character):
code2 = character * 2 - 10 // 4
new_code2 = chr(code2)
return new_code2
def main():
encoded_list = []
decoded_list = []
choice = input('Hello! Do you want to encode or decode? ').lower()
if choice == 'encode':
text = input('Enter text to encode or "done": ')
while text != 'done':
encoded = encode(text)
encoded_list.append(encoded)
text = input('Enter text to encode or "done": ')
if text == 'done':
print(encoded_list)
elif choice == 'decode':
text2 = input('Enter characters to decode or "done": ')
while text2 != 'done':
text2 = int(text2)
decoded = decode(text2)
decoded_list.append(decoded)
text2 = input('Enter characters to decode or "done": ')
if text2 == 'done':
print(decoded_list)
else:
print('Please enter a valid response')
main()
thanks so much!

The main problem is that you have to encode each character of the string separately. The ord function can only take a single character, so instead of:
encoded = encode(text)
you want:
encoded = ""
for char in text:
encoded += encode(text)
Also (this is unrelated to the error you're getting), you forgot to account for order of operations. You put code * 4 + 10 // 2 instead of
(code * 4 + 10) // 2, so the code is really equivalent to code * 4 + 5. You did a similar thing for decode.

You need to use ord on each character of the string, not the whole string at once:
def encode(character):
code = map(ord,character)
new_code = [(x * 4 + 10) // 2 for x in code]
return new_code
def decode(character):
code2 = [(x * 2 - 10) // 4 for x in character]
new_code2 = "".join([chr(x) for x in code2])
return new_code2

Related

Create encryption and decryption program

I need someone to edit my code to debug it. It won't display any of the encrypted or decrypted text which I think is because the formatting is messed up but I don't know. If you could help that'd be greatly appreciated. (I have to include functions and user input)
result = ''
text = ''
text = input("Do you want to encrypt or decrypt the message?\n 1 to encrypt, 2 to decrypt or 0 to exit program. ")
def toList(text):
text.split()
return text
decrypted = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
encrypted = b"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM "
encrypt_table = bytes.maketrans(decrypted, encrypted)
decrypt_table = bytes.maketrans(encrypted, decrypted)
text = input('Enter message for encryption: ')
def encrypt(text):
result = ''
text = ''
result = text.translate(encrypt_table)
print(result + '\n\n')
cipherText = input('Enter message to decrypt: ')
def decrypt(cipherText):
result = ''
message = ''
result = message.translate(decrypt_table)
print(result + '\n\n')
if text == '1':
encrypt(text)
print(result + '\n\n')
elif text == '2':
decrypt(cipherText)
elif text != '0':
print('You have entered an invalid input, please try again. \n\n')
You had quite a number of confusions. Look at encrypt, for example. You pass in text, then you immediately set text='', thereby destroying the message that was input. Similarly, in decrypt, you pass in cipherText, but you run message.translate. And your functions need to return their results, not print them. Let the caller decide what to do with the returned results.
Also, it is a good practice to collect your functions at the top of the module, so you don't fool yourself into believing that things get called in the wrong order.
Here is your code, modified so that it works:
def encrypt(text):
result = text.translate(encrypt_table)
return result
def decrypt(message):
result = message.translate(decrypt_table)
return result
decrypted = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
encrypted = b"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM "
encrypt_table = bytes.maketrans(decrypted, encrypted)
decrypt_table = bytes.maketrans(encrypted, decrypted)
while True:
text = input("Do you want to encrypt or decrypt the message?\n 1 to encrypt, 2 to decrypt or 0 to exit program. ")
if text == '1':
text = input('Enter message for encryption: ')
result = encrypt(text)
print(result)
elif text == '2':
cipherText = input('Enter message to decrypt: ')
result = decrypt(cipherText)
print(result)
elif text == '0':
break
else:
print('You have entered an invalid input, please try again. \n\n')
Note that encrypt and decrypt don't really need to store in a temporary variable. The only reason to do that is because it is more convenient for debugging, to add a quick print(result) before the return.

My program runs but it doesn't output an Encrypted Message

The code I wrote is a Vignere Cipher encryption program that uses a keyword to encrypt a message. I wrote this code and when I finished it I ran it and it did all it was supposed to but output the encrypted message. See my code below, any help is gratefully received:
ans = False
print(""" *****Hello. Welcome to the Vignère Cipher Encryption Program*****
***This program uses a keyword that is repeated until it
matches the same lenght of the message and then adds its
numerical value to the numerical value of the message and
outputs the encrypted message in alpha.
Please press:
E to Encrypt
D to Decrypt
or double tap enter to quit.
""")
ans=input("What would you like to do now???")
if ans == "E":
plaintext = input("Please enter a message to be encrypted: ").upper()
keyword = input("Please enter a keyword to be used to encrypt a message (alpha only): ").upper()
ciphered = " "
for i in range (len(plaintext)):
char = plaintext[i]
alphakeywordvalue = ord(keyword[i%len(keyword)]) - ord("A")+1
if char.isupper():
if ans == "E" :
value = ord(char) + alphakeywordvalue
if value > ord("Z"):
value -= 26
print ("Your encrypted text is:", ciphered)
elif ans == "D":
plaintext = input("Please enter a message to be dencrypted: ").upper()
keyword = input("Please enter a keyword to be used to dencrypt a message (alpha only(make sure that it is the same keyword used to encrypt the message)): ").upper()
ciphered = " "
for i in range (len(plaintext)):
char = plaintext[i]
alphakeywordvalue = ord(keyword[i%len(keyword)]) - ord("A")+1
if char.isupper():
if ans == "D" :
value = ord(char) - alphakeywordvalue
if value <ord("A"):
value += 26
ciphered += chr(value)
print ("Your decrypted text is:", ciphered)
This is not a good style of writing code. Very shabby and hard to read. You should make methods for individual parts and create a separate main() like section to interact with the user, if required. Engine should be hidden, car body should be polished. Keep them separate. And yes, DO NOT REPEAT YOURSELF
Well, here's my re-written code. Important parts contain the comments. Errors are explained after it..
def encrypt(message, key, direction='E'):
# Look here. There are three arguments. Third one takes 'E' to encrypt
# and anything else to decrypt. You can modify to handle more cases
ciphered = "" # Initialize. You did it almost well
for i in range (len(message)):
char = message[i]
alphakeywordvalue = ord(key[i%len(key)]) - ord("A")+1 # Perfect. We took the key
if direction=='E': # To encrypt
value = ord(char) + alphakeywordvalue
else: # To decrypt
value = ord(char) - alphakeywordvalue
ciphered += chr(value) # chr is the inverse of ord. It gets the character back
# You missed this line
return ciphered
That's it. Now you can write the code to interact with user or other modules. Here's a sample test:-
message = "Hello World"
key = "abc"
print "ORIGINAL : "+message
encoded_message = encrypt(message, key, 'E')
print "ENCRYPTED : "+encoded_message
plain_message = encrypt(encoded_message, key, 'D')
print "DECRYPTED : "+plain_message
Here's the output:
Now you can edit this method to handle more cases like out of ascii range characters etc.
How could it print the encrypted message, the encryption routine never changes ciphered from an empty string.
if ans == "E":
plaintext = input("Please enter a message to be encrypted: ").upper()
keyword = input("Please enter a keyword to be used to encrypt a message (alpha only): ").upper()
1)-> ciphered = " "
for i in range (len(plaintext)):
char = plaintext[i]
alphakeywordvalue = ord(keyword[i%len(keyword)]) - ord("A")+1
2)-> if char.isupper():
3)-> if ans == "E" :
value = ord(char) + alphakeywordvalue
if value > ord("Z"):
value -= 26
4)-> print ("Your encrypted text is:", ciphered)
ciphered set to empty string, is never changed.
You always know char is upper, because you made all the plaintext upper()
You know ans == "E" because you tested it earlier
This print() is so far indented it tries to print every time through the loop

Moving a string forward in the alphabet?

I have been asked just as a challenge to create a program of which encrypts an input. I have looked into creating a program but there isn't much around on how to do it. It seems like it shouldn't be too complicated, but I haven't been taught any of it in my lessons. I read this post too Get character position in alphabet but didn't have much luck! I have this so far:
import sys
import os
import time
import string
def Cryption():
####################
encrypt = 'encrypt'
decrypt = 'decrypt'
####################
s = raw_input("Would you like to encrypt or decrypt? Enter your answer")
if encrypt in s:
print("Loading encryption sector...")
time.sleep(2)
enc = raw_input("Please input the string you would like to encrypt")
print ("Your decrypted word is: " + enc)
if decrypt in s:
print("Loading decryption sector...")
time.sleep(2)
dec = raw_input("Please input the string you would like to decrypt")
else:
print("Your input was invalid, please re-enter your choice!")
r = raw_input("Press enter to restart your program")
Cryption()
Cryption()
I was thinking if I took the input added 5 onto each letter value then re-printed the product. Which functions would I use to add 5 onto the order in the alphabet? ord()? And if so could someone point me in the direction of how to use it? Thanks in advance!
import string
def encrypt_s(shift):
lower = string.ascii_lowercase
# shift forward current index of each character + the shift
dict_map = {k:lower[(lower.index(k)+shift)%26] for k in lower}
return dict_map
def decrypt_s(shift):
lower = string.ascii_lowercase
# shift forward current index of each character - the shift to get original string
dict_map = {k:lower[(lower.index(k)-shift)%26] for k in lower}
return dict_map
def Cryption():
####################
encrypt = 'encrypt'
decrypt = 'decrypt'
####################
s = raw_input("Would you like to encrypt or decrypt? Enter your answer")
if encrypt in s:
print("Loading encryption sector...")
time.sleep(2)
enc = raw_input("Please input the string you would like to encrypt").lower()
enc_s = encrypt_s(5)
enc = "".join([enc_s[char] if char in enc_s else char for char in enc])
print ("Your encrypted word is: ",enc)
elif decrypt in s:
print("Loading decryption sector...")
time.sleep(2)
dec = raw_input("Please input the string you would like to decrypt").lower()
dec_s = decrypt_s(5)
dec = "".join([dec_s[char] if char in dec_s else char for char in dec])
print ("Your decrypted word is: ",dec)
else:
print("Your input was invalid, please re-enter your choice!")

How to temporarily ignore punctuation? python

hi i'm trying to write a function to decode a message the user entered
decypherbook = {'0000':8, '0001':1, '0010':0, '0011':9, '0100':5, '0101':3, '0110':7, '0111':2, '1110':4, '1111':6}
userdecode = raw_input("Enter the number you want to de-cypher: ")
def decode(cypher, msg):
length = len(msg)
decoded = ""
key_index = 0 ## starting position of a key in message
while key_index < length:
key = msg[key_index:key_index + 4]
decoded += str(cypher[key])
key_index += 4
return decoded
print "After de-cypher: ", decode(decypherbook, userdecode)
but if the user input a message like "0001,0001", which i would like the result be "1,1". How could i make my code temporarily ignore punctuation so it doesn't mess up with my indexing +4 in my code and still able to print out the punctuation later?
You can check if the next characeter is an integer. If not, just add it to the string and continue to the next character:
def decode(cypher, msg):
length = len(msg)
decoded = ""
key_index = 0 ## starting position of a key in message
while key_index < length:
key = msg[key_index:key_index + 4]
decoded += str(cypher[key])
key_index += 4
# Pass every non digit after
while key_index < length and not msg[key_index].isdigit():
decoded += msg[key_index]
key_index += 1
return decoded
Here is an example of execution:
>>> def decode(cypher, msg):
... # ...
>>> decode(decypherbook, '0001,0010')
'1,0'
Side note: You could also prefer to make a list as a buffer instead of recreating a string every time (string are immutable, every += creates a new object) and do ''.join(buffer) at the end. Just for performance purpose.
Use split method from string object
userdecode = raw_input("Enter the number you want to de-cypher: ")
userdecode_list = userdecode.split(",")
And than call your function like this with join method from string object
print "After de-cypher: ", decode(decypherbook, "".join(userdecode_list))
I feel like replace matches your need more.
def decode(cypher, msg):
for key, value in cypher.items():
msg = msg.replace(key, str(value))
return msg
A fun-one-liner (which assumes userdecode is guaranteed to be of the form r"(\d{4},)*")
def decode(cypher, msg):
return ",".join([str(cypher[x]), for x in userdecode.split(",")])

Replacing words in Python?

I'm trying to replace the letters in key with the letters in alpha (and vice versa):
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
key = "XPMGTDHLYONZBWEARKJUFSCIQV"
I am trying to change a string to become encoded (and the other way around), so say "Hello" would become "LTZZE". Any idea how to do this? This is my current code:
usrInput = 0
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
key = "XPMGTDHLYONZBWEARKJUFSCIQV"
def menu():
print "SECRET DECODER MENU"
print ""
print "1) Quit"
print "2) Encode"
print "3) Decode"
usrInput = raw_input("What would you like to do?")
return usrInput
def encodeWord():
plain = plain.upper()
length = len(plain)
encode = plain.encode(alpha, key)
return encode
def decodeWord():
coded = coded.upper()
length = len(coded)
decode = coded.decode(key, alpha)
return decode
def main():
keepGoing = True
while keepGoing:
usrInput = menu()
if usrInput == "2":
plain = raw_input("Text you want to be encoded: ")
encodeWord()
print encode(plain)
elif usrInput == "3":
coded = raw_input("Code you need to be decyphered: ")
decodeWord()
print decode(coded)
elif usrInput == "1":
print "Thanks for doing super secret spy stuff with me. No one seems to want to anymore. Goodbye. ):"
keepGoing = False
else:
print "I don't know what to do! Ahhh!"
main()
Note this is a homework assignment for a computer science class. I created the assignment, and I'm aware it's on stack overflow. If you turn it in as your own work, I will know. You will earn a zero for the assignment and we will begin academic misconduct proceedings.
(If you're playing along at home, this is indeed a string manipulation assignment, and explicitly NOT to be considered a good cryptographic practice. We also are not allowing maketrans() for this assignment, because it's a string manipulation and function exercise for beginning programmers.)
If you're really desperate for help, come see me or one of the recitation leaders we're paying to help you.
Use str.maketrans and str.translate. If you use Python 2 this functions are in string (here (maketrans) and here (translate)).
Example (python 3):
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
key = "XPMGTDHLYONZBWEARKJUFSCIQV"
enc = str.maketrans(alpha, key)
usrInput = 'HELLO'
print(usrInput.translate(enc))
Example (python 2)
import string
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
key = "XPMGTDHLYONZBWEARKJUFSCIQV"
enc = string.maketrans(alpha, key)
inp = 'HELLO'
print string.translate(inp, enc)
Output:
LTZZE

Categories

Resources