Reputation: 13
# Paste the text you want to encipher (or decipher)
original = input("Original text: W fowgsr am roiuvhsf wb hvs Oasfwqob")
# Declare (or guess) the offset. Positive or negative ints allowed
offset = int(input("Offset: 12"))
ciphered = ''
for c in original:
c_ascii = ord(c)
if c.isupper():
c = chr((ord(c) + offset - ord('A')) % 26 + ord('A'))
elif c.islower():
c = chr((ord(c) + offset - ord('a')) % 26 + ord('a'))
ciphered += c
# makes a new file, caesar.txt, in the same folder as this python script
with open("caesar.txt", 'w') as f:
f.write(ciphered)
""" This is some code our teacher put up to help us decrypt Caeser Cyphers, but for some reason, I still get my input as my output, any ideas as to why this doesn't work? The teacher confirmed that it worked. """ """ The shift of 12 characters for this example sentence is "i raised my daughter in the american" ( i need it to be cap sensitive.) - This code will also be decrpyting more sentences with the same shift of 12 """
Upvotes: 1
Views: 171
Reputation: 7978
I think you are running it incorrectly. It looks like you tried to add your inputs directly into the code:
original = input("Original text: W fowgsr am roiuvhsf wb hvs Oasfwqob")
....
offset = int(input("Offset: 12"))
take a look at the help for 'input'
Help on built-in function input in module builtin:
input(...) input([prompt]) -> value
Equivalent to eval(raw_input(prompt)).
so the argument to input() is the prompt, and all that text is not being taken as an input, but instead is displayed as the prompt...
Try running it from the command line and typing your inputs in at the prompts instead. That works for me when I run this.
Upvotes: 1
Reputation: 21
I'm slightly confused by the question, so this answer may be wrong, but if you want to decode the message, simply swap the + before offset to - (for each case). You should end up with this:
# Paste the text you want to encipher (or decipher)
original = input("Original text: W fowgsr am roiuvhsf wb hvs Oasfwqob")
# Declare (or guess) the offset. Positive or negative ints allowed
offset = int(input("Offset: 14"))
ciphered = ''
for c in original:
c_ascii = ord(c)
if c.isupper():
c = chr((ord(c) - offset - ord('A')) % 26 + ord('A'))
elif c.islower():
c = chr((ord(c) - offset - ord('a')) % 26 + ord('a'))
ciphered += c
# makes a new file, caesar.txt, in the same folder as this python script
with open("caesar.txt", 'w') as f:
f.write(ciphered)
This decodes the message, you can just add an option when the computer asks user whether to encode or decode. Please tell me if this is what you were looking for, I'm happy to try to go over the code more times if you need.
Upvotes: 1