在输入单词值后,我无法弄清楚为什么我的代码没有打印出任何内容。我可以输入一个单词,但在通过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
答案 0 :(得分:3)
如果您使用名为dictionary的数据结构(python中的一个非常基本的数据结构),那么您想要实现的目标非常简单。像这样更改数据结构:
let swipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "didSwipe:")
self.view.addGestureRecognizer(swipeGestureRecognizer)
现在您可以使用键(单词)访问值(发音)。
像这样,dic={"ai" :"eye-","ae" :"eye-","ao": "ow-","au" :"ow-"......}
你会得到:
dic["ai"]
所以,现在让我们试着找到解决方案:
定义字典。
eye-
获取输入,如果不使用python3
,最好使用raw_input dic={"ai" :"eye-","ae" :"eye-","ao": "ow-","au" :"ow-"......}
用空格分割输入并形成一个列表。
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
。
因为,您的要求对我来说并不十分清楚,我已经包含了解决问题所需的所有内容。
因此,在需要的地方使用它们并解决问题。
快乐的编码。
答案 1 :(得分:0)
答案和评论中说过“使用字典”的人是对的,但是你不能一次循环输入一个字符,因为你有重叠的匹配。在不查看下一个字符的情况下,您无法判断“a”是否是“ai”或“an”的一部分,并且这些情况的处理方式不同。这是一个完整的解决方案,带有注释,可以处理细微内容,并在遇到非法字符串时提供信息性错误消息。
hawaiian_pronunciation = {
"ai": "eye-",
"ae": "eye-",
"ao": "ow-",
"au": "ow-",
"ei": "ay-",
"iu": "ew-",
"oi": "oy-",
"ui": "ooey-",
"a": "ah-",
"e": "eh-",
"i": "ee-",
"o": "oh-",
"u": "oo-",
"p": "p",
"k": "k",
"h": "h",
"l": "l",
"m": "m",
"n": "n"
}
def segment(word, start):
# we have to consider the longest possible segment first
# so that hai gets tokenized h-ai instead of h-a-i
for length in (2,1):
# check if the length of the segment puts us past the end of the word
# the upper bound can be equal to the length of the word since the
# string[upper_bound] is not actually included in the range
if start+length > len(word):
continue
# the input segment we are considering
input = word[start:start+length]
# is it in our dictionary?
if input in hawaiian_pronunciation:
# if it is get the corresponding pronunciation
output = hawaiian_pronunciation[input]
# return the output and length for a successful match
return output, length
# if no candidate matches, raise an exception describing where you were
# when you failed to find a match
raise Exception("cannot match word {} at position {}, bad segment {}",
word, start, word[start:])
def pronounce(word):
"generate pronunciation from word"
# build a list of strings
out = []
# we have to use a while loop and an explicit index
# because we can jump by one or two spaces depending on the length of the match
start = 0
while start < len(word):
# when we get a match, append the new sound and
# advance the appropriate number of spaces
new_sound, length = segment(word, start)
out.append(new_sound)
start += length
return "".join(out)
def main():
print pronounce("hai")
main()