caesar-cipher.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
  2. def en_decode(d, t, s):
  3. abc = alphabet
  4. if d == "decode":
  5. abc.reverse()
  6. output = ""
  7. for letter in t:
  8. if letter not in abc:
  9. output += letter
  10. else:
  11. index = abc.index(letter) + s
  12. if index >= len(abc):
  13. index = index % len(abc)
  14. output += abc[index]
  15. print(output)
  16. while True:
  17. direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
  18. if direction != "encode" and direction != "decode":
  19. print("Go away! You are idiot")
  20. text = input("Type your message:\n").lower()
  21. shift = int(input("Type the shift number:\n"))
  22. en_decode(direction, text, shift)
  23. is_continue = input("Type 'y' if you want another encription. Or 'n' if not:\n").lower()
  24. if is_continue != "y":
  25. break
  26. #TODO-1: Create a function called 'encrypt' that takes the 'text' and 'shift' as inputs.
  27. #TODO-2: Inside the 'encrypt' function, shift each letter of the 'text' forwards in the alphabet by the shift amount and print the encrypted text.
  28. #e.g.
  29. #plain_text = "hello"
  30. #shift = 5
  31. #cipher_text = "mjqqt"
  32. #print output: "The encoded text is mjqqt"
  33. ##HINT: How do you get the index of an item in a list:
  34. #https://stackoverflow.com/questions/176918/finding-the-index-of-an-item-in-a-list
  35. ##🐛Bug alert: What happens if you try to encode the word 'civilization'?🐛
  36. #TODO-3: Call the encrypt function and pass in the user inputs. You should be able to test the code and encrypt a message.