Skip to content

Latest commit

 

History

History
160 lines (113 loc) · 4.23 KB

File metadata and controls

160 lines (113 loc) · 4.23 KB

Simple Calculation Programs

How far can we go in the car?

Let’s create a simple problem to solve with Python.

Our car’s gas tank can hold 12.0 gallons of gas. It gets 22.0 miles per gallon (mpg) when driving at 55.0 miles per hour (mph). If we start with the tank full and carefully drive at 55.0 mph, how many miles can we drive (total miles driven) using the whole tank of gas?

BONUS:

How long will it take us to drive all those miles?

What do we need figure out? We need a variable for our result: total_miles_driven. So we start our program this way…​

total_miles_driven = 0

# print result of computation
print(total_miles_driven)

It’s often good to start with a very simple template. If we run that, we will see 0 (zero) as the result, right?

Next step, let’s add the variables we know.

total_miles_driven = 0
total_hours_taken = 0

total_gas_gallons = 12.0
miles_per_gallon = 22.0
miles_per_hour = 55.0

# print result of computation
print(total_miles_driven)

Okay, good. We’ve added all the stuff we know about the computation. Well, except the part of the actual computation.

You probably know that if you multiply the miles_per_gallon by the total_gas_gallons, that will give you total_miles_driven. And if you divide the total_miles_driven by the miles_per_hour, you will get the total_hours_taken.

So let’s add those as Python statements.

total_miles_driven = 0
total_hours_taken = 0

total_gas_gallons = 12.0
miles_per_gallon = 22.0
miles_per_hour = 55.0

total_miles_driven = miles_per_gallon * total_gas_gallons
total_hours_taken = total_miles_driven / miles_per_hour

# print result of computation
print(total_miles_driven, total_hours_taken)

We get as a result 264 miles driven in 4.8 hours. And that’s how a simple Python program can get written.

Let’s do another.

The Cost of a "Free" Cat

A friend of ours is offering you a "free cat". You’re not allergic to cats but before you say yes, you want to know how much it’ll cost to feed the cat for a year (and then, approximately how much much each month).

We find out that cat food costs $2 for 3 cans. Each can will feed the cat for 1 day. (Half the can in the morning, the rest in the evening.) We know there are 365 days in a year. We also know that there are 12 months in the year. So how much will it cost to feed the cat for a year?

Looking at it, this may be quite simple. If we know each can feeds the cat for a day, we then know that we need 365 cans of food. So we can describe that as

total_cost = 0
cans_needed = 365
cost_per_can = 2.0 / 3.0

total_cost = cans_needed * cost_per_can # right?

months_per_year = 12
cost_per_month = total_cost / months_per_year
# print result of computation
print(total_cost, cost_per_month)

What’s going to be the answer? [1] Run it in your Repl window to work it all out.

And let’s do one more.

You Used Too Much Data!

A cell phone company charges a monthly rate of $12.95 and $1.00 a gigabyte of data. The bill for this month is $21.20. How many gigabytes of data did we use? Again, let’s use a simple template to get started.

data_used = 0.0

# print result of computation
print("total data used (GB)", data_used)

Let’s add what we know: that the monthly base charge (for calls, and so on) is $12.95 and that data costs 1 dollar per gigabyte. We also know the monthly bill is $21.20. Let’s get all that written down.

data_used = 0.0
cost_per_gb = 1.0
monthly_rate = 12.95

this_bill = 21.20

# print result of computation
print("total data used (GB)", data_used)

Now we’re ready to do the computation. If we subtract the monthly_rate from this_bill, we get the total cost of data. Then, if we divide the total cost of data by the cost per gigabyte, we will get the data_used.

data_used = 0.0
cost_per_gb = 1.0
monthly_rate = 12.95

this_bill = 21.20
total_data_cost = this_bill - monthly_rate

data_used = total_data_cost / cost_per_gb

# print result of computation
print("total data used (GB)", data_used)

How many GBs of data did we use? Turns out to be 8.25 gigabytes.

Now if the bill was $24.00? How many GBs then? (go ahead, I’ll wait…​) [2]


1. total_cost will be $243.33 and $20.28 per month.
2. total data used (GB) 11.05