Answer:
multiple things probably
Step-by-step explanation:
Well the code is a bit confusing to read since the lack of indents but I'll try my best to interpret what you were trying to do.
By the looks of the exception you provided, it seems that the key "I" does not exist in your dictionary: "vowels_dict"
Same thing applies with the key "a" not existing in your dictionary: "consonants_dict"
This makes sense since by looking at your code since they obviously don't exist in their respective dictionaries.
I tried pasting the code into VisualStudio to get a better look at it and I tried to fix the indenting and to ensure I got it correct I provided what I indented below
"
for i in range(len(hawaiian_word)):
if hawaiian_word[i] in valid_letters:
for key in hawaiian_word:
if hawaiian_word[i] in consonants_dict:
pronunciation += consonants_dict[key]
elif hawaiian_word[0][0] in vowels_dict:
pronunciation += vowels_dict_capitalized[key]
elif hawaiian_word[i] in vowels_dict:
pronunciation += vowels_dict[key]
elif hawaiian_word[i-2] in vowels_dict:
pronunciation += vowels_no_hyphen_dict[key]
if hawaiian_word[i] in invalid_letters:
print('Invalid word, ' + hawaiian_word[i] + ' is not a valid hawaiian character.')
"
So there are a few issues I noticed, and I'm not exactly sure how to fix them since I'm not exactly sure what is supposed to be done.
1.
hawaiian_word[0][0] is redundant and likely not what you think.
The think with this line is it first returns the first letter of hawaiian_word which is now a one letter string, and now you get the first letter of this one letter string... which is the same string. You're likely trying to do something else but like I explained I'm not quite sure.
2.
for key in hawaiian_word:
if hawaiian_word[i] in consonants_dict:
pronunciation += consonants_dict[key]
So no runtime error should occur here since you're first checking if the key even exists, but I noticed your dictionary is defined as: "consonants_dict = {
'p':'p',
'k':'k',
'h':'h',
'l':'l',
'm':'m',
'n':'n',
'w':'w',
'aw':'w',
'iw':'v',
'ew':'v',
'uw':'w',
'ow':'w'
}
"
and you'll notice the last few keys have two letters, except your for loop is going through the word one letter at a time so these keys will never be used.
3. (probably the cause of your error)
the line in each elif statement you have some code along the lines of: pronunciation += dictionary[key]
except this key is only being defined in the for loop and and never changes. This key is actually just going to be the last letter of the word in each case assuming the first if condition is met the for loop runs and key is finally set to the last letter and never changes until it runs again but even then after it finishes it is set to the last letter. This word is unlikely to be in each dictionary. I'm assuming you meant to actually do:
elif hawaiian_word[0][0] in vowels_dict:
key = hawaiian_word[0][0]
pronunciation += vowels_dict_capitalized[key]
elif hawaiian_word[i] in vowels_dict:
key = hawaiian_word[i]
pronunciation += vowels_dict[key]
elif hawaiian_word[i-2] in vowels_dict:
key = hawaiian_word[i-2]
pronunciation += vowels_no_hyphen_dict[key]
because you're checking if the current letter you're on in word is in the dictionary but then you use something completely different?