Skip to content

Instantly share code, notes, and snippets.

@levidurfee
Created February 6, 2016 01:34
Show Gist options
  • Save levidurfee/80c1aa47adf64351099f to your computer and use it in GitHub Desktop.
Save levidurfee/80c1aa47adf64351099f to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import csv # Need csv to read/write csv's
import sys # Need sys to read/write files
import math # Need math to do calculations
import time # Need time to get current date
from datetime import date # Not sure about
today = date.today() # Get today's date
print "What is your weight?"
weight = raw_input()
# Open weight.txt for writing / appending to file
k = open('weight.txt', 'a')
# Create writer object from csv of file handler
writer = csv.writer(k)
# Write todays date and weight as CSV to file
writer.writerow( (today, weight) )
# Close the file handler
k.close()
# Now we need to read the file, so we create a file handler and open the file in read more
f = open('weight.txt', 'r')
# Read the lines into a variable
lines = csv.reader(f)
# Need these to keep track of where we are in the for loop
i = 0 # We will add each row's weight to this (e.g. 180 + 180 + ...)
x = 0 # This will keep track of the number of rows - probably another way to do this
# Loop through each row, each row will be stored in the row variable (doesn't have to be called row)
# Alt syntax for levi in lines
# print levi[1]
# That would use levi as the row variable
for row in lines:
#print row[1]
i = i + int(row[1]) # keep adding up the weights, so we can average it
x = x + 1 # Keep track of the number of rows there are (1, 2, 3...)
# Close the filehandler
f.close()
# Get an average, sum of weight (i) divided by number of rows (x)
avg = i / x
#print 'total %i' % i
print 'avg %i' % avg #Display the average to the user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment