In: Computer Science
This lesson's Group Activities are:
Use python and use double strings
Talk like a Pirate! In this activity you are to create an English to Pirate Translator. Users should input a phrase and your program should translate it into pirate speak. A few rules:
1. Certain words need to be converted:
2. You need to obey capitalization and punctuation: if the word is capitalized, then its translation should also be capitalized. If the sentence ends in a period, then the translation should end in a period. To keep punctuation simple, you can stick to coding for commas and periods, and ignore all the rest.
3. Pirates like the word "arr". For fun, randomly insert it into your translation.
4. Remember to use all the tools in your arsenal, especially mainline logic, functions, loops, and error handling.
import random
def alp_size(str):
count=0
for i in str:
if (i.isalpha()) == True:
count+=1
return count
def assign(str):
if(("hello" in str)&(alp_size(str)==5)):
if(len(str)>5):
if((str[5]==".")|(str[5]==",")):
return "ahoy"+str[5]
return "ahoy"
if(("HELLO" in str)&(alp_size(str)==5)):
if(len(str)>5):
if((str[5]==".")|(str[5]==",")):
return "AHOY"+str[5]
return "AHOY"
if(("Hello" in str)&(alp_size(str)==5)):
if(len(str)>5):
if((str[5]==".")|(str[5]==",")):
return "Ahoy"+str[5]
return "Ahoy"
if(("hi" in str)&(alp_size(str)==2)):
if(len(str)>2):
if((str[2]==".")|(str[2]==",")):
return "yo-ho-ho"+str[2]
return "yo-ho-ho"
if(("HI" in str)&(alp_size(str)==2)):
if(len(str)>2):
if((str[2]==".")|(str[2]==",")):
return "YO-HO-HO"+str[2]
return "YO-HO-HO"
if(("Hi" in str)&(alp_size(str)==2)):
if(len(str)>2):
if((str[2]==".")|(str[2]==",")):
return "Yo-Ho-Ho"+str[2]
return "Yo-Ho-Ho"
if(("my" in str)&(alp_size(str)==2)):
if(len(str)>2):
if((str[2]==".")|(str[2]==",")):
return "me"+str[2]
return "me"
if(("MY" in str)&(alp_size(str)==2)):
if(len(str)>2):
if((str[2]==".")|(str[2]==",")):
return "ME"+str[2]
return "ME"
if(("My" in str)&(alp_size(str)==2)):
if(len(str)>2):
if((str[2]==".")|(str[2]==",")):
return "Me"+str[2]
return "Me"
if(("friend" in str)&(alp_size(str)==6)):
if(len(str)>6):
if((str[6]==".")|(str[6]==",")):
return "bucko"+str[6]
return "bucko"
if(("FRIEND" in str)&(alp_size(str)==6)):
if(len(str)>6):
if((str[6]==".")|(str[6]==",")):
return "BUCKO"+str[6]
return "BUCKO"
if(("Friend" in str)&(alp_size(str)==6)):
if(len(str)>6):
if((str[6]==".")|(str[6]==",")):
return "Bucko"+str[6]
return "Bucko"
if(("sir" in str)&(alp_size(str)==3)):
if(len(str)>3):
if((str[3]==".")|(str[3]==",")):
return "matey"+str[3]
return "matey"
if(("SIR" in str)&(alp_size(str)==3)):
if(len(str)>3):
if((str[3]==".")|(str[3]==",")):
return "MATEY"+str[3]
return "MATEY"
if(("Sir" in str)&(alp_size(str)==3)):
if(len(str)>3):
if((str[3]==".")|(str[3]==",")):
return "Matey"+str[3]
return "Matey"
if(("where" in str)&(alp_size(str)==5)):
if(len(str)>5):
if((str[5]==".")|(str[5]==",")):
return "whar"+str[5]
return "whar"
if(("WHERE" in str)&(alp_size(str)==5)):
if(len(str)>5):
if((str[5]==".")|(str[5]==",")):
return "WHAR"+str[5]
return "WHAR"
if(("Where" in str)&(alp_size(str)==5)):
if(len(str)>5):
if((str[5]==".")|(str[5]==",")):
return "Whar"+str[5]
return "Whar"
if(("is" in str)&(alp_size(str)==2)):
if(len(str)>2):
if((str[2]==".")|(str[2]==",")):
return "be"+str[2]
return "be"
if(("IS" in str)&(alp_size(str)==2)):
if(len(str)>2):
if((str[2]==".")|(str[2]==",")):
return "BE"+str[2]
return "BE"
if(("Is" in str)&(alp_size(str)==2)):
if(len(str)>2):
if((str[2]==".")|(str[2]==",")):
return "Be"+str[2]
return "Be"
if(("the" in str)&(alp_size(str)==3)):
if(len(str)>3):
if((str[3]==".")|(str[3]==",")):
return "th'"+str[3]
return "th'"
if(("THE" in str)&(alp_size(str)==3)):
if(len(str)>3):
if((str[3]==".")|(str[3]==",")):
return "TH'"+str[3]
return "TH'"
if(("The" in str)&(alp_size(str)==3)):
if(len(str)>3):
if((str[3]==".")|(str[3]==",")):
return "Th'"+str[3]
return "Th'"
if(("there" in str)&(alp_size(str)==5)):
if(len(str)>5):
if((str[5]==".")|(str[5]==",")):
return "thar"+str[5]
return "thar"
if(("THERE" in str)&(alp_size(str)==5)):
if(len(str)>5):
if((str[5]==".")|(str[5]==",")):
return "THAR"+str[5]
return "THAR"
if(len(str)>5):
if((str[5]==".")|(str[5]==",")):
return "Thar"+str[5]
return "Thar"
if(("you" in str)&(alp_size(str)==3)):
if(len(str)>3):
if((str[3]==".")|(str[3]==",")):
return "ye"+str[3]
return "ye"
if(("YOU" in str)&(alp_size(str)==3)):
if((str[3]==".")|(str[3]==",")):
return "YE"+str[3]
return "YE"
if(("You" in str)&(alp_size(str)==3)):
if((str[3]==".")|(str[3]==",")):
return "Ye"+str[3]
return "Ye"
if(str.endswith("ing")) :
return (str[0:-3]+"in'")
if(str.endswith("ing.")) :
return (str[0:-4]+"in'.")
if(str.endswith("ing,")) :
return (str[0:-4]+"in',")
return str;
def main():
try:
phrase = input("Enter your phrase: ")
array=phrase.split()
phrase=""
i=0
for x in array:
phrase+=assign(x)+" "
i=random.randint(0,1)
if(i==1) :
phrase+= "arr "
print(phrase)
except:
print("Translation went wrong")
if __name__ == "__main__":
main()
OUTPUT: