I am trying to encrypt a text by changing its character code and then converting it back into characters using python. It is performing encryption but giving this error for decryption:
Traceback (most recent call last): File "C:\\Users\\CZ 3\\Downloads\\nis1.py", line 15, in
cipher1 = cipher1 + chr(ord(cipher[i])/2)
TypeError: integer argument expected, got float
cipher = ''
plaintext = ''
choice = ''
cipher1 = ''
a=''
choice = input("\nEnter 1 to Encrypt and decrypt ")
while choice == '1':
plaintext = input("\nEnter the message to encrypt: ")
for i in range(0, len(plaintext)):
cipher = cipher + chr(ord(plaintext[i])*2)
print ("encrypted text" + '\n\n' + cipher + '\n\n' )
print("decrypted text:" + '\n\n')
for i in range(0, len(plaintext)):
cipher1 = cipher1 + chr(ord(cipher[i])/2)
print (cipher1 + '\n\n')
cipher= ''
choice = input("\nEnter 1 to Encrypt and decrypt: ")
In Python 3 /
is true division (see PEP 238 ), it return float with two int:
>>> type(6/2)
<class 'float'>
If you sure all numbers are even you can use //
floor div:
>>> type(6//2)
<class 'int'>
so try this:
cipher1 = cipher1 + chr(ord(cipher[i]) // 2)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.