# Computing sceince

#1
So I am not use to python and have been given a quick task, a caesar cipher. I wrote that and they want a key word, which is the infromation needed to trasnform the plaintext into encrypted mesage and is also used by the recipient to decrypt the message. But I dont know how to write that in, my code is below and should be indented as it is.
def caesar(plain_text, shift):
cipherText = ''
for ch in plain_text:
stayInAlphabet = ord(ch) + shift
if ch.islower():
if stayInAlphabet > ord('z'):
stayInAlphabet -= 26
elif stayInAlphabet < ord('a'):
stayInAlphabet += 26
elif ch.isupper():
if stayInAlphabet > ord('Z'):
stayInAlphabet -= 26
elif stayInAlphabet < ord('A'):
stayInAlphabet += 26
finalLetter = chr(stayInAlphabet)
cipherText += finalLetter
print(cipherText)
return cipherText

selection = input("Would you like to decrypt or encrypt a message? Please enter 'E' or 'D': ")
if selection == 'encrypt':
Cipher = input("Enter text to Cipher: ")
caesar(Cipher, offset)

else:
Cipher = input("What is your Cipher? ")
offset = ((int(input("What is your offset? "))) % 26) * -1
caesar(Cipher, offset)
0
#2
Ok, so it did not indent but here is a picture.
0
4 years ago
#3
Could you explain which part of this you're stuck on?

Also, if you need to post example code, then I'd strongly suggest pasting it into somewhere like https://pyfiddle.io because this forum is poor at preserving formatting and indentation unfortunately!

The code you've pasted looks like it just handles the Caesar cypher and not a Keyword cypher, so here's a wiki page which explains how a Keyword cypher works: https://en.wikipedia.org/wiki/Keyword_cipher

The Wikipedia page uses an example keyword of KRYPTOS, and has a substitution table which looks like:

Code:
```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

K R Y P T O S A B C D E F G H I J L M N Q U V W X Z```
Perhaps you could use a string in Python to help simplify your algorithm, similar to these above, to help you find substitute letters.

For example:
Code:
```substitutions = "KRYPTOSABCDEFGHIJLMNQUVWXZ"
position = ord('M') - ord('A')   # Position for 'M' in the substitutions string

# Get the substitute character for 'M'
substitute = substitutions[position]```
Or reverting back to the original:
Code:
```substitutions = "KRYPTOSABCDEFGHIJLMNQUVWXZ"
position = substitutions.find('J')  # Find character 'J'

# Get the original character from 'J'
theOriginal  = chr(ord('A') + position)```
Remember that the Keyword cypher relies on the uniqueness of each character in the keyword, so If you need to read the keyword from the user, then I'd suggest you also validate that it doesn't contain any duplicate characters.

Good luck!
0
X

new posts
Back
to top
Latest
My Feed

### Oops, nobody has postedin the last few hours.

Why not re-start the conversation?

see more

### See more of what you like onThe Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

### Poll

Join the discussion

#### Were exams easier or harder than you expected?

Easier (17)
27.87%
As I expected (17)
27.87%
Harder (24)
39.34%
Something else (tell us in the thread) (3)
4.92%