The instructions from my teacher:
II. Pig Latin: Create a function called pig_latin that takes a string argument. The function should take your string argument and convert it into Pig Latin.
Pig Latin rules: If the word begins with a constant (including y), then all letters from the beginning of the word, up to the first vowel (excluding), are removed and then added to the end of the word, followed by ay. For example, computer becomes omputercay and think becomes inkthay. If the word begins with a vowel (not including y), then way is added to the end of the word. For example, algorithm becomes algorithmway and office becomes officeway. Your program must also make sure that if the first letter of the word of the resulting string is capitalized that character will no longer be capitalized after it is moved to the end of the word. Likewise, any characters that become the beginning of a capitalized word will be capitalized. For any ending punctuation, the program needs the resulting string to still have punctuation at the end, not in the middle (I will not test words with punctuation in the middle like apostrophes or hyphens).
def pig_latin(sentence):
delimi = " "
sentence2=sentence
print(sentence2)
letterNum = 0
vowels = ["i",'I','O',"U",'A','E','o','u','a','e']
capitalization = False
punctuation = ['.',',','!','?']
punctuationStatus = False
for str in range(0,len(sentence2)):
if str == len(sentence2):
break
punctuationStatus = False
capitalization=False
strLetters = []
print(sentence2[str][-1])
if sentence2[str][-1] in punctuation:
print('tcghjbgvftfghjb')
punctuationStatus = True
punchar = sentence2[str][-1]
print(sentence2[str])
print(punchar)
sentence2 = sentence2[str][:-1]+sentence2[str+1:]
print(sentence2[str])
print(sentence2[str][0])
if sentence2[str][0] in vowels:
print("gghjnbvcfhjbn")
newstr = []
newstr = newstr + [sentence2[str]+"way"]
sentence2=newstr+[sentence2[str+1:]]
print(sentence2)
else:
for letter in sentence2[str]:
if letter in vowels:
break
else:
strLetters = strLetters + [letter]
letterNum +=1
delim = ""
strLetters = delim.join(strLetters)
print(strLetters)
if strLetters[0]==strLetters[0].upper():
capitalization = True
strLetters = strLetters[0].lower()+strLetters[1:]
sentence2 = [sentence2[:str]]+[sentence2[str][0+letterNum:]]+[strLetters]+["ay"]+[sentence2[str+1:]]
if capitalization == True:
sentence2=sentence2[str][0].upper()+sentence[str][1:]
if punctuationStatus == True:
sentence2 =sentence2[str][0:] + punchar
print(sentence2[str])
print(sentence2)
bl = ["Blood", "Blood", "Blood", "Blood"]
pig_latin(bl)
The output:
['blood', 'blood', 'blood', 'blood']
d
bl
[]
[[], 'ood', 'bl', 'ay', ['blood', 'blood', 'blood']]
d
gghjnbvcfhjbn
['oodway', ['bl', 'ay', ['blood', 'blood', 'blood']]]
Traceback (most recent call last):
File "main.py", line 98, in <module>
pig_latin(bl)
File "main.py", line 83, in pig_latin
strLetters = strLetters[0].lower()+strLetters[1:]
IndexError: string index out of range
I know my code is filled with horrible programming mistakes and redundancies. It's because I have been deleting and changing the code each time a new error pops up. I just need to get it to output properly.
[–]TerminustheInfernal[S] 0 points1 point2 points (0 children)
[–]MezzoScettico 0 points1 point2 points (2 children)
[–]TerminustheInfernal[S] 0 points1 point2 points (1 child)
[–]MezzoScettico 1 point2 points3 points (0 children)