[Tutor] Compare function!
Alan Gauld
alan.gauld at btinternet.com
Mon Oct 14 23:07:27 CEST 2013
On 14/10/13 15:41, Sammy Cornet wrote:
> . In which I´m want to get the user to prompt for the values of a and b.
So where is the code where you prompt the user and read input.
All we can see is a single function?
> For some reason I can´t run it. The follwing is what I have written on
> my script:
>
> def comparefunc (a, b):
> a = 3
> b = 4
The above assignments make your parameters useless since you have
overwritten them
This function effectively compares 3 and 4 only. As such the result will
always return -1.
Having func as part of the name is usually considered a bad idea.
We can see its a function by the way we use it. The only time
having func in the name is useful is when you are passing it
as a parameter to another function or storing it in another
variable. But I suspect that's several learning steps away
from you just now.
> if a < b:
> return 1
> elif a == b:
> return 0
> elif a < b:
> return -1
> print "return"
This all looks OK except putting prints inside a function like this is
usually only good as a debugging aid. You probably want to lose that
once you have it working correctly.
> And, this is what I get for my output:
> >>> ================================ RESTART
> ================================
That may be because you define the function but don't
seem to call it at any point.
You need to remove the two assignments for a and b
Then implement the get input calls to get the values
from the user then call the function and finally print
the result.
Something like this:
a = int(raw_input('gimme an a '))
b = int(raw_input('gimme a b '))
result = camparefunc(a,b)
print result
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