Advertisement
Mars83

6-3

Oct 9th, 2011
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.59 KB | None | 0 0
  1. #! /usr/bin/env python3.2
  2. # -*- coding: utf-8 -*-
  3.  
  4. # main.py
  5. """ Task: Exercise 6.3
  6.    Encapsulate this code in a function named count, and generalize it so that
  7.    it accepts the string and the letter as arguments.
  8.    
  9.    word = 'banana'
  10.    count = 0
  11.    for letter in word:
  12.        if letter == 'a':
  13.            count = count + 1
  14.    print(count)
  15. """
  16.  
  17. ''' Functions '''
  18. def count(word, letter):
  19.     count = 0
  20.     for letter in word:
  21.         if letter == 'a':
  22.             count += 1
  23.     return count
  24.  
  25. ''' Test '''
  26. word = 'banana'
  27. count = count(word, 'a')
  28. print(count)
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement