Question: I cant seem to figure out what is wrong with my code when it prints out I get the words all together for example: Enter

I cant seem to figure out what is wrong with my code when it prints out I get the words all together for example:
Enter message to be encrypted: Hello World
Enter shift amount (1-25): 3
Encrypted message: KhoorZruogg
And then when I do the opposite I get this and it doesnt even have the correct letters:
Enter message to be encrypted: KhoorZruogg
Enter shift amount (1-25): 23
Encrypted message: belloqorlddd
This is my current code:
def encode(msg, shift):
newmsg =""
for ch in msg:
if ('A'<= ch and ch <='Z'):
newch = chr((ord(ch)- ord('A')+ shift %26+ ord('A')))
newmsg = newmsg + newch
elif ('a'<= ch and ch <='z'):
newch = chr((ord(ch)- ord('a')+ shift)%26+ ord('a'))
newmsg = newmsg + newch
else:
newch = ch
newmsg += newch
return newmsg
msg = input("Enter message to be encrypted: ")
shiftstr = input("Enter shift amount (1-25): ")
shift = int(shiftstr)
print("Encrypted message: "+ encode(msg, shift))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!