Python Caesar Cipher Using Turkish Alphabet Array
I would reorganize your code, to make it look more like this:
def ceasercypher(steps, word):
alpLower, alpUpper = ["a", "b", "c", "ç", "d", "e", "f", "g", "ğ", "h", "ı", "i", "j", "k", "l", "m", "n", "o", "ö", "p", "r",
"s", "ş", "t", "u", "ü", "v", "y", "z"], ["A", "B", "C", "Ç", "D", "E", "F", "G", "Ğ", "H", "I", "İ", "J", "K", "L", "M", "N", "O", "Ö", "P", "R",
"S", "Ş", "T", "U", "Ü", "V", "Y", "Z"]
answer = []
for letter in word:
if letter.isupper():
answer.append(alpUpper[int((steps + alpUpper.index(letter))%len(alpUpper))])
if letter.islower():
answer.append(alpLower[int((steps + alpLower.index(letter))%len(alpLower))])
return "".join(answer)
print(ceasercypher(0, "ağV"))
just for fun, you can always try to write it short! but if you work in a team or, want to be able to understand it later this is bad practice. I call it "programmers useless flex, and root of a lot of bugs"
def ceasercypher(steps, word):
alpLower, alpUpper = ["a", "b", "c", "ç", "d", "e", "f", "g", "ğ", "h", "ı", "i", "j", "k", "l", "m", "n", "o", "ö", "p", "r",
"s", "ş", "t", "u", "ü", "v", "y", "z"], ["A", "B", "C", "Ç", "D", "E", "F", "G", "Ğ", "H", "I", "İ", "J", "K", "L", "M", "N", "O", "Ö", "P", "R",
"S", "Ş", "T", "U", "Ü", "V", "Y", "Z"]
return "".join([alpUpper[int((steps + alpUpper.index(letter))%len(alpUpper))] if letter.isupper() else alpLower[int((steps + alpLower.index(letter))%len(alpLower))] if letter.islower() else " [INVALIDCHAR] " for letter in word])
print(ceasercypher(1, "ağVZ%K%"))
Actually, you don't need to loop through each element in the list to know what it's index is, you can just get the index with the element using index().
animals = ['cat', 'dog', 'giraffe', 'elephant', 'monkey']
# Will print the index of 'giraffe' in animals
print(animals.index('giraffe'))
Output:
Applying it to your code:
for c in text:
if c in alpUpper:
i = alpUpper.index(c)
c = alpUpper[(i + shift) % 29]
b += 1
encrypt.append(c)
else:
b == 0