think python学习心得-(6)利用words.txt来进行填字游戏
【摘要】 练习9-1
fin = open('C://words.txt')for line in fin: word = line.strip() if len(word) >20: print word
words.txt被保存在了C盘根目录下。
练习9-2
统计字母表中含有e的单词占有的百分比
fin = open('C://words.txt')count...
练习9-1
-
fin = open('C://words.txt')
-
for line in fin:
-
word = line.strip()
-
if len(word) >20:
-
print word
练习9-2
统计字母表中含有e的单词占有的百分比
-
fin = open('C://words.txt')
-
counter1 = 0
-
counter2 = 0
-
def has_no_e(wo):
-
for i in range(len(wo)):
-
if wo[i-1]=='e':
-
return True
-
-
for line in fin:
-
word = line.strip()
-
flag = has_no_e(word)
-
counter1=counter1+1
-
if flag == True:
-
counter2=counter2+1
-
-
counter1=float(counter1)
-
counter2=float(counter2)
-
print 100-counter2/counter1*100
利用下标循环实现输入文字中是否存在禁用字母的鉴别
-
def avoids(word,forbiden):
-
for i in range(len(forbiden)):
-
for j in range(len(word)):
-
if word[j-1]==forbiden[i-1]:
-
return False
-
return True
-
-
print 'Plesase input forbiden alphabet:'
-
ban=raw_input('>')
-
print 'Please input desired word:'
-
test=raw_input('>>')
-
print avoids(test,ban)
检测是否是由可用字符组成的
-
def use_only(word,avaliable):
-
for i in range(len(word)):
-
flag=0
-
for j in range(len(avaliable)):
-
if word[i-1]==avaliable[j-1]:
-
flag =1
-
if flag==0:
-
return False
-
return True
-
-
print 'Plesase input avaliable alphabet:'
-
ban=raw_input('>')
-
print 'Please input desired word:'
-
test=raw_input('>>')
-
print use_only(test,ban)
练习9-5
检测alphebat是否被全部使用
-
def uses_all(word,avaliable):
-
counter=0
-
for i in range(len(avaliable)):
-
flag=0
-
for j in range(len(word)):
-
if word[j-1]==avaliable[i-1]:
-
flag =1
-
if flag==0:
-
return False
-
else:
-
counter=counter+1
-
-
if counter == len(avaliable):
-
return True
-
else:
-
return False
-
-
print 'Plesase input avaliable alphabet:'
-
ban=raw_input('>')
-
print 'Please input desired word:'
-
test=raw_input('>>')
-
print uses_all(test,ban)
-
def is_abecedarian():
-
fin = open ('C://words.txt')
-
counter=0
-
for line in fin:
-
flag = 0
-
for i in range(len(line)-1):
-
if line[i]<=line[i+1]:
-
flag=flag+1
-
if flag==len(line)-2:
-
counter=counter+1
-
return counter
-
print 'The number of abecedarian words in directionary is'
-
print is_abecedarian()
QNMD车迷天下。。。。不做了
文章来源: zclhit.blog.csdn.net,作者:zclhit_,版权归原作者所有,如需转载,请联系作者。
原文链接:zclhit.blog.csdn.net/article/details/51038066
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)