count = 0
for char in fruit:
if char == 'a':
count = count + 1
print count
第一步:list替换fruit : num替换char。不雅着急改变其他部分。
count = 0
for num in list:
if num == 'a':
count = count +1
print count
第二步:修改测试条件,检查num是否出现在low和high之间。
count = 0
for num in list:
if low < num < high:
count = count + 1
print count
第三步:封装代码在inBuchet中,参数是list,low,high
def inBucket(list,low,high):
count = 0
for num in list:
if low < num < high:
count = count + 1
return count
我们使用一个列表来存储着八个整数的结果:
numBuckets = 8
buckets = [0]*numBuckets
bucketWidth = 1.0/numBuckets
for i in range(numBuckets):
low = i * bucketWidth
high = low + bucketWidth
buckets = inBucket(list.low,high)
print buckets