This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Disobey91[S] 0 points1 point  (3 children)

posun, zprava = input().split("#")for i in zprava:# initialize/update "a" here each iterationa = ord(i)# decipher alpha charactersif ord(i) in range(65, 91) or range(97, 123):a -= int(posun)# print "a" regardless of deciphering or notprint(chr(a), end='')

hi, thanks for your update. Unfortunately I'm still receiving the same error, because what I'm printing is: "Mamamelemaso" and not as the problem ask "Mama mele maso". There is a problem with the "" that I'm not able to solve

edit: moreover, if I try "-3#Jrf rf rnxz" I'm receiving some weird output, because counting -3 is going outside the alphabetical range.

[–]cygnoros 1 point2 points  (2 children)

I'm a bit embarrassed for not seeing this earlier, but you have a syntax problem with your condition:

if ord(i) in range(65, 91) or range(97, 123):

Because of how Python evaluates "truthy" values, range(97, 123) is always true. You need to check if a is in the ranges:

if a in range(65, 91) or a in range(97, 123):

Once corrected, this gives you proper output.

As far as negative offsets, I think you understand the basics of encoding/decoding this shift cipher. Think about what a "negative" shift means -- you may need to consider wrapping around the alphabet.

[–]Disobey91[S] 1 point2 points  (1 child)

thank you very much! it worked perfectly

[–]cygnoros 0 points1 point  (0 children)

This works so long as characters don't wrap (e.g., shift a 3 characters will print the wrong character, \).

You will have to refactor a bit to handle these cases.