0
#Caesar Cipher Muntanga FinalMessage="" Mode="" MessageNumber="" while Mode!="encrypt" and Mode!="decrypt": Mode = input("Do you wish to encrypt or decrypt a message?") Mode = Mode.lower() Message = input("Please enter the message you wish to encrypt:") Message = Message.upper() Key = input("Please enter your key:") Key = int(Key) if Key < 1: print("This program does not encrypt backwards. Please restart and enter a key between 1-25") raise SystemExit elif Key > 25: print("You are looping round the alphabet. Please restar the program and enter a key between 1-25") raise SystemExit if Mode == "encrypt": for i in range(0, len(Message)): if MessageNumber.isalpha(): if MessageNumber.isupper(): MessageNumber = ord(Message[i]) MessageNumber = MessageNumber + Key if MessageNumber > 90: MessageNumber = MessageNumber - 26 FinalMessage = FinalMessage + chr(MessageNumber) elif MessageNumber.islower(): MessageNumber = ord(Message[i]) MessageNumber = MessageNumber + Key if MessageNumber > 122: MessageNumber = MessageNumber - 26 FinalMessage = FinalMessage + chr(MessageNumber) print("Your encrypted message is:") print(FinalMessage) raise SystemExit 

My cipher outputs blank message (check image) Image of failed test

2 Answers 2

1

since someone else pointed out your problem i will instead show you an easier way to do ceasar cipher shifts

from string import ascii_lowercase as lc,ascii_uppercase as uc,maketrans as trans def encode(m,rotation_amount): alphabet = lc+uc rotated_alphabet = (lc[rotation_amount:]+lc[:rotation_amount] +uc[rotation_amount:]+uc[:rotation_amount]) return m.translate(trans(alphabet,rotated_alphabet)) print encode("how are YoU") 
Sign up to request clarification or add additional context in comments.

Comments

1

You're using MessageNumber before it has any meaningful value. You initialized MessageNumber with an empty string at the beginning of the script, but since this variable has to be set for each character of the input string, you don't need to initialize it "globally".

When you remove the initialization, you will quickly see a meaningful error saying that MessageNumber is NoneType in the line if MessageNumber.isalpha():.

Additionally, you can deduplicate some lines by moving those calls one level higher:

if Mode == "encrypt": for i in range(0, len(Message)): MessageChar = Message[i] MessageNumber = ord(MessageChar) if MessageChar.isalpha(): MessageNumber = MessageNumber + Key if MessageChar.isupper(): if MessageNumber > 90: MessageNumber = MessageNumber - 26 elif MessageChar.islower(): if MessageNumber > 122: MessageNumber = MessageNumber - 26 FinalMessage = FinalMessage + chr(MessageNumber) 

3 Comments

now it says "f MessageNumber.isalpha(): AttributeError: 'int' object has no attribute 'isalpha'"
I've introduced another variable that holds the character in addition to the variable that holds the character code. There are of course a million different ways to solve this.
It's pretty clear. It says that MessageNumber is of type int and instances of that type don't have a isalpha function on them. Strings have such functions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.