Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions projects/017-caesar-cipher/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

user_message = input('Write a message: ').lower()
user_shift = int(input('supply a shift amount: '))
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

for letter in user_message:
if letter not in alphabet:
print(letter)

else:
position = alphabet.index(letter)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what changes if you use enumerate func in the for loop instead of index method? do you understand what you optimize if you use enumerate in the for loop?

new_position = position + user_shift

if new_position >= len(alphabet):
restart = new_position - len(alphabet)
cipher = alphabet[restart]

else:
cipher = alphabet[new_position]

print(cipher)