Tuesday, September 29, 2009

fuelEfficiency.py, v 2.0

I'm a teaching assistant in the ComputerScience&SoftwareEngineering (CSSE) department at my college (Rose-Hulman) and since I only started this quarter without previous TA experience, I get the late-night Monday slot from 9 till 11. Thusly, I don't get a lot of visitors. But yesterday evening, I got two groups of freshmen come in and ask for help on the same programs. I was actually excited and happy to discover that I was able to help them quite well; It felt really good to assist them in their learning. Anyway, the program that I helped them create was a piece of Python code that collects user data on the odometer readings and gas spent over successive legs of a car trip. It spits out the MilesPerGallon per leg and the average mpg over the whole trip.

Anyway, this was worrisome to me because that was one program that I had difficulty getting right, because I stubbornly wanted to get it perfect. Ended up being 300 lines of code, with documentation. Now, due to my further experience with code and helping the freshmen with the same program, I felt I could take another shot at the fuel efficiency code and get it more perfect and more efficient. So here is my fuelEfficiency.py program, v 2.0 ----


# fuelEfficiency.py
# by Douglas Selby, on 9-29-09
# Program will compute the fuel efficiency of a multi-leg journey
# First prompt for starting odometer reading (distance, in miles)
# Get information about series of legs
# For each leg:
# User enters current odometer reading and amount of gas used
# User signals end of the trip with a blank line
# Returns out miles per gallon for each leg and total miles per gallon for whole trip


## ----Start converting program components of main definitions---- ##

# Asks for what the odometer reading is for the current leg
def ask():
data = raw_input("What is the odometer reading (miles) and the gas that has been used (gallons)? Seperate with a space. ")
return data

# calls ask, only called if user inputted data incorrectly
def askAgain():
print "The information that you entered was incorrect. Please try again.\n"
data = ask()
return data

# Converts string 'number' into an integer value
def efloat(number):
number = eval(number)
number = float(number)
# print type(number)
# number is now 'float'
return number

## ----End sub-program components of main---- ##


## ----Start main definition---- ##

def main():

# Initialization
blankline = ""
data = ""
oList = []
mpgList = []
sumGas = 0
odom = 0
gas = 0
leg = 0

# Spacer to recognize program start
print "-- Trip start --\n"
# Prompt for starting odometer reading (starto)
odom = raw_input("What is the starting odometer reading (miles)? ")
# If user does not enter a value, prompts again
while odom == blankline:
odom = raw_input("You must enter a starting odometer reading: ")
# If user enters an invalid entry (any negative number), prompts again
while odom < "0":
print "Your odometer cannot have a negative setting."
odom = raw_input("Please check that you are not driving a future or alien vehicle and re-enter the starting odometer reading: ")

# Convert odom to an float value
odom = efloat(odom)
oList.append(odom)
# Start leg 1 prompt (leg 1 only required leg)
print "\n-- First Leg (or final destination) --"
data = ask()

while data != blankline:
leg += 1
temp = data.split()
while len(temp) != 2:
data = askAgain()
temp = data.split()
odom = temp[0]
gas = temp[1]
odom = efloat(odom)
gas = efloat(gas)
oList.append(odom)
sumGas += gas
mpg = (odom - oList[leg-1]) / gas
mpgList.append(mpg)
print "\n-- Next Leg (or final destination) --"
data = ask()

# Spacer to recognize program start
print "-- Trip end -\n"
if leg != 0:
# Get total average mpg
fmpg = (oList[leg-1]-oList[0])/sumGas
# Print final answers
print "-- Final fuel efficiency results (in miles per gallon, or mpg) --\n"
for i in range(leg):
print "Leg %0d - %0.2f mpg\n" % (i+1, mpgList[i])
print "The fuel efficiency for the entire trip is %0.2f mpg" % (fmpg)
else:
print "No leg data was entered; did you go anywhere? Restart the program when you actually want to track your journey mileage."

## ----End main definition---- ##


## ----Start program---- ##

## ----Run main function run---- ##
main()
## ----End main function run---- ##

## ----End program---- ##

No comments: