Python解题时遇到string index out of range错误求原因分析,pythonrange,Pig Latin&rd
Python解题时遇到string index out of range错误求原因分析,pythonrange,Pig Latin&rd
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 wordwordlist=[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 newwordwordlist2=[] 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的错误,应该是辅音开头那个函数有问题,应该怎么改呢?
错误原因在于下面这个函数
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
你的index是从1开始的,如果调用fuyinkaitou('C'), 则会出现错误
IndexError: string index out of range
可以修改成:
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
另外你的程序最后输出的列表形式,要修改成字符串形式。可以把最后一句改为:
print ' '.join(wordlist2)
编橙之家文章,
相关内容
- Python倒计时效果用什么方法实现,python倒计时,我在尝试
- python报错:wordcloud.whl is not a supported wheel on this plat,
- 实现PythonFlask后台数据实时显示的方法,pythonflask后台
- Python celery与redis构建消息list阻塞问题求助,celeryredis
- python flask新手相关的问题,pythonflask,问题home.html{
- 请教web.py服务器端接收到的上传文件名出现乱码怎么解
- 需要推荐个python轻量级地图框架或者库,python地图框架
- 需要完全自定义auth模块操作方法是怎样的,auth操作方
- 请教Python文字冒险游戏指令源码思路,python冒险游戏
- python线程消息处理系统怎样在维持主线程同时结束子线
评论关闭