Learning Modules, Arguments, Parameters (imma noob)

Ian Kelly ian.g.kelly at gmail.com
Fri Sep 25 15:20:47 EDT 2015


On Fri, Sep 25, 2015 at 1:03 PM, Cody Cox <codywcox at gmail.com> wrote:
> def main():
>     #set the variable to 0.0, makes it a float and creates a place in memory for the variable.
>     kilo = 0.0

This is addressing a symptom, not the actual problem. Initializing
kilo here prevents Python from complaining when you try to access the
kilo variable in the "kilo = get_input(kilo)" line below. However, you
don't need it there either.

> '''
>     this I am not sure about, I set Kilo=get_input(kilo), but why do I need the argument when
>     I am going to pass it to convert_to_kilometers? but the program works so I am guessing that is where it is saving
>      the variable in order to be passed? so it needs to be returned as an argument to be passed as an argument for
>      the next call???
>
> '''
>     kilo = get_input(kilo)

No, you don't need the argument at all. You use arguments to pass in
data that the function needs to do its work. In this case you're
passing in the *current* value of kilo, which you set above to be 0.0.
This isn't data that is needed by get_input, so you shouldn't pass it
in (and the get_input function should not require it).

The result of the get_input call is then assigned to kilo, replacing
the 0.0 which you don't need. It's fine for kilo to be initialized
with this line rather than the one above.

>     convert_to_kilometers(kilo)

This is okay, but as you get more advanced you will probably want this
function to return a value, which you could store in another variable,
e.g.:

    miles = convert_to_miles(kilo)

> def get_input(kilo):
>     kilo =float(input('Enter the amount of Kilometers: '))
>     return kilo

As noted above, this function should not have kilo as a parameter. It
never uses it. This function needs to return kilo *to* its caller, not
take a value for kilo *from* its caller.

> def convert_to_kilometers(kilo):
>     miles = kilo * .6214
>     print(kilo,'Kilometers converts to: ',miles, ' Miles.')

This is fine but poorly named. This function converts kilometers to
miles. It doesn't convert anything to kilometers as suggested by the
name.

And again, as you get more advanced you would probably want this to
return the value of miles rather than just print it.



More information about the Python-list mailing list