[Tutor] isbetween function chapter 5 How to Think Like Computer
Scientist
Chad Crabtree
flaxeater at yahoo.com
Tue Oct 19 15:30:10 CEST 2004
Eri Mendz wrote:
> Hello all,
>
> I'm a newbie trying to learn Python at my own pace. I work in sales
> NOT programming so please excuse me of obvious things i may
overlook
> in the course of my self-teaching.
>
> Here is the code i made trying to solve the exercise mentioned in
the
> subject of my email. I made it like so as i think pure if-elif-else
> statement is unwieldy. Of course its just me and pros here got
> something to correct my statement:
>
> #!/usr/bin/env python
> # filename: isbetween.py
> # description: make function with 3 parameters & determine the
median
> # Author: Eri Mendz
> # Tue Oct 19 15:07:22 AST 2004
> # CYGWIN_NT-5.0
>
> def test1(x,y,z):
> if x > y and x > z: # x is highest
> if y > z: # z-y-x
> print y, "is between", z, "and", x
> else: # y-z-x
> print z, "is between", y, "and", x
>
> def test2(x,y,z):
> if y > x and y > z: # y is highest
> if x > z: # z-x-y
> print x, "is between", z, "and", y
> else:
> print z, "is between", x, "and", y
>
> def test3(x,y,z):
> if z > x and z > y: # z is highest
> if x > y: # y-x-z
> print x, "is between", y, "and", z
> else:
> print y, "is between", x, "and", z
>
> def isbetween(x,y,z):
> test1()
> test2()
> test3()
>
> # test isbetween()
> isbetween(23,10,56)
>
> I get:
> TypeError: test1() takes exactly 3 arguments (0 given)
>
> Dumb me i didnt supplied arguments as required. But do i really
have
> to do that? I like the 3 functions to act as dummy functions (sort
of)
> and let the real work happen inside isbetween(). Kindly enlighten
me
> how to do it right.
>
I would rewrite isbetween() as thus
def isbetween(x,y,z):
test1(x,y,z)
test2(x,y,z)
test3(x,y,z)
as far as I know there really is no way around it. I thought up
another
one just for the heck of it using lists and [].sort()
def isbetween(x,y,z):
lst=[x,y,z]
lst.sort()
print lst[1], "is between" , lst[0],"and",lst[2]
isbetween(3,7,4)
4 is between 3 and 7
I put the values in a list then use [x,y,z].sort() to put the items
in
order then I print it out. If you want to see how sort works better
go
to the python command line and type help([]) or look at built-in
types
on the library reference. Just as a note [].sort() does it's job in
place so it does not return the sorted list.
Happy Hacking.
_______________________________
Do you Yahoo!?
Declare Yourself - Register online to vote today!
http://vote.yahoo.com
More information about the Tutor
mailing list