
6-3
By:
Mars83 on
Oct 9th, 2011 | syntax:
Python | size: 0.59 KB | hits: 35 | expires: Never
#! /usr/bin/env python3.2
# -*- coding: utf-8 -*-
# main.py
""" Task: Exercise 6.3
Encapsulate this code in a function named count, and generalize it so that
it accepts the string and the letter as arguments.
word = 'banana'
count = 0
for letter in word:
if letter == 'a':
count = count + 1
print(count)
"""
''' Functions '''
def count(word, letter):
count = 0
for letter in word:
if letter == 'a':
count += 1
return count
''' Test '''
word = 'banana'
count = count(word, 'a')
print(count)