Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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', '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']
- direction = input("Type 'encode' to encrypt, type 'decode' to decrypt: ").lower()
- text = input("Type your message: ").lower()
- shift = int(input("Type the shift number:"))
- #TODO-1: Create a function called 'encrypt' that takes the 'text' and 'shift' as inputs.
- #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.
- #e.g.
- #plain_text = "hello"
- #shift = 5
- #cipher_text = "mjqqt"
- #print output: "The encoded text is mjqqt"
- ##HINT: How do you get the index of an item in a list:
- #https://stackoverflow.com/questions/176918/finding-the-index-of-an-item-in-a-list
- #TODO-3: Call the encrypt function and pass in the user inputs. You should be able to test the code and encrypt a message.
- #TODO-1: Create a different function called 'decrypt' that takes the 'text' and 'shift' as inputs.
- #TODO-2: Inside the 'decrypt' function, shift each letter of the 'text' *backwards* in the alphabet by the shift amount and print the decrypted text.
- #e.g.
- #cipher_text = "mjqqt"
- #shift = 5
- #plain_text = "hello"
- #print output: "The decoded text is hello"
- #TODO-3: Check if the user wanted to encrypt or decrypt the message by checking the 'direction' variable. Then call the correct function based on that 'drection' variable.
- # You should be able to test the code to encrypt *AND* decrypt a message.
- def encrypt(text_, shift_):
- cipher_text = ""
- for letter in text_:
- position = alphabet.index(letter)
- new_position = position + shift
- cipher_text += alphabet[new_position]
- print(f"The encoded text is: \n{cipher_text}")
- def decrypt(text_, shift_):
- cipher_text = ""
- for letter in text_:
- position = alphabet.index(letter)
- new_position = position - shift_
- cipher_text += alphabet[new_position]
- print(cipher_text)
- if direction == "encode":
- encrypt(text, shift)
- if direction == "decode":
- decrypt(text, shift)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement