Search code examples
pythonpython-3.xencryptionvigenere

Vigenere Cipher no error message Python


Here is the code for the Vigenere Cipher:

BASE = ord("A") #65
print("Welcome to the keyword encrypter and decrypter!")
msg = ("FUN")
keyword = ("RUN")
list_out = []

for letter in msg:
    a = (ord(letter) - (BASE)) #67 - 65 = 2

for character in keyword:
    b = (ord(character) - (BASE)) #72 - 65 = 7
    list_out.append(BASE + a + b) #65 + 2 + 7 = 74
("".join(str(list_out)))

I am trying to get the each letter from the message and the keywords to individually be taken away from 65, which is the BASE. Then at the end I want the BASE to be added to the results from a and b. I want the new letter appended to the list and printed. If anyone can help, it will be much appreciated.

Above I stated how the program should work however I am not sure what the problem/problems are. The main problem with my code is that nothing is being printed.


Solution

  • You call join on the list to join the contents, not str(list), you are casting the list itself to a str and calling join on that, not the actual list.

    You need to map each int to a str in your case.

    "".join(map(str,list_out))
    

    Which is equivalent to "".join([str(x) for x in list_out ])

    If you want to change the ord's to chars you can map to chr:

    "".join(map(chr,(list_out)))
    

    Which can all be done in a single comprehension:

    print("".join([chr(BASE + a + (ord(ch) - (BASE))) for ch in keyword)])
    

    You are also only using the last value for a in the previous loop as you are assigning a new value each iteration depending on what that is supposed to be doing you may need += or nested loops:

    for letter in msg:
        # will be equal to  (ord(N) - (BASE))
        a = (ord(letter) - (BASE)) #67 - 65 = 2