day 8 - easy !
This commit is contained in:
41
008/task.py
Normal file
41
008/task.py
Normal file
@ -0,0 +1,41 @@
|
||||
|
||||
|
||||
def shift_string(string, offset):
|
||||
alphabet = "abcdefghijklmnopqrstuvwxyz"
|
||||
max_offset = len(alphabet) -1
|
||||
min_offset = 0
|
||||
|
||||
output = ""
|
||||
|
||||
for letter in string.lower():
|
||||
letter_index = alphabet.find(letter)
|
||||
if letter_index == -1:
|
||||
result = letter
|
||||
else:
|
||||
if letter_index + offset > max_offset:
|
||||
result = alphabet[letter_index - len(alphabet) + offset]
|
||||
elif letter_index + offset < 0:
|
||||
result = alphabet[letter_index + len(alphabet) + offset]
|
||||
else:
|
||||
result = alphabet[letter_index + offset]
|
||||
|
||||
output += result
|
||||
|
||||
print(f"Your result is: {output}")
|
||||
|
||||
|
||||
b_continue = "yes"
|
||||
|
||||
while b_continue == "yes":
|
||||
action = input("What do you want to do? \"encode\" or \"decode\"?\n")
|
||||
message = input("Type your message.\n")
|
||||
offset = int(input("What is your offset?\n"))
|
||||
|
||||
if action == "encode":
|
||||
shift_string(message, offset)
|
||||
elif action == "decode":
|
||||
shift_string(message, -offset)
|
||||
else:
|
||||
print("Invalid action - Try again")
|
||||
|
||||
b_continue = input("Do you want to continue? \"yes\" or \"no\"\n")
|
||||
Reference in New Issue
Block a user