changeset 5:f358a45c148e

Created a function to count the total number of marlins.
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Tue, 05 Feb 2013 17:49:53 +0000
parents 79ef88975054
children 4ec6aa30a1f8
files python-intro.py
diffstat 1 files changed, 13 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/python-intro.py	Tue Feb 05 17:05:06 2013 +0000
+++ b/python-intro.py	Tue Feb 05 17:49:53 2013 +0000
@@ -11,16 +11,26 @@
     else:
         return True
 
+def count_marlins(data):
+    """Receives a line of data and returns the count of Marlins in that line (zero otherwise)"""
+
+    date, species, count = data.split(",")
+    marlins = 0
+    if species == "marlin":
+        marlins = count
+    return int(marlins)
+
+
 # script starts here...
 source = open('data.txt', 'r')
 
-number = 0
+number_of_marlins = 0
 
 # count number of data records
 for line in source:
     if is_data(line) == True:
-        number = number + 1
+        number_of_marlins = number_of_marlins + count_marlins(line)
 
 source.close()
 
-print "Total number of data records:", number
+print "Total number of marlins:", number_of_marlins