[Tutor] Passing arguments?
Alan Gauld
alan.gauld at btinternet.com
Sun Oct 20 20:26:22 CEST 2013
On 20/10/13 05:20, Jenny Allar wrote:
> I've written the code below the assignment, and I think I have
> everything covered in terms of asking the user for the information I
> need and somehow calculating costs, but I'm just ridiculously confused
> on the order and placement of the functions and components of this
> program- specifically the shiprate variable.
Its pretty much fine except that you define shiprate inside the function
which overwrites the value you pass in. You should define the value
before you call the function 9which you are doing) but not set it inside
the function.
Then you only need to define one function which accepts the shiprate as
an argument.
One little extra is that you could define a default value for the
parameter to save you sup[plying it in the most common case:
def calc_weight(weight, shiprate=1.5):
total = weight * shiprate
Although the name does not describe what the function does so
a better name is probably calc_cost()
> if weight <= 10:
> shiprate = 1.5
> calc_weight_small(weight, shiprate)
Notice you call calc_weight_small() here....
> def cacl_weight_small(weight, shiprate):
But you define cacl_weight_small() here.
That won't work so either you did not send us
your real code or you have a bug waiting to bite...
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list