[Tutor] I need help fast
Cameron Simpson
cs at cskk.id.au
Tue Oct 1 19:26:48 EDT 2019
On 01Oct2019 18:24, Adam Dickenson <adamdickenson50252 at gmail.com> wrote:
>Attached are what I need help on.
This is a text only list. Attachments should be dropped. (They seem to
have come through, so you're lucky, but we _strongly_ prefer inline
code).
>I cannot get them started and they are
>due at 11PM tonight. Can someone help?
We don't know when 11PM is for you. You'll just have to hope you're
lucky with who is looking at the list in your time window.
I'm going to assume "cannot get them started" means you don't know where
to begin implementing the code, not "I cannot invoke Python".
So, let's look at these scripts. Comments below the script (we use the
inline style in this list: replies below the relevant quote from the
preceeding message, ideally trimming excess quoteed stuff to just the
necessary stuff needed for context; like a conversation in a novel).
>'''
> to run tests on mac: python3 -m doctest car_speed.py -v
> to run tests on Win: python -m doctest car_speed.py -v
>'''
>
>def car_speed(distance_of_skid):
> '''
> Calculate the speed in MPH of a car that skidded
> d feet on dry concrete when the brakes were applied
>
> args:
> distance_of_skid (float): the distance of the skid in feet
>
> returns:
> an estimate of the speed the car was going when the brakes were applied (float)
>
> formula:
> speed in MPH equals the square root of (24 * d)
>
> examples/doctest:
>
> the car didn't skid at all
> >>> round(car_speed(0), 2)
> 0.0
>
> the car skid 1 foot
> >>> round(car_speed(1), 2)
> 4.9
>
> the car skid 10 feet
> >>> round(car_speed(10), 2)
> 15.49
>
> the car skid 33.33 feet
> >>> round(car_speed(33.33), 2)
> 28.28
>
> the car skid 12345 feet
> >>> round(car_speed(12345), 2)
> 544.32
>
> '''
> # TO DO: Add your code here
>
> return
So, you need to write code to implement this calculation in Python and
return the result. So the "return" line needs to read "return
inferred_speed", and we'll define an "inferred_speed" variable. Which we
need to compute.
Fortunately the docstring above provides the formula they want you to
use: "speed in MPH equals the square root of (24 * d)". Which saves me
having to look up arcane stuff like how many feet make a mile, etc,
living as I do in the SI world (kilometres, metres, etc).
So: you have the distance supplied to you: it is the function parameter
"distance_of_skid" at the top of the function; they want to use this in
the formula as "d".
You do need a square root function, but Python ships with one in its
"math" module. It is described here:
https://docs.python.org/3/library/math.html#math.sqrt
To access it put this at the start of your Python script:
from math import sqrt
So now you could write:
inferred_speed = sqrt(24 * distance_of_skid)
to implement that formula.
Leaving out the docstring, your whole programme would look like this
(totally untested):
from math import sqrt
def car_speed(distance_of_skid):
inferred_speed = sqrt(24 * distance_of_skid)
return inferred_speed
You should be able to approach the other questions in a similar manner.
Note that while I dropped the docstring in the above text for clarity,
you do want it in your actual script because their instructions for
testing the script look like this:
to run tests on mac: python3 -m doctest car_speed.py -v
to run tests on Win: python -m doctest car_speed.py -v
That invokes Python's "doctest" module against your script, which finds
special parts of the function's docstring such as this:
>>> round(car_speed(1), 2)
4.9
and recognises them as tests (because they look like Python's
interactive prompt). So doctest will run:
round(car_speed(1), 2)
and compares the result against "4.9", which is what should come from
your function (after the rounding). This will let you easily test your
programme against the assortment of examples in the docstring.
Finally: DO NOT forget the indenting. It is critical in Python and
indicates what pieces of code are part of some structure. So:
def car_speed(distance_of_skid):
inferred_speed = sqrt(24 * distance_of_skid)
return inferred_speed
defines a larger structure (the "car_speed" function) and the two lines:
inferred_speed = sqrt(24 * distance_of_skid)
return inferred_speed
are part of that function _because_ they are indented underneath it.
Have a go at the other functions and see how they go.
You can return to this list (by replying to my message), but do so
_with_ your attempts and an explaination of what's wrong with them. We
don't do homework, but we will assist with understanding.
Ah, I see you've made an attempt at the cost_of_running_lightbulb
function. Ok:
>def cost_of_running_lightbulb(cents_per_kw_hour, bulb_wattage, hours_on):
> '''
> Calculate the cost of running a lightbulb using the formula
> wattage x hours used divided by 1000 x cost per kWh in cents
>
> args:
> cents_per_kw_hour (float): the cost in cents per kilowatt hour
> bulb_wattage (float): the wattage of the bulb
> hours_on (float): the number of hours the bulb was on
>
> returns:
> the cost of running the lighbulb in dollars
[...]
> # TO DO: Add you code here
>print(60 * 1 / 1000 * 0.01)
>return
The first thing to notice here is that your code is not indented. Indent
it the same as the comment "# TODO..." to make the code part of the
function.
The next thing is: 60 * 1 / 1000 * 0.01 is a constant expression: it
will always produce the same answer. You need to replace various terms
in that expression with the parameters from the function header:
cents_per_kw_hour, bulb_wattage, hours_on. Also, you can drop the "* 1",
multiplying by 1 changes nothing, so you can simplify things.
They're provided the formula: write it out in Python using those
parameters.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Tutor
mailing list