0

I'm stuck in solving the following exercise:

"*From the standard input you will get an integer n and a message encrypted by Caesar cipher (i.e. shifted by n letters down the alphabet). Decrypt the message and print it.

Hint: Functions chr a ord, function print with parameter end=''.

Try Also:
-3#Jrf rf rnxz*"
**Sample Input:
5#Rfrf rjqj rfxt**
**Sample Output:
Mama mele maso**

The code I've wrote is the following:

posun, zprava = input().split("#")
for i in zprava:
 a = ord(i) - int(posun)
 print(chr(a), end='')

But except for having the requested output, the exercise is tagged as wrong. Any suggestion?

gog
11.4k2 gold badges29 silver badges42 bronze badges
asked Oct 10, 2022 at 8:38
9
  • Try adding print() at the end to flush stdout. Commented Oct 10, 2022 at 8:41
  • 2
    if you substract 5 from ord('f') you end up with ord('a') -> good. But think what happens if you substract 5 from ord('b')? you have to keep in the a-z or A-Z range. Commented Oct 10, 2022 at 8:41
  • 2
    In addition, to what @SembeiNorimaki wrote, what happens if you subtract 5 from ord(' ')? Commented Oct 10, 2022 at 8:42
  • 1
    Yes, if you do ord("a") - 1 you want to end up in z, but in the ascii table the character before a is not z. You will need some code to check that your value keep in the range a-z. Similarly, if you use negative Cesar offsets, then you might end up with characters "above" z, so you want also to consider cases like ord("z") + 1. In addition your code also uses capital letters, so you have two ranges to consider: a-z and A-Z Commented Oct 10, 2022 at 8:59
  • 1
    you should update your question with your code, not write a comment. I'd recommend you take a look at a python tutorial to get familiarized with the language, and maybe start with an easier exercise if this one is giving you problems. if i in string.ascii_lowercase or string.ascii_uppercase is not doing an or the way you think, you should instead do if i in string.ascii_lowercase or i in string.ascii_uppercase you could start with checking how the syntax for boolean operations work. Commented Oct 10, 2022 at 9:39

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.