Snake Charming

Posted on October 5th, 2012

Filed under: Code — Karl Olson @ 4:04 am

Or, why I’ve added a new category to the blog.

I’ve come to realize that when I don’t have school work to do, music to write/produce for other rad musicians, or reviews to write about cartoons, I should probably try to be proactive in keep my programming skills sharp. While I wouldn’t feel comfortable doing anything too serious at the moment, I figure short Python scripts might be a good plan. Thus, I’ve added a code section to my blog, and (hopefully) this means I put the extra time in my day that isn’t going into the above responsibilities into something more productive than reading up on internet news I’ll forget a day later.

Eventually, these posts will just be updates that say I’ve posted something new to git-hub or something similar, but to inaugurate this, I have written a little script that compares cost of ownership between two cars. Check it out after the break.

##the utterly simplified car cost comparison script v.0.1
##author: karl olson
##known issues: ignores the cost of automotive maintenance such oil changes,
##let alone cost of part should something break down. would need enhancements
##to compare vehicles that run on fuels that have different costs.

print ('Automatic Automotive Cost Comparison')
print ('Finds how many miles you have drive before the difference ' +
'between two cars washes out.')

##get car prices, average mpg, gas prices
car1price = int(input('Enter first automobile\'s price: '))
car1mpg = int(input('Enter first automobile\'s average mpg: '))
car2price = int(input ('Enter second automobile\'s price: '))
car2mpg = int(input ('Enter second automobile\'s average mpg: '))
gasprice = int(input('Enter gas price: '))

##take the price difference between the two models
##v.0.1 - lazy assumption that second car is cheaper
##also assumes that the first car has a higher mpg

##miles traveled so far - 0 since they are new
milage = 0

##swap prices/data so that end user can enter vehicles in any order rather than
##high price first, low price second
if (car1price < car2price): temp = car1price car1price = car2price car2price = temp temp = car1mpg car1mpg = car2mpg car2mpg = temp ##if mpg of the more expensive vehicle is less than the less expensive vehicle ##don't even bother running the price comparison algorithm if (car1mpg <= car2mpg): print('total of cost of ownership will never reach parity.') else: ##take the initial price difference between the two models pricediff = car1price-car2price ##calculate total costs so far for each car until the prices of the second ##reaches the first while (pricediff > .01):

#calulate number of gallons price difference buys you
gallons = pricediff/gasprice

#calulate total milage so far.
milage = milage + (gallons * car2mpg)

##add price difference to car 1
car2price = car2price + pricediff

##add fuel economy adjusted price difference to car 2
##also assumes that the first car has a higher mpg
car1price = car1price + (pricediff * (car2mpg / car1mpg))

##print results so far.
##print(int(car1price), int(car2price), int(milage), int(pricediff))

##take the price difference again for next loop
pricediff = car1price-car2price

print('$', int(car1price), ' is where ownership costs for both cars ' +
'are the same, and they will been driven for ' , int(milage) , ' miles.')

Or, since my code tag css could use some help itself, you can download the code itself.

Comments Off on Snake Charming

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.