0

so I have the encrypt dictionary already set up:

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
encrypt = "CDEFGHIJKLMNOPQRSTUVWXYZAB "

word = input("Enter the message: ")
d = {alphabet[i]: encrypt[i] for i in range(len(alphabet))}

and lets say I want to encrypt word, I would use replace()here, but if I use that one, it just replaces everything with A and B.

My code currently looks like that:

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
encrypt = "CDEFGHIJKLMNOPQRSTUVWXYZAB "

d = {alphabet[i]: encrypt[i] for i in range(len(alphabet))}

word = input("Enter a word: ")
 
for key in d.keys():
    word = word.upper().replace(key, d[key])

print(word)

and in the Terminal it prints out "BAAB". Not sure why its only using the A and B and nothing more.

7
  • why just adding a fixed value to the ASCII rapresentation of the actual letters? According to yours is +2? Commented May 3, 2022 at 13:40
  • @NicoCaldo could you may explain where I would've done that? Is it because of the "key"-values? Commented May 3, 2022 at 13:47
  • @NicoCaldo: Not quite. An exception would be needed for space. Commented May 3, 2022 at 13:47
  • 2
    Just a reminder for beginning programmers that this encryption is very easily broken. See e.g. devqa.io/encrypt-decrypt-data-python for a more straight-forward and much stronger encryption. Commented May 3, 2022 at 13:50
  • 1
    @Jelmer Yes I know, its just the Caesar Cipher that I had to use in a exercise, otherwise I surely would've used a much more secured encryption! Commented May 3, 2022 at 14:01

5 Answers 5

3

You can do it with .join():

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
encrypt = "CDEFGHIJKLMNOPQ9RSTUVWXYZAB "
d = {a: e for  a, e in zip(alphabet, encrypt)}
''.join(d[i] for i in 'word'.upper())

Output:

'XQSF'
Sign up to request clarification or add additional context in comments.

1 Comment

No need to reinvent dict(zip(alphabet, encrypt)).
0

You're replacing the letter one by one so the letter a is changed to c but in the next loop, c will be changed to e etc, you only have b and a because it's the 2 last letters in your code and the gap in your code is 2.

You have to use another variable and iterate through the initial variable and add the value letter in another variable from the key in the first variable

Not sure if it's really clear so here's an example :

word2 = ""
for i in word:
  word2 += d[i]

Comments

0

First you replace every A with C, so you don't have As anymore (only B to Z). Then you replace every B with D, so you don't have Bs anymore, either (only C to Z). Etc. Near the end, you only have Ys and Zs left, and you replace them with A and B, respectively. That's why your output only has As and Bs.

You should make all replacements "in parallel" instead of sequentially like you do, and a proper way to do that is to use str.translate:

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
encrypt = "CDEFGHIJKLMNOPQRSTUVWXYZAB "
trans = str.maketrans(alphabet, encrypt)

word = 'Chaos'
print(word.upper().translate(trans))

Output:

EJCQU

Comments

0

Something like this should solve it for you:

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
encrypt = "CDEFGHIJKLMNOPQRSTUVWXYZAB "

d = {alphabet[i]: encrypt[i] for i in range(len(alphabet))}

print(''.join([d[i.upper()] for i in input("Enter a word: ")]))

2 Comments

you don't need the extra [ ] to make it a list comprehension, join also works on generator expressions print(''.join(d[i.upper()] for i in input("Enter a word: ")))
@Nin17 list comp is more efficient there, though.
0

Adding to @Nin17's answer and assuming the dictionary d is given, you can use map instead of a list comprehension to obtain the string:

res = "".join(map(d.get, "word".upper()))

As NicoCaldo points out, you may also simply add 2 to the ASCII numerical representation of each character, wrapping around and making an exception for the space character .

from_letter = ord("A")
to_letter = ord("Z")
range = to_letter - from_letter
offset = ord("B") - from_letter
res = "".join(map(lambda c: c if c == " " else chr(from_letter + ((ord(c)-from_letter+offset) % range)), "some words".upper()))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.