1

I am writing a code that will encrypt a list of String entered by user. these lists will be encrypted and then it will be decrypted. But once it will reach the part of encryption it gives me this error.

Traceback (most recent call last): File "C:/Users/dana/Desktop/q2.py", line 16, in x = ord(c) TypeError: ord() expected a character, but string of length 4 found

I am sure that it will give the same error even in the decryption part.

This is my code:

    # Encryption
list1=[]
list2=[]
i = 0
while not(False):
    plain_text = input ("Enter any string ")
    if (plain_text !='q'):
        list1.append(plain_text)
        i=i+1
    else:
        break



encrypted_text = ""
for c in list1:
    x = ord(c)
    x = x + 1
    c2 = chr(x)
    encrypted_text = encrypted_text + c2
print(encrypted_text)

#Decryption
encrypted_text = "Uijt!jt!b!uftu/!BCD!bcd"

plain_text = ""
for c in encrypted_text:
    x = ord(c)
    x = x - 1
    c2 = chr(x)
    plain_text = plain_text + c2
print(plain_text)
list2=[encrypted_text]
print(plain_text)
print("the original msgs are :" , list1)
print("the encrypted msgs are :" ,list2)
Kim YOOKO
  • 35
  • 1
  • 8

2 Answers2

2

list1 contains whatever strings the user enters in response to the input prompt.

Then your first for loop iterates over list1. c takes on values which are the elements of list1. Then you use ord on c. Your intent, I expect, is to use ord on the elements of c, instead. Try adding an additional loop somewhere.

Also, consider organizing your code into functions.

Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122
1

ord() takes single character

for c in list1:
 x = ord(c)

but above loop return string as C that's why you are getting error Corrected Code

list1=[]
list2=[]
i = 0
while not(False):
    plain_text = input ("Enter any string ")
    if (plain_text !='q'):
        list1.append(plain_text)
        i=i+1
    else:
        break



encrypted_text = ""
for c in list1: #Changed Code
  for c1 in c:
    x = ord(c1)
    x = x + 1
    c2 = chr(x)
    encrypted_text = encrypted_text + c2
print(encrypted_text)

#Decryption
encrypted_text = "Uijt!jt!b!uftu/!BCD!bcd"

plain_text = ""
for c in encrypted_text:
    x = ord(c)
    x = x - 1
    c2 = chr(x)
    plain_text = plain_text + c2
print(plain_text)
list2=[encrypted_text]
print(plain_text)
print("the original msgs are :" , list1)
print("the encrypted msgs are :" ,list2)
Jay Shankar Gupta
  • 5,918
  • 1
  • 10
  • 27