算法 - Python做题 提示 string index out of range
怪我咯
怪我咯 2017-04-17 13:14:15
0
1
1397

Pig Latin”是一个英语儿童文字改写游戏,整个游戏遵从下述规则:

(1). 元音字母是‘a’、‘e’、‘i’、‘o’、‘u’。字母‘y’在不是第一个字母的情况下,也被视作元音字母。其他字母均为辅音字母。例如,单词“yearly”有三个元音字母(分别为‘e’、‘a’和最后一个‘y’)和三个辅音字母(第一个‘y’、‘r’和‘l’)。

(2). 如果英文单词以元音字母开始,则在单词末尾加入“hay”后得到“Pig Latin”对应单词。例如,“ask”变为“askhay”,“use”变为“usehay”。

(3). 如果英文单词以‘q’字母开始,并且后面有个字母‘u’,将“qu”移动到单词末尾加入“ay”后得到“Pig Latin”对应单词。例如,“quiet”变为“ietquay”,“quay”变为“ayquay”。

(4). 如果英文单词以辅音字母开始,所有连续的辅音字母一起移动到单词末尾加入“ay”后得到“Pig Latin”对应单词。例如,“tomato”变为“omatotay”, “school” 变为“oolschay”,“you” 变为“ouyay”,“my” 变为“ymay ”,“ssssh” 变为“sssshay”。

(5). 如果英文单词中有大写字母,必须所有字母均转换为小写。

输入样例

Welcome to the Python world Are you ready

输出样例

elcomeway otay ethay ythonpay orldway arehay ouyay eadyray

请构建一个完整的程序,要求接下列输入,然后将这段英文转化为Pig Latin语言,将输出填入到空格中。

Python is intended to be a highly readable language It is designed to have an uncluttered visual layout frequently using English keywords where other languages use punctuation Furthermore Python has a smaller number of syntactic exceptions and special cases than C or Pascal

我的解法:

s='Python is intended to be a highly readable language It is designed to have an uncluttered visual layout frequently using English keywords where other languages use punctuation Furthermore Python has a smaller number of syntactic exceptions and special cases than C or Pascal'
wordlist=s.split(' ')

def lower(word):
    if word[0]>='A'and word[0]<='Z':
        return chr(ord(word[0])+32)+word[1:]
    else:
        return word
wordlist=[lower(word) for word in wordlist]

yuanyin=['a','e','i','o','u']
yuanyin2=yuanyin+['y']
def fuyinkaitou(word):##针对辅音开头的情况
    index=1
    while (word[index] not in yuanyin2):
        index+=1
        if index==(len(word)-1):break
    newword=word[index:]+word[:index]+'ay'
    return newword
wordlist2=[]    
for word in wordlist:
    #print word,wordlist2
    if word[0] in yuanyin:
            #print "swich 1",word
            word+='hay'
            wordlist2.append(word)

    elif word.startswith('qu'):
        #print "swich 2",word
        word=word[2:]+'quay'
        wordlist2.append(word)

    else:
        #print "swich 3",word
        word=fuyinkaitou(word)
        wordlist2.append(word)
print wordlist2

总是报string index out of range的错误,应该是辅音开头那个函数有问题,应该怎么改呢?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(1)
左手右手慢动作

The cause of the error lies in the following function

def fuyinkaitou(word):##针对辅音开头的情况
    index=1
    while (word[index] not in yuanyin2):
        index+=1
        if index==(len(word)-1):break
    newword=word[index:]+word[:index]+'ay'
    return newword

Your index starts from 1. If you call fuyinkaitou('C'), an error will occur
IndexError: string index out of range
Can be modified to:

def fuyinkaitou(word):##针对辅音开头的情况
    index=1
    if len(word) == 1:
        return word + 'ay'
    else:
        while (word[index] not in yuanyin2):
            index+=1
            if index==(len(word)-1):break
        newword=word[index:]+word[:index]+'ay'
        return newword

In addition, the list format finally output by your program must be modified into a string format. You can change the last sentence to:

print ' '.join(wordlist2)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template