First of hopefully many questions to come as I dive deeper into Python. I'm following along to the Giraffe Academy course and have tried to create a more complex program. It's a silly little decoder tool but it's helping me learn a lot of the python basics. I have the first part of the program below:
import string
std = string.ascii_lowercase
rvs = string.ascii_lowercase[::-1]
vwls = {'a':5,'e':1,'i':4, 'o':2, 'u':3}
def translate(phrase):
phrase = phrase.lower()
translation = ""
for letter in phrase:
if letter.isspace() == True:
translation = translation + " "
elif letter in ".!?":
translation = translation + letter
elif letter in vwls:
if str(vwls[letter]) not in translation:
translation = translation + str(vwls[letter])
vwls[letter] = vwls[letter] + 5
else:
indx = std.index(letter)
translation = translation + rvs[indx]
return translation
print(translate(input("Enter a phrase: ")))
My goal is to create the decoder for the output of the previous code. The good news is I know what is causing my problem. Because the message is a string, any vowel that goes past 2 digits wont be picked up by my code below. I need to find a way to include the next character in the string so that it can choose the correct key/value pairing in the dict. Below is what I have so far:
import string
std = string.ascii_lowercase
rvs = string.ascii_lowercase[::-1]
vwls = {'5':'a','1':'e','4':'i','2':'o','3':'u'}
def translate(phrase):
translation = ""
for letter in phrase:
if letter.isspace() == True:
translation = translation + " "
elif letter in ".!?":
translation = translation + letter
elif letter in vwls:
if letter in vwls[letter]:
translation = translation + vwls[letter]
vwls[str(int(letter) + 5)] = vwls[letter]
del vwls[letter]
else:
for index in range(len(phrase)-1):
if phrase[index] == phrase[index+1]:
translation = translation + 'poop'
else:
indx = rvs.index(letter)
translation = translation + std[indx]
return translation
print(translate(input("Enter a phrase: ")))
Any feedback is greatly appreciated! I have tried searching for similar issues and I am still learning the rules for stack overflow so I apologize if I have broken any rules regarding this question.
question from:
https://stackoverflow.com/questions/65546411/creating-the-reversal-portion-of-a-cryptography-decoder 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…