在我为word输入值之后,我很难弄清楚为什么我的代码不能打印出任何东西。我可以输入一个单词,但是在通过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发布于 2015-10-03 04:24:46
如果你使用一种叫做字典的数据结构,这是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)如果key不存在,将打印None。
由于我对您的要求不是很清楚,我已经包含了解决问题所需的所有内容。
因此,在需要的地方使用它们并解决问题。
祝你编码愉快。
发布于 2015-10-03 05:08:37
答案和评论中那些说“使用字典”的人是对的,但你不能一次只循环输入一个字符,因为你有重叠的匹配。如果不看下一个字符,就无法判断"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()https://stackoverflow.com/questions/32915120
复制相似问题