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
  • 10,367
  • 2
  • 24
  • 38
  • Try adding `print()` at the end to flush stdout. – Aran-Fey Oct 10 '22 at 08: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. –  Oct 10 '22 at 08:41
  • 2
    In addition, to what @SembeiNorimaki wrote, what happens if you subtract 5 from `ord(' ')`? – Jiří Baum Oct 10 '22 at 08:42
  • @SembeiNorimaki subtracting 5 to ord('b') is giving me 93 as a result; Jiří Baum, in your case is giving 27. But I don't understand what you mean. The problem is that I'm going out of the range of the alphabet? – Disobey1991 Oct 10 '22 at 08:56
  • 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 –  Oct 10 '22 at 08:59
  • Your code will fail if there is more than 1 `#` sign – mousetail Oct 10 '22 at 09:01
  • Caesar cipher usually assumes that `a-1 = z` etc, so you may want to try some modular arithmetic. – bereal Oct 10 '22 at 09:02
  • @SembeiNorimaki I've tried to define something like: for i in zprava: if i in string.ascii_lowercase or string.ascii_uppercase: if ord(i) in range(65, 91) or range(97, 123): a = ord(i) - int(posun) print(chr(a), end='') and I'm receiving as an output what I need, but is still giving me error in exercise – Disobey1991 Oct 10 '22 at 09:21
  • 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. –  Oct 10 '22 at 09:39

0 Answers0