0
votes

For some reason the line "new1 = [chr(x) for x in new]" cannot take a float. I am converting all the ASCII integers in the 'new' list back to a list with characters, according to their respective ASCII number, this list would be saved as new1.

if de == "d":
   num = input("input ur number: ")
   key = int(input("Decryption key?"))
   num = [int(x) for x in num.split()]
   new = [x /key for x in num]
   new1 = [chr(x) for x in new]

This is the error:

--Traceback (most recent call last): File "C:\Python34\Encryptiontrue.py", line 23, in new1 = [chr(x) for x in new]

-- File "C:\Python34\Encryptiontrue.py", line 23, in new1 = [chr(x) for x in new]

-- TypeError: integer argument expected, got float

Full Code:

de = input("Decrypt or encrypt?: ")

if de == "e":
   word = input("input ur text: ")
   word = word.lower() 
   new = list(word)

   key = int(input("what number by?: "))

   new1 = [ord(x) * user for x in new]

   new2 = " ".join(str(x) for x in new1)
   print(new2)

if de == "d":
   num = input("input ur number: ")
   key = int(input("Decryption key?"))
   num = [int(x) for x in num.split()]
   new = [x /key for x in num]
   new1 = [chr(x) for x in new]
1
What are you inputs? - Andy
chr(x) shouldn't take a float because how would you convert something like 26.23 into a ascii character? do an explicit integer conversion in your list comprehension for new with x//key or int(x/key). - R Nar
@RNar Thank you so much! It works :) - Clem Lanoe

1 Answers

1
votes

Change

new = [x / key for x in num]

To

new = [x // key for x in num]

In Python 3, / indicates "true division" which always returns a float. // indicates "floor division". Floor division will return an integer if both arguments are integers. For more details see PEP 238 -- Changing the Division Operator.