Skip to content

Latest commit

 

History

History
15 lines (12 loc) · 557 Bytes

looping-and-counting.md

File metadata and controls

15 lines (12 loc) · 557 Bytes

下面的程序统计了字符a在字符串中出现的次数:

word = 'banana' 
count = 0
for letter in word:
    if letter == 'a': 
    count = count + 1
print(count)

这个程序演示了另一种计算模式,称为计数器。变量count初始值为0,然后遍历过程中,每发现一个a,count值就加一。当循环结束时,count就是a在字符串中出现的次数。

习题 6.3 定义一个count函数并封装这段代码,对其进行通用化改造,能够接收字符串和字母作为参数。