Advertisement
makispaiktis

Caesar's Code

Apr 22nd, 2019 (edited)
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. alphabet = "abcdefghijklmnopqrstuvwxyz"
  2. numbers = "1234567890"
  3. word = input("Enter the word you want me to cipher: ")
  4. offset = int(input("Enter offset (must be an integer): "))
  5.  
  6. try:
  7.     print()
  8.     print("Before ciphering: " + word)
  9.     list = []                                          # There, I will add the characters of the converted word
  10.  
  11.  
  12.     for ch in word:                                    # 1st scan: ch = each character of the variable "word"\
  13.         counter = 0                                             # Create a counter: it will be used to count the FAILS in the
  14.                                                                 # comparison between the "ch" and all the letters in "alphabet"
  15.         for i in range(len(alphabet)):                 # 2nd scan: i = index in string "alphabet"
  16.  
  17.             if ch.lower() == alphabet[i]:              # Here, I see if "ch" converted to lowerCase is equal to alphabet[i]
  18.  
  19.                 if ch == ch.lower():                                                 # If "ch" is lowerCase,
  20.                     list.append(alphabet[(i + offset) % (len(alphabet))])            #I add the appropriate letter in list
  21.                     break
  22.                 else:                                                               # If "ch" is upperCase,
  23.                     letter = alphabet[(i + offset) % (len(alphabet))]               #I add the "letter" ("letter" is lowerCase) after I convert it into upperCase
  24.                     list.append(letter.upper())
  25.                     break
  26.  
  27.             elif i <= len(numbers)-1 and ch == numbers[i]:                          # If "ch" = numbers[i] and i <= 9 (avoid overflow)
  28.                 list.append(numbers[ (i + offset) % len(numbers) ])
  29.                 break
  30.  
  31.             elif ch == " ":                             # This is the case, when I find a "spacebar" character
  32.                 list.append(" ")                        # Malista, edw ekteleitai MONO mia fora h 2h for
  33.                 break
  34.  
  35.             else:                                       # Here, the 2nd loop goes, when "ch" !=  alphabet[i] and "ch" != " "
  36.                 counter += 1                            # I increase by 1 the counter in case of failure in comparison between ch and alphabet[i] or spacebar
  37.  
  38.         if counter == len(alphabet):                    # Gia na ftasei edw shmainei oti **DEN EGINE KANENA BREAK** kai episis o **counter = 26**, ara 26 failures ---> o "ch" einai kapoios allos character
  39.             list.append(ch)
  40.  
  41.     print("After  ciphering: " + ''.join(map(str, list)))
  42.  
  43. except ValueError:
  44.     print("Value Error!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement