输入字词值后,我很难弄清楚为什么我的代码没有打印出任何内容。我可以输入一个单词,但是通过while循环求值后它什么也不会输出。你怎么看?

ai = "eye-"
ae = "eye-"
ao = "ow-"
au = "ow-"
ei = "ay-"
eu = "eh-oo-"
iu = "ew-"
oi = "oy-"
ou = "ow-"
ui = "ooey-"
a = "ah-"
e = "eh-"
i = "ee-"
o = "oh-"
u = "oo-"
p = "p"
k = "k"
h = "h"
l = "l"
m = "m"
n = "n"

word = input("Please enter a Hawaiian word you want pronounced")
character_count1 = int(0)
character_count2 = int(1)
pronunciation = ""
while character_count1 < len(word):
    if word[character_count1:character_count2] == ai or word[character_count1:character_count2] == "ae"or word[character_count1:character_count2] == "ao"or word[character_count1:character_count2] == "ei"or word[character_count1:character_count2] == "eu"or word[character_count1:character_count2] == "iu"or word[character_count1:character_count2] == "oi"or word[character_count1:character_count2] == "ou":
        print("your word",pronunciation + word[character_count1:character_count2])
        character_count1 + 2 and character_count2 + 2
    elif word[character_count1:character_count1] == a or word[character_count1:character_count1] == e or word[character_count1:character_count1] == i or word[character_count1:character_count1] == o or word[character_count1:character_count1] == p or word[character_count1:character_count1] == k or word[character_count1:character_count1] == h or word[character_count1:character_count1] == l or word[character_count1:character_count1] == m or word[character_count1:character_count1] == n:
        print("your word",pronunciation + word[character_count1:character_count1] )
        character_count1 + 1 and character_count2 + 1

最佳答案

如果您使用称为字典的数据结构,这是python中非常基本的数据结构,那么您要实现的目标非常容易。像这样更改数据结构:

dic={"ai" :"eye-","ae" :"eye-","ao":  "ow-","au" :"ow-"......}


现在,您可以使用键(单词)访问值(发音)。

像这样,

dic["ai"]


你会得到:

eye-


因此,现在让我们尝试获取解决方案:

定义字典。

 dic={"ai" :"eye-","ae" :"eye-","ao":  "ow-","au" :"ow-"......}


接受输入,如果不使用python3,最好使用raw_input

word = raw_input("Please enter a Hawaiian word you want pronounced")


用空格分隔输入并形成一个列表。

lst=word.split()


lst的元素用作字典key来找到value。遍历列表,检查输入是否与dic的任何键匹配

for i in lst:
    print dic.get(i)


如果None不存在,将打印key

因为,您的要求对我来说不是很清楚,我已经包含了解决问题所需的所有东西。

因此,请在需要的地方使用它们并解决问题。

快乐的编码。

关于python - Python String Help Hawaiian Word发音,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32915120/

10-09 08:22