0% found this document useful (0 votes)
7 views2 pages

T5 Escano

This document outlines a Python program that allows users to encode and decode messages using a shift cipher. Users can input a message and a shift value, and the program will output the encoded or decoded text accordingly. The program continues to prompt the user for further actions until they choose to exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

T5 Escano

This document outlines a Python program that allows users to encode and decode messages using a shift cipher. Users can input a message and a shift value, and the program will output the encoded or decoded text accordingly. The program continues to prompt the user for further actions until they choose to exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

# Start with a variable to control the loop

continue_program = True

# Start the loop


while continue_program:
# Ask if the user wants to encode or decode
choice = input("Type 'encode' to encrypt, type 'decode' to decrypt: ")

# If the user chooses to encode


if choice == 'encode':
# Ask the user to input the message
message = input("Type your message: ")

# Ask the user to input the shift value


shift = int(input("Type the shift number: "))

# Initialize an empty string to hold the encoded message


encoded_message = ""

# Define the alphabet for lowercase and uppercase


lowercase = 'abcdefghijklmnopqrstuvwxyz'
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

# Loop through each character in the message


for letter in message:
# If the character is a lowercase letter
if letter in lowercase:
# Find its new position in the alphabet and wrap around using
modulo
new_index = (lowercase.index(letter) + shift) % 26
encoded_message += lowercase[new_index]
# If the character is an uppercase letter
if letter in uppercase: # Removed elif and used if here
# Find its new position in the alphabet and wrap around using
modulo
new_index = (uppercase.index(letter) + shift) % 26
encoded_message += uppercase[new_index]
else:
# If it's not a letter, just add it as it is
encoded_message += letter

# Print the encoded message


print("The encoded text is", encoded_message)

# If the user chooses to decode


if choice == 'decode': # Changed elif to if here as well
# Ask the user to input the message
message = input("Type your message: ")

# Ask the user to input the shift value


shift = int(input("Type the shift number: "))

# Initialize an empty string to hold the decoded message


decoded_message = ""

# Define the alphabet for lowercase and uppercase


lowercase = 'abcdefghijklmnopqrstuvwxyz'
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# Loop through each character in the message
for letter in message:
# If the character is a lowercase letter
if letter in lowercase:
# Find its new position in the alphabet (reversed) and wrap around
using modulo
new_index = (lowercase.index(letter) - shift) % 26
decoded_message += lowercase[new_index]
# If the character is an uppercase letter
if letter in uppercase: # Again removed elif and used if here
# Find its new position in the alphabet (reversed) and wrap around
using modulo
new_index = (uppercase.index(letter) - shift) % 26
decoded_message += uppercase[new_index]
else:
# If it's not a letter, just add it as it is
decoded_message += letter

# Print the decoded message


print("The decoded text is", decoded_message)

# Ask if the user wants to encode or decode again


repeat = input("Do you want to decode or encode again? (yes/no): ")

# If the user doesn't want to continue, stop the loop


if repeat.lower() != 'yes':
print("Goodbye!")
continue_program = False # Set to False to exit the loop

You might also like