Skip to content

Instantly share code, notes, and snippets.

@WillBishop
Created April 19, 2019 02:19
Show Gist options
  • Save WillBishop/313182455745730bfdb6be294189948a to your computer and use it in GitHub Desktop.
Save WillBishop/313182455745730bfdb6be294189948a to your computer and use it in GitHub Desktop.
import re #This is regex, it's kind of advanced for a beginner project to be honest
from string import punctuation #Punctuation is an array of all English puncutation marks, we use this to not translate anything that is punctuation
def convert_to_pyglatin(user_input): #Defining a function which takens one input parameter
words = re.findall(r"[\w']+|[.,!?;]", user_input) #regexr.com/4cim0 <- Look at this for an explanation
final_string = "" #I create an empty string where we add our words to in our loop
for word in words: #This is a for-loop, it'll go over every element in our words array defined above. the `word` part of it could be anything, it's like defining a variable
if word not in punctuation: #If the word isn't in our list of punctuation marjs, we'll try and translate
first_letter = word[:1].lower() # word[:1] returns the first (1) letter of a word, if we changed it to two we'd get the first two letters
word_without_first_letter= word[1:] #word[1:] does a similar thing, but returns the word without the first letter
final_string += " " + word_without_first_letter + first_letter + "ay" #We then add it all back to the string
else:
final_string += word #If it's punctuation we just add it as is
return final_string #Then we return it
print(convert_to_pyglatin(input("What would you like to translate?: "))) #Here we're doing 3 things: 1) Printing any output, 2) calling our translation function, and 3) calling the input function for user input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment