【发布时间】:2016-11-08 12:05:36
【问题描述】:
我有以下功能,第二个功能count_forbid(a)只能工作1次。在此示例中,它计算不包含字母 'c' 的单词的正确值,但对于 y,它返回零。所以这意味着代码只能在第一次正确执行,而在所有其他时间返回零:
import string
fin = open('words.txt')
def forbid_or_not(word,forb):
for letter in word:
if letter in forb:
return False
return True
def count_forbid(a):
count = 0
for line in fin:
word1 = line.strip()
if forbid_or_not(word1,a):
count += 1
return count
x = count_forbid('c')
y = count_forbid('d')
【问题讨论】:
标签: python function file python-3.x