学习python时,记录自己或观察别人从错误到正确的思考,远比只看正确答案有效——傅查理1.判断是否有字母”e"写一个函数has_no_e,当给定的单词不包含字母'e'时返回True一开始我这样写:defhas_no_e(word):forletterinword:ifletter=='e':returnFalsereturnTruehas_no_e('tdy')但是总是和预期的结果不一样。word有'e'时返回True,所以加个print看情况:defhas_no_e(word):forletterinword:ifletter=='e':print(letter)returnFalsereturnTruehas_no_e('hello')但它没有效果,而且print似乎没有效果。这时候我有点怀疑打印问题了。defhas_no_e(word):forletterinword:print(letter)has_no_e('hello')但是打印这个没有问题,所以想单独试一下判断条件:word='hello'forletterinword:ifletter=='e':returnFalseelse:returnTrueFile"",line4returnFalse^SyntaxError:'return'outsidefunction这报语法错误。我在网上查过。原因是return在函数外没有意义。后面想把return改成break,但是break在多层循环中只能打断一层,不能跳出循环。由于去掉后一直没有解决,只能从原来的代码中找原因。后来发现returnTrue在循环里,所以放到循环外应该没问题,然后试试defhas_no_e(word):forletterinword:ifletter=='e'orletter=='E':returnFalsereturnTruehas_no_e('tdy')正常返回,所以没问题,后面加上'E'的筛选2.阅读在words.txt中,打印出不带'e'的词,并计算此类词在整个词表中的百分比。words.txt可以在http://thinkpython.com/code/w...找到,以及然后计算打印字数和总字数,然后计算百分比,先计算打印字数如下defhas_no_e(word):forletterinword:ifletter=='e'orletter=='E':returnFalseprint(word)returnTruefin=open('words.txt')count=0sum=0forlineinfin:word=line.strip()has_no_e(word)count+=1sum+=1print(count)word打印是正确的,但是sum和count是一样的,这时候我想到的是先找出总的word个数,看哪个错了fin=open('words.txt')count=0forlineinfin:count+=1print(count)和上面找到的结果一样,那么上面是总字数,说明我对循环不熟,以为前面加个条件就是计算条件个数。但是加了一个函数后,不知道函数里面怎么统计字数,所以不想用这个函数,如下:fin=open('words.txt')sum=0count=0forlineinfin:forletterinword:ifletter=='e':continueprint(word)count+=1sum+=1print(sum)print(count)但是打印出来的都是最后一个字母,还有一个数量统计问题。后面觉得这个逻辑是对的,把判断加到原来代码的功能上试试;defhas_no_e(word):forletterinword:ifletter=='e'orletter=='E':returnFalsereturnTruefin=open('words.txt')sum=0count=0forlineinfin:word=line.strip()ifhas_no_e(word):#只有返回True时才遵循下面的逻辑,这样就不会统计所有的wordsprint(word)count+=1sum+=1print(count)print(sum)的结果print(count/sum)是正确的,但是结果是小数,需要转换成百分比a=count/sumb='%.2f%%'%(a*100)#将小数转换成百分比并保留两位小数print(b)3、写一个函数is_abecedarian,如果单词中的字母是按字母顺序排列的(重复两次即可),则返回True看到这让我想起了之前做的一个字符串比较题。题目说字符串也可以通过运算符进行比较。大写字母比小写字母小,相似的字母按字母顺序排序。第一个比后者小,然后根据这个写下面的代码:defis_abecedarian(word):foriinrange(0,len(word)-1):#注意这里是len(word)-1,因为下面有word[i+1],如果不减一,就会导致下标超出范围ifword[i]>word[i+1]:returnFalsereturnTrueis_abecedarian('Aabcedarian')根据这个函数和之前写的代码,可以计算出words.txt单词列表中符合这个规则的单词总数fin=open('words.txt')count=0sum=0forlineinfin:word=line.strip()ifis_abecedarian(word):print(word)count+=1sum+=1print(count)print(sum)a=count/sumb='%.2f%%'%(a*100)print(b)匹配113809个词中的596个,占0.52%。看了答案,有如下三种方法:#for循环方法,不过和我的有点不同defis_abecedarian1(word):previous=word[0]forcinword:ifcword[1]:returnFalsereturnis_abecedarian2(word[1:])is_abecedarian2('aello')#whileloopdefis_abecedarian3(word):i=0whilei=0:ifword1[i]!=word2[j]:返回Falsei=i+1j=j-1返回Truedefage_me():formo_ageinrange(10,100):forageinrange(100):iflen(str(age))<2andis_reverse(str(age).zfill(2),str(mo_age))和age=0:如果word1[i]!=word2[j]:返回Falsei=i+1j=j-1returnTruedefall_list(arr):#统计列表中每个元素出现的次数result={}foriinset(arr):result[i]=arr.count(i)返回resultdefage_me():a=[]formo_ageinrange(10,100):forageinrange(100):iflen(str(age))<2andis_reverse(str(age)).zfill(2),str(mo_age))andage120:breakdaughter=daughter+1returncountdefcheck_diffs():"""枚举母女之间可能存在的年龄差异,对于每个差异,计算他们一生中年龄彼此相反的次数。"""diff=10whilediff<70:n=num_instances(diff)ifn>0:print(diff,n)diff=diff+1print('diff#instances')check_diffs()#printprint('女儿妈妈')num_instances(18,True)