Reputation: 27
I have a Hawaiian Word Pronouncer I have to code. Basically, it takes a word such as Aloha and turns it into Ah-loh-hah. For certain strings, Values I'm recieving outputs that I don't want. 'Keiki' should be 'kay-kee' but instead I'm getting 'Kayee-kee'. Another example is 'Maui' which should be 'Mow-ee' but instead I'm getting 'Mowooeyee'.
I think there is something wrong with the vowel and pair branches since it is taking in the two letters and checking them even if one of them is currently used in pair, but I don't know how to implement a fix. Do you guys have any suggestions?
execute = True
answer = []
word = str(input('Enter a hawaiian word: ')).lower()
vowels = {'a':'ah', 'e':'eh', 'i':'ee', 'o':'oh', 'u':'oo'}
pairs = {'ai':'eye', 'ae':'eye', 'ao':'ow', 'au':'ow', 'ei':'ay', 'eu':'eh-oo', 'iu':'ew', 'oi':'oy', 'ou':'ow', 'ui':'ooey'}
x = 0
while x < len(word):
valid = ['a', 'e', 'i', 'o', 'u', 'p', 'k', 'h', 'l', 'm', 'n', 'w', "`", ' ', "'"]
first = str(word[x])
#print('first '+first)
if first not in valid:
print(f'Invalid word, {word[x]} is not a valid hawaiian character.')
execute = False
break
else:
try:
second = word[x+1]
except IndexError:
second = None
pair = pairs.get(str(first + second)) if second != None else None
if first in vowels:
if pair:
answer.append(pair)
elif (first == 'i' or 'e') and second == 'w':
answer.append(vowels[first]+'-v')
else:
answer.append(vowels[first])
if second is not None and second != "'" and second != ' ':
answer.append('-')
else:
if first != 'w':
answer.append(first)
x+=1
if execute:
print(f'{word.upper()} is pronounced {"".join(answer).capitalize()}')
Upvotes: 1
Views: 82
Reputation: 181
You are incrementing your loop by 1 regardless of whether you are evaluating a single vowel or a pair, this is some vowels/pairs to be evaluated twice. Try changing your code so that if you evaluate a vowel, you increment by 1, and if you evaluate a pair then you increment by 2.
Upvotes: 1