Currently, I have this and it's working for all cases:
import math
import os
import random
import re
import sys
def caesarCipher(s, k):
st = []
for i in range(0,len(s)):
if 65<=ord(s[i])<=90:
temp = ord(s[i])+k%26
if (temp>90):
temp-=26
st.append(chr(temp))
elif 97<=ord(s[i])<=122:
temp = ord(s[i])+k%26
if (temp>122):
temp-=26
st.append(chr(temp))
else: st.append(s[i])
return ''.join(st)
if __name__ == '__main__':
s = input()
k = int(input())
result = caesarCipher(s, k)
I'm not sure if the multiple if
loops reduce readability. Is there a cleaner way to do this?
Sample I/O:
Input: s = middle-Outz , k=2
Output: s = okffng-Qwvb
Input: s = Always-Look-on-the-Bright-Side-of-Life ; k=5
Output: s = Fqbfdx-Qttp-ts-ymj-Gwnlmy-Xnij-tk-Qnkj