Text Shift function in Python

后端 未结 5 427
陌清茗
陌清茗 2020-12-03 15:50

I\'m writing code so you can shift text two places along the alphabet: \'ab cd\' should become \'cd ef\'. I\'m using Python 2 and this is what I got so far:

         


        
5条回答
  •  借酒劲吻你
    2020-12-03 16:19

    Tried with Basic python. may useful for someone.

    # Caesar cipher
    import sys
    
    text = input("Enter your message: ")
    
    cipher = ''
    try:
      number = int(input("Enter Number to shift the value : "))
    except ValueError:
      print("Entered number should be integer. please re0enter the value")
      try:
        number = int(input("Enter Number to shift the value : "))
      except:
        print("Error occurred. please try again.")
        sys.exit(2)
      
    for char in text:
        if not char.isalpha():
          flag = char
        elif char.isupper():
          code = ord(char) + number
          if 64 < code <= 90:
            flag = chr(code)
          elif code > 90:
            flag = chr((code - 90) + 64)
            
        elif char.islower():
          code = ord(char) + number
          if 96 < code <= 122:
            flag = chr(code)
          elif code > 122:
            flag = chr((code - 122) + 96)
        
        else:
          print("not supported value by ASCII")
        
        cipher += flag
    
    print(cipher)
    

提交回复
热议问题