Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

My function is supposed to take a string argument as input, and return the rot-13 encoding of the input string.

def str_rot_13(string):

    c = list(string)

    for num in c:
       if ord(num) >= ord('a') and ord('z'):
          if ord(num) >=('m'):
             ord(num) -=13
          else:
             ord(num) +=13

       elif ord(num) >= ord('A') and ord('Z'):
          if ord(num) >=('M'):
             ord(num) -=13
          else:
             ord(num) +=13

    z += chr(ord(num))
    return z

It's giving me a "can't assign to function call" error. I'm not sure what I'm doing wrong.

Edit: Finally got it to work! Thanks.

The solution:

 if ord(num) >= ord('a') and ord('z'):
       if ord(num) >=('m'):
         k+= chr(ord(num)-13)
      else:
         k+= chr(ord(num)+13)

    elif ord(num) >= ord('A') and ord('Z'):
       if ord(num) >=('M'):
          k+= chr(ord(num)-13)
       else:
          k+= chr(ord(num)+13)

 return k
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
161 views
Welcome To Ask or Share your Answers For Others

1 Answer

What you're doing wrong is, you're assigning to a function call! E.g:

ord(num) -=13

you're assigning to the function call ord(num) -- and, you can't do it.

What you actually want to do is presumably:

num = chr(ord(num) - 13)

and so on.

Of course, you'll still have problems appending to z unless you define z in a part of the code you chose not to show us. Hard to help debug code you choose to hide from us, of course.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...