Learning Modules, Arguments, Parameters (imma noob)

Larry Hudson orgnut at yahoo.com
Thu Sep 24 16:27:22 EDT 2015


On 09/24/2015 11:45 AM, codywcox at gmail.com wrote:
> I seem to be having a problem understanding how arguments and parameters work, Most likely why my code will not run.
> Can anyone elaborate on what I am doing wrong?
>
> '''
> Cody Cox
> 9/16/2015
> Programming Exercise 1 - Kilometer Converter
> Design a modular program that asks the user to enter a distance in kilometers and then convert it to miles
> Miles = Kilometers * 0.6214
> '''
>
> def main():
>     get_input()
>     convert_kilo()
>
>
>   def get_input(kilo):
>     kilo = float(input('Enter Kilometers: '))
>     return kilo
>
>   def convert_kilo(kilo,miles):
>       miles = float(kilo * 0.6214)
>       print( kilo,' kilometers converts to ',miles,' miles')
>
>   main()
>
----------------------------------
 > def main():
 >    ...
I'm going to discribe this last

 >   def get_input(kilo):
 >     kilo = float(input('Enter Kilometers: '))
 >     return kilo
 >
1)  def get_input(kilo):  has a leading space.  This is an indenting error
2)  You use parameters to send data TO the function, NOT to return it (generally, there are 
exceptions).  Leave out the kilo, you want simply 'def get_input():'
3)  The body,  kilo = ... and return ..., is ok, but can be shortened to a single line:
         return float(input('Enter Kilometers: '))

 >   def convert_kilo(kilo,miles):
 >       miles = float(kilo * 0.6214)
 >       print( kilo,' kilometers converts to ',miles,' miles')
 >
1)  Again indenting error, and miles (an output) is not needed as a parameter.
2)  miles = float(kilo * 0.6214):  kilo and 0.6214 are already floats.  There is no need to 
_convert_ to float.  Doesn't hurt, but it is redundant.
3)  The print is acceptable, but has unnecessary spaces.  Print() by default puts a space 
between the items, so using spaces inside the strings gives you double-spaces, one from the 
print() and one from the string.

 > def main():
 >     get_input()
 >     convert_kilo()
 >
1)  get_input() as you have it written requires a parameter, you are not giving it one. 
However, as I explained above, get_input() should not have a parameter anyway.
2)  get_imput() returns a value (kilo), but here you are throwing it away.  This needs to be:
         kilo = get_input()
3)  convert_kilo() _requires_ a parameter, you are not giving one.  And it does NOT need the 
second (miles) parameter that you originally wrote.  What you want is:
        convert_kilo(kilo)

I hope you can make sense out of my explanations.

      -=- Larry -=-




More information about the Python-list mailing list