在一定范围内进行编码和解码

时间:2019-03-21 15:58:17

标签: python

我的代码快要完成了,我只是在解码/编码的某些部分上苦苦挣扎。当我尝试用28编码ABC时,它将返回符号而不是字母。但是,它适用于小写字母。我不确定如何解决它,我正在寻求帮助。我并不是要大幅度地更改代码,而应保持原样,因为我认为我只是缺少正确的值或操作。

这是我当前的代码:

def encode_or_decode(encode_or_decode_choice, str_to_change, key):

 if encode_or_decode_choice == "encode":
    new_str = ""
    for i in range(len(str_to_change)):
        shifter = ord(str_to_change[i])
        shift1 = key
        if shifter + shift1 > 122:
            shift1 = shift1 - (122 - shifter)
            shift1 = shift1 % 26
            new_str = new_str + chr(shift1 + 96)
        elif shifter + shift1 < 90:
            shift1 = shift1 - (90 - shifter)
            shift1 = shift1 % 26
            new_str = new_str + chr(shift1 + 64)
        else:
            new_str = new_str + chr(shifter + key)
    return new_str

 elif encode_or_decode_choice == "decode":
    new_str = ""
    for i in range(len(str_to_change)):
        shifter = ord(str_to_change[i])
        shift1 = key
        if shifter + shift1 > 122:
            shift1 = shift1 - (122 - shifter)
            shift1 = shift1 % 26
            new_str = new_str + chr(shift1 + 96)
        elif shifter + shift1 > 90:
            shift1 = shift1 - (90 - shifter)
            shift1 = shift1 % 26
            new_str = new_str + chr(shift1 + 64)
        else:
            new_str = new_str + chr(shifter - key)
    return new_str
 else:
    print("Bad Input")
    encode_or_decode_test()

def encode_or_decode_test():
  desc1 = True
  while desc1 == True:
      result = encode_or_decode(str(input("Would you like to encode or decode? ")), str(input("Enter a string: ")), int(input("Enter a key (integer): ")))
      print("Encoding or decoding given word based off decision, by given key:", result)
      decisions = str(input("Would you like to encode or decode again? (y or n) "))
      if decisions == "y":
          desc1 = True
      else:
          desc1 = False


def main():
    encode_or_decode_test()

1 个答案:

答案 0 :(得分:0)

encode中的elif条件应为-

<dependency>
    <groupId>org.repodriller</groupId>
    <artifactId>repodriller</artifactId>
    <version>1.4.0</version>
</dependency>