-1
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!?."
list = []

msg = 'Hello World'
key = input()
key = int(key)
print(alphabet[key])

for x in msg:
  list.append(x)
  
print(list)

So basically i need to replace each letter of my array to a specify position (given by the key) of my string. e.g. key = 3 so H becomes L , E becomes I , L becomes P etc... it's basically a Caesar cipher

5
  • 1
    Don't use list as a variable name, it's the name of a built-in. Commented Oct 3, 2022 at 18:09
  • I don't see any arrays in this code. alphabet and msg are strings, key is an integer, and list is a list. Commented Oct 3, 2022 at 18:10
  • 1
    The functions "chr" and "ord" can help here. Commented Oct 3, 2022 at 18:11
  • There are hundreds of other questions about Caesar cipher here, you can look at them for hints. Commented Oct 3, 2022 at 18:12
  • str.index() may help you. Commented Oct 3, 2022 at 18:14

4 Answers 4

0
from collections import deque

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!?. "

msg = 'Hello World'
key = int(input()) #3

new_alphabet = deque([i for i in alphabet])
new_alphabet.rotate(key)

new_message_list = [new_alphabet[alphabet.index(m)] for m in msg]
print("".join(new_message_list))  # Ebiil!Tloia
Sign up to request clarification or add additional context in comments.

1 Comment

in a regular caesars cipher the shift direction is from left to right for a positiv number as key. deque.rotate(n) is shifting in the other direction from right to left. to change shift direction, place a - in front of the key numer
0

Hello, maybe one of these 2 Solutions will help you:

# Solution 1 using the ASCII table as base:
msg = 'Hello World'
key = int(input())
res = ''

for letter in msg:
    res += ''.join(chr(ord(letter) + key))    # ord(letter) gets the ascii value of the letter. then adding the key and chr() trandforming back from ascii value to character

print(res)


# Solution 2 using your alphabet as base
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!?."

msg = 'Hello World'
key = int(input())
res = ''

for letter in msg:
    try:
        position = alphabet.index(letter) + key # get the index position of each letter and adding the key
        char_new = alphabet[position]
    except ValueError:  # handels cases where characters are used, that are not defined in alphabet
        char_new = ' '
    res += ''.join(char_new)  # joins the new position to the result

print(res)

Comments

0

a space has been added to the end of alphabet to handle spaces in input.

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!?. "
lst = []
msg = 'Hello World'
key = int(input('enter key number: '))

for x in msg:
      lst.append(alphabet[(alphabet.index(x) + key) % len(alphabet)])
print(''.join(lst))

Comments

0
import string

alphabet=[i for i in string.ascii_uppercase] 

key=int(input("Enter key: ")) 
msg=input("Enter plain text message:").upper()
enc=[] 

for i in msg:   
    
    if i == " " or i in string.punctuation: 
        enc.append(i)
    
    if i in alphabet:
        enc.append(alphabet[(alphabet.index(i)+key)%len(alphabet)])
        
print("Encrypted message is:"+"".join(enc)) 

1 Comment

the string.punctuation is to handle ponctuation like ()[]{}!@#$% and some other characters

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.