-1

Im trying to enter multiple numbers such as

[106, 103, 110, 110, 113, 32, 121, 113, 116, 110, 102] 

into the decryption using the ord(), then enter it through the caeser cipher. The caeser cipher works, but I dont know how to make work the chr(), but it shows that it is an error when I key in the numbers. Thank you!

    print("Decryption")
    text = int(input("Enter encrypted numbers: \n"))
    encrypt = chr(text)
    decrp_key = int(input("Enter key:\n"))
    decrypted_text = ""
    for i in range(len(encrypt)):
        if ord(encrypt[i]) == 32:
            decrypted_text += chr(ord(encrypt[i]))
        elif ((ord(encrypt[i]) - decrp_key) < 97) and ((ord(encrypt[i]) - decrp_key) > 90):
            temp = (ord(encrypt[i]) - decrp_key) + 26
            decrypted_text += chr(temp)
        elif (ord(encrypt[i]) - decrp_key) < 65:
            temp = (ord(encrypt[i]) - decrp_key) + 26
            decrypted_text += chr(temp)
        else:
            decrypted_text += chr(ord(encrypt[i]) - decrp_key)
    print("Decrypted Text: " + decrypted_text)```
nagyl
  • 1,644
  • 1
  • 7
  • 18
  • 1
    Can you please specify, the input/output you wanted. You are reading a single int, but you are referring to a list of integers in your description. – nagyl Jul 24 '20 at 04:22
  • im trying to input the numbers together then using chr convert it into words in order to be translated for the caeser cipher. so i can be able to read the output as words. – 悪魔 王 Jul 24 '20 at 04:25
  • Hint: take the user input, but `append` to a list, if you need a list of numbers. – de_classified Jul 24 '20 at 04:28
  • ValueError: invalid literal for int() with base 10: '106, 103, 110, 110, 113, 32, 121, 113, 116, 110, 102' this is the error i got – 悪魔 王 Jul 24 '20 at 04:29
  • You can't input all the numbers at *once*, you have to `append` them to a list, `storeNum= []` then append to that list. – de_classified Jul 24 '20 at 04:34

2 Answers2

1

I think you want to take a set of input numbers and iterate through them. If you are fairly new to python you may need to learn a few commands but here's something you can work with.

decrp_key = input("Enter key:\n")
decrypted_text = list(map(int,decrp_key.split(',')))
print (decrypted_text)

Output:

>>> Enter key:
>>> 1, 105, 201, 103, 205
>>> [1, 105, 201, 103, 205]

To access the list, you can use a for loop.

for i in decrypted_text:
    print(i)

Output:

1
105
201
103
205
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
0

If you would like to input a list of numbers, then you need to do

print('Decryption')
text = ""
while True:
    temp = input('Enter encrypted number: ')
    if not temp: #break on empty input
        break
    text = text + chr(int(temp))
decrp_key = int(input("Enter key:\n"))
#rest of your code goes here...

Also you should wrap you input around with a try except block, to make sure your input is a valid int!

nagyl
  • 1,644
  • 1
  • 7
  • 18
  • hi i got a value error, im quite new to python. i dont quite understand – 悪魔 王 Jul 24 '20 at 04:31
  • You have to add the numbers one by one. Empty line to stop. Please accept this as an answer if its working – nagyl Jul 24 '20 at 04:33
  • so its not possible to add several numbers at a time? – 悪魔 王 Jul 24 '20 at 04:34
  • If you are a beginner, first learn to use input function like above. After you can learn text manipulation. – nagyl Jul 24 '20 at 04:36
  • No, he would like to provide a whole list of ints to the input, which he could not understand how to process yet, which is not equal to appending a list in his code. He would like to convert those ASCII (int) values to characters, therefore there is no need for any list, could be seen by looping through a string below reading by him. Thank you! – nagyl Jul 24 '20 at 04:41
  • i tried using this code but i got a Value error. could you further advice me on how i can input multiple numbers? – 悪魔 王 Jul 24 '20 at 04:48
  • No sorry, you clearly don't understand how input works. I suggest you learn, and practice a little, before inputting a whole list. By the way you can use my code, and add the numbers one by one. There is another answer which provides what you need, but you won't understand nothing. At least upvote. Thank you! – nagyl Jul 24 '20 at 04:50