Revision 5:f358a45c148e
| python-intro.py | ||
|---|---|---|
| 11 | 11 |
else: |
| 12 | 12 |
return True |
| 13 | 13 |
|
| 14 |
def count_marlins(data): |
|
| 15 |
"""Receives a line of data and returns the count of Marlins in that line (zero otherwise)""" |
|
| 16 |
|
|
| 17 |
date, species, count = data.split(",")
|
|
| 18 |
marlins = 0 |
|
| 19 |
if species == "marlin": |
|
| 20 |
marlins = count |
|
| 21 |
return int(marlins) |
|
| 22 |
|
|
| 23 |
|
|
| 14 | 24 |
# script starts here... |
| 15 | 25 |
source = open('data.txt', 'r')
|
| 16 | 26 |
|
| 17 |
number = 0 |
|
| 27 |
number_of_marlins = 0
|
|
| 18 | 28 |
|
| 19 | 29 |
# count number of data records |
| 20 | 30 |
for line in source: |
| 21 | 31 |
if is_data(line) == True: |
| 22 |
number = number + 1
|
|
| 32 |
number_of_marlins = number_of_marlins + count_marlins(line)
|
|
| 23 | 33 |
|
| 24 | 34 |
source.close() |
| 25 | 35 |
|
| 26 |
print "Total number of data records:", number |
|
| 36 |
print "Total number of marlins:", number_of_marlins |
|
Also available in: Unified diff