Python 定义一个名为articleStats()的函数,该函数接受名为fileName的字符串参数
定义一个名为articleStats()的函数,该函数接受字符串参数 表示文件名的命名文件名。该文件包含 用空格分隔的小写单词。函数返回总数 所有文章的数量。文章是下列词语之一:a, 这个,一个 我知道这是一个很简单的问题,但我真的很困惑 我做错了什么。 这就是我目前所知道的,但我知道这是错误的Python 定义一个名为articleStats()的函数,该函数接受名为fileName的字符串参数,python,function,parameters,Python,Function,Parameters,定义一个名为articleStats()的函数,该函数接受字符串参数 表示文件名的命名文件名。该文件包含 用空格分隔的小写单词。函数返回总数 所有文章的数量。文章是下列词语之一:a, 这个,一个 我知道这是一个很简单的问题,但我真的很困惑 我做错了什么。 这就是我目前所知道的,但我知道这是错误的 def articleStats(filename): filename.split() for word in filename: if 'a' or 'the' or 'an' i
def articleStats(filename):
filename.split()
for word in filename:
if 'a' or 'the' or 'an' in word:
print(len(filename))
articleStats('an apple a day keeps the doctor away')
问题是
如果word中的'a'或'The'或'an:
。在Python中,字符串文本的计算结果为True
,因此您的条件被视为:如果为True或True,或者word中的“an”
如果word中的“a”或word中的“the”或word中的“an”,则将此更改为
为了更好地理解正在发生的情况,请运行以下代码以了解Python如何处理其他情况
tests = [[], ['not empty list'], {}, {'not':'empty dict'}, 0, 1, '', 'not empty string']
for test in tests:
print test, bool(test)
你怎么知道这是错的?你不会在只说“我知道我病了”之后就去看医生要求诊断。可能是重复的,因为它一直在打印