Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #97085 > unrolled thread
| Started by | codywcox@gmail.com |
|---|---|
| First post | 2015-09-24 11:45 -0700 |
| Last post | 2015-09-25 22:55 -0700 |
| Articles | 16 — 11 participants |
Back to article view | Back to comp.lang.python
Learning Modules, Arguments, Parameters (imma noob) codywcox@gmail.com - 2015-09-24 11:45 -0700
Re: Learning Modules, Arguments, Parameters (imma noob) MRAB <python@mrabarnett.plus.com> - 2015-09-24 20:25 +0100
Re: Learning Modules, Arguments, Parameters (imma noob) John Gordon <gordon@panix.com> - 2015-09-24 19:32 +0000
Re: Learning Modules, Arguments, Parameters (imma noob) Larry Hudson <orgnut@yahoo.com> - 2015-09-24 13:27 -0700
Re: Learning Modules, Arguments, Parameters (imma noob) alister <alister.nospam.ware@ntlworld.com> - 2015-09-25 10:42 +0000
Re: Learning Modules, Arguments, Parameters (imma noob) Cody Cox <codywcox@gmail.com> - 2015-09-25 11:50 -0700
Re: Learning Modules, Arguments, Parameters (imma noob) Laura Creighton <lac@openend.se> - 2015-09-25 22:15 +0200
Re: Learning Modules, Arguments, Parameters (imma noob) Laura Creighton <lac@openend.se> - 2015-09-25 22:25 +0200
Re: Learning Modules, Arguments, Parameters (imma noob) Cody Cox <codywcox@gmail.com> - 2015-09-25 13:52 -0700
Re: Learning Modules, Arguments, Parameters (imma noob) Terry Reedy <tjreedy@udel.edu> - 2015-09-25 20:08 -0400
Re: Learning Modules, Arguments, Parameters (imma noob) Cody Cox <codywcox@gmail.com> - 2015-09-25 11:53 -0700
Re: Learning Modules, Arguments, Parameters (imma noob) Cody Cox <codywcox@gmail.com> - 2015-09-25 12:03 -0700
Re: Learning Modules, Arguments, Parameters (imma noob) Ian Kelly <ian.g.kelly@gmail.com> - 2015-09-25 13:20 -0600
Re: Learning Modules, Arguments, Parameters (imma noob) Denis McMahon <denismfmcmahon@gmail.com> - 2015-09-25 23:59 +0000
Re: Learning Modules, Arguments, Parameters (imma noob) wxjmfauth@gmail.com - 2015-09-26 00:02 -0700
Re: Learning Modules, Arguments, Parameters (imma noob) Larry Hudson <orgnut@yahoo.com> - 2015-09-25 22:55 -0700
| From | codywcox@gmail.com |
|---|---|
| Date | 2015-09-24 11:45 -0700 |
| Subject | Learning Modules, Arguments, Parameters (imma noob) |
| Message-ID | <7ad8941d-04aa-42c5-82e9-10cdf02ab695@googlegroups.com> |
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()
[toc] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2015-09-24 20:25 +0100 |
| Message-ID | <mailman.141.1443122761.28679.python-list@python.org> |
| In reply to | #97085 |
On 2015-09-24 19:45, codywcox@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
> '''
>
First of all, it looks like your indentation is slightly off because
"def main" line is that the left margin and the other 2 "def" lines and
the "main()" line aren't.
> def main():
You're not passing anything into 'get_input', neither are you saving
any result that it might be returning.
> get_input()
You're not passing anything into 'convert_kilo'.
> convert_kilo()
>
>
You're defining 'get_input' to accept 1 parameter 'kilo'. Why? You're
assigning to 'kilo' in the first line, so any value you _did_ pass in
would be ignored.
> def get_input(kilo):
> kilo = float(input('Enter Kilometers: '))
> return kilo
>
You're defining 'convert_kilo' to accept 2 parameters. You're using the
'kilo' parameter, but not the 'miles' parameter because you're
assigning to it on the first line, so any value you _did_ pass in would
be ignored.
> def convert_kilo(kilo,miles):
'kilo' is already a float, and you're multiplying it by a float, so the
'float' call is pointless.
> miles = float(kilo * 0.6214)
> print( kilo,' kilometers converts to ',miles,' miles')
>
> main()
>
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2015-09-24 19:32 +0000 |
| Message-ID | <mu1j3u$mm2$1@reader1.panix.com> |
| In reply to | #97085 |
In <7ad8941d-04aa-42c5-82e9-10cdf02ab695@googlegroups.com> codywcox@gmail.com writes:
> 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?
> def get_input(kilo):
> kilo = float(input('Enter Kilometers: '))
> return kilo
get_input() gets all its input from the keyboard. It doesn't need any
arguments.
> def convert_kilo(kilo,miles):
> miles = float(kilo * 0.6214)
> print( kilo,' kilometers converts to ',miles,' miles')
convert_kilo() calculates miles on its own, so you don't need to pass it
as an argument.
> def main():
> get_input()
> convert_kilo()
>
> main()
When calling get_input, you need to save the return value in a variable,
and then pass that variable as an argument to convert_kilo.
The updated code would look like this:
def main():
kilo = get_input()
convert_kilo(kilo)
def get_input():
kilo = float(input('Enter Kilometers: '))
return kilo
def convert_kilo(kilo):
miles = float(kilo * 0.6214)
print( kilo,' kilometers converts to ',miles,' miles')
main()
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Larry Hudson <orgnut@yahoo.com> |
|---|---|
| Date | 2015-09-24 13:27 -0700 |
| Message-ID | <ZOGdnboVJ4o3wZnLnZ2dnUU7-WednZ2d@giganews.com> |
| In reply to | #97085 |
On 09/24/2015 11:45 AM, codywcox@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 -=-
[toc] | [prev] | [next] | [standalone]
| From | alister <alister.nospam.ware@ntlworld.com> |
|---|---|
| Date | 2015-09-25 10:42 +0000 |
| Message-ID | <mu38fj$gec$1@speranza.aioe.org> |
| In reply to | #97085 |
On Thu, 24 Sep 2015 11:45:06 -0700, codywcox 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()
clearly a homework exercise but you have asked for help in understanding
& not just for a solution which is good.
as others have pointed out you are throwing away the data returned from
get_input
I would also suggest that you return the result form convert_kilo instead
of printing it in the function & print it in the main loop instead.
kilo=get_input()
miles=convert_kilo(kilo)
print (miles)
it does not matter for this trivial assignment but as you progress
further you will discovery it is better to keep output seperated from the
main logic of the program.
--
What is mind? No matter. What is matter? Never mind.
-- Thomas Hewitt Key, 1799-1875
[toc] | [prev] | [next] | [standalone]
| From | Cody Cox <codywcox@gmail.com> |
|---|---|
| Date | 2015-09-25 11:50 -0700 |
| Message-ID | <f89010c3-5041-402c-adec-3e613d8a76d2@googlegroups.com> |
| In reply to | #97085 |
Awesome guys! Thank you for helping me understand this material. Parameters and Arguments are tricky. Looks like its mainly a game of connect the dots with variables. lol. When you return a variable, it needs somewhere to go, and that's why it goes to the next call into the argument area if I need to use that piece of information for a calculation, right? so if I understand correctly, when I create a function and return a variable, that variable needs to go in the argument when it is called? or into another functions argument for calculation? I apologize if I sound dumb. lol. I know this must be very elementary. Yes this is a homework assignment, but I was trying to figure out how parameters and arguments work, not get an answer, this was a huge help. I will keep looking over your details over and over to make sure I understand. Appreciate all the help and understanding! :) -Cody
[toc] | [prev] | [next] | [standalone]
| From | Laura Creighton <lac@openend.se> |
|---|---|
| Date | 2015-09-25 22:15 +0200 |
| Message-ID | <mailman.167.1443212140.28679.python-list@python.org> |
| In reply to | #97123 |
In a message of Fri, 25 Sep 2015 11:50:10 -0700, Cody Cox writes:
>Awesome guys! Thank you for helping me understand this material. Parameters and Arguments are tricky. Looks like its mainly a game of connect the dots with variables. lol.
>
>When you return a variable, it needs somewhere to go, and that's why it goes to the next call into the argument area if I need to use that piece of information for a calculation, right?
No.
>so if I understand correctly, when I create a function and return a variable, that variable needs to go in the argument when it is called? or into another functions argument for calculation?
>I apologize if I sound dumb. lol. I know this must be very elementary.
No.
>Yes this is a homework assignment, but I was trying to figure out how parameters and arguments work, not get an answer, this was a huge help. I will keep looking over your details over and over to make sure I understand.
>
>Appreciate all the help and understanding! :)
>
>-Cody
You have a very basic conceptual misunderstanding.
def main():
count = 0
animals = 'tigers'
change_my_value_to_50_frogs(count, animals)
print ("I fought", count, animals)
def change_my_value_to_50_frogs(count, animals):
count = 50
animals = 'frogs'
main()
-------------
I am pretty sure you expect this code to print 'I fought 50 frogs'.
It doesn't. It prints 'I fought 0 tigers'.
You have the idea that when you pass parameters to a function, by name,
you want the values of those parameters to get changed by the function.
(and I sort of cheated by calling the function change_my_value).
This is not what happens. You cannot change values this way.
so we rewrite change_my_value_to_50_frogs
def fifty_frogs():
count = 50
animals = 'frogs'
return count, animals
Now fifty_frogs is going to happily return the changed values you wanted.
But you have to set up your main program to receive those changed values
you wanted.
so we rewrite main as:
def main():
count = 0
animals = 'tigers'
count, animals = fifty_frogs()
print ("I fought", count, animals)
now
main()
says 'I fought 50 frogs'.
NOTE: there is nothing, nothing, nothing special about the fact that
in fifty_frogs I used the names 'count' and 'animals'.
Let us try a new version of fifty_frogs()
def fifty_frogs():
experience = 50
monsters = 'frogs'
return experience, monsters
All the same, just different words.
Does that matter?
No. You are going to return whatever is called 'experience' and
whatever is called 'monsters' and assign them to 'count' and 'monsters'.
Whether you name these things the same thing, or different things
makes no difference.
Does this make sense?
If not, come back with more questions. And play around with the
python interactive interpreter, or idle -- that is what it is for.
neat little experiements like this. :)
Laura
[toc] | [prev] | [next] | [standalone]
| From | Laura Creighton <lac@openend.se> |
|---|---|
| Date | 2015-09-25 22:25 +0200 |
| Message-ID | <mailman.168.1443212735.28679.python-list@python.org> |
| In reply to | #97123 |
In a message of Fri, 25 Sep 2015 22:15:26 +0200, Laura Creighton writes: >No. You are going to return whatever is called 'experience' and >whatever is called 'monsters' and assign them to 'count' and 'monsters'. ARRGH! I meant assign them to 'count' and 'animals'. (I read that 3 times and _still_ got it wrong!) I am very sorry for this confusion. Laura
[toc] | [prev] | [next] | [standalone]
| From | Cody Cox <codywcox@gmail.com> |
|---|---|
| Date | 2015-09-25 13:52 -0700 |
| Message-ID | <74135f8c-9e1c-486b-b474-e12278911c09@googlegroups.com> |
| In reply to | #97128 |
On Friday, September 25, 2015 at 1:26:02 PM UTC-7, Laura Creighton wrote: > In a message of Fri, 25 Sep 2015 22:15:26 +0200, Laura Creighton writes: > > >No. You are going to return whatever is called 'experience' and > >whatever is called 'monsters' and assign them to 'count' and 'monsters'. > > ARRGH! I meant > assign them to 'count' and 'animals'. > (I read that 3 times and _still_ got it wrong!) > I am very sorry for this confusion. > Laura All good, I got the idea, and you explained it very nicly. thank you for this example. I shall be back with more questions! haha. -Cody
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2015-09-25 20:08 -0400 |
| Message-ID | <mailman.169.1443226102.28679.python-list@python.org> |
| In reply to | #97123 |
On 9/25/2015 2:50 PM, Cody Cox wrote: > Awesome guys! Thank you for helping me understand this material. > Parameters and Arguments are tricky. Looks like its mainly a game of > connect the dots with variables. lol. If you stick with the convention that parameters are names in the header of a function definition, arguments are objects in a function call, and calls bind parameter names to argements (or sometimes collected arguments), then it is not so tricky. But also remember that not everyone follows this convention, or even any consistent usage. To reiterate about calling and binding. def f(x): pass f(2) binds the name 'x' to the int object with value 2. This is essentially the same as 'x = 2', except that the binding takes place in the local namespace of the function rather than in the local namespace of the call. -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Cody Cox <codywcox@gmail.com> |
|---|---|
| Date | 2015-09-25 11:53 -0700 |
| Message-ID | <683e63d0-e7d0-4d88-96d8-30878d595499@googlegroups.com> |
| In reply to | #97085 |
Oh, i also noticed that declaring the variable I was using and setting it =0.0 helped me out, seems the program had "garbage" in it... (that's what my professor said.)
[toc] | [prev] | [next] | [standalone]
| From | Cody Cox <codywcox@gmail.com> |
|---|---|
| Date | 2015-09-25 12:03 -0700 |
| Message-ID | <bd1ede95-8b5c-44ba-bb7a-9289e5568bf8@googlegroups.com> |
| In reply to | #97085 |
#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 covert it to miles
# Miles = Kilometers * 0.6214
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 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)
convert_to_kilometers(kilo)
def get_input(kilo):
kilo =float(input('Enter the amount of Kilometers: '))
return kilo
def convert_to_kilometers(kilo):
miles = kilo * .6214
print(kilo,'Kilometers converts to: ',miles, ' Miles.')
main()
'''
This was a great learning experience trying to understand modules
and how parameters & arguments work together.
Thanks for helping me understand
'''
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2015-09-25 13:20 -0600 |
| Message-ID | <mailman.166.1443208890.28679.python-list@python.org> |
| In reply to | #97125 |
On Fri, Sep 25, 2015 at 1:03 PM, Cody Cox <codywcox@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.
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-09-25 23:59 +0000 |
| Message-ID | <mu4n59$dtf$1@dont-email.me> |
| In reply to | #97125 |
On Fri, 25 Sep 2015 12:03:43 -0700, Cody Cox wrote:
> #Design a modular program that asks the user to enter a distance in
> kilometers and then covert it to miles # Miles = Kilometers * 0.6214
#!/usr/bin/python
# main calls the input routine to get the km value, then
# calls the conversion routine to convert the km value
# to miles, then prints the output
def main():
km = get_kms()
mi = convert_km_mi(k)
print "{} Km is {} miles.".format(km, mi)
# get_float_input() reads a float input using the supplied
# prompt.
def get_float_input(prompt):
return float(input(prompt))
# Get kms uses the generic get_float_input to read a km
# value
def get_kms():
return get_float_input("Enter Kms: ")
# generic conversion function
def convert_float_a_b(a, factor):
return float(a * factor)
# convert km_mi uses the generic converter
# to convert km to miles
def convert_km_mi(km):
return convert_float_a_b(km, 0.6214)
# now call main to kick it all off
main()
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | wxjmfauth@gmail.com |
|---|---|
| Date | 2015-09-26 00:02 -0700 |
| Message-ID | <0ca72920-576b-4767-8309-ca53d5f60ccf@googlegroups.com> |
| In reply to | #97130 |
>
> print "{} Km is {} miles.".format(km, mi)
>
Prefix of SI units: km and not Km (lowercase "k").
http://www.bipm.org/en/about-us/
jmf
[toc] | [prev] | [next] | [standalone]
| From | Larry Hudson <orgnut@yahoo.com> |
|---|---|
| Date | 2015-09-25 22:55 -0700 |
| Message-ID | <5O6dnZH9L9vfrpvLnZ2dnUU7-aGdnZ2d@giganews.com> |
| In reply to | #97125 |
You've already received a lot of answers and guidance, but here is on more point...
On 09/25/2015 12:03 PM, Cody Cox wrote:
[snip]
> this I am not sure about, I set Kilo=get_input(kilo), ...
Watch your capitalization! Kilo is _NOT_ the same as kilo. Case is significant in Python (as
well as in many other programming languages).
Also, as has already been pointed out: what you want here is kilo=get_input(). Along with the
corresponding change to the get_input() definition. This function does NOT need a passed parameter.
-=- Larry -=-
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web