【发布时间】:2014-03-18 23:51:57
【问题描述】:
练习 9.3。编写一个名为 Avoids 的函数,它接受一个单词和一串禁用字母,如果单词不使用任何禁用字母,则返回 True。 修改你的程序,提示用户输入一串禁止使用的字母,然后打印不包含这些字母的单词数。你能找到排除最少单词的 5 个禁止字母的组合吗? 拿到第一部分:
def avoids(word,forb):
for letter in forb:
if letter in word:
return False
return True
与第二个作斗争,这是我的尝试:
fin=open('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 2.7\words.txt','r')
def no_contain():
forb=raw_input('enter the forbidden letters')
tot=0
ct=0
for line in fin:
word=line.strip()
for word in fin:
tot+=1
for letter in forb:
if letter in word:
ct+=1
return tot-ct
得到一些有趣的答案。另外,什么时候应该使用ct=0...ct+=1 技术而不是ct=ct+1?
【问题讨论】: