[Tutor] isbetween function chapter 5 How to Think Like Computer Scientist

Lloyd Kvam pythonTutor at venix.com
Tue Oct 19 15:57:49 CEST 2004


On Tue, 2004-10-19 at 08:45, 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.

This is the best way (to my mind) to adjust your code:
def isbetween(x,y,z):
	def test1():
	# the values for x,y,z are picked up from the enclosing isbetween function
	   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
	# repeat for test2 & test3
   test1()
   test2()
   test3()

However, there are a couple of points to notice.  Python supports
statements like:
	if x > y > z: print "x is highest and y is between z and x"

This program really relates labels to values.  There is not a lot of
benefit to using python variable names to match the labels.  So if you
had a list of (value, label) pairs, you could simply sort the list. And
then display the labels in the correct order.  Given:
	value_label_list = [(23,'x'),(10,'y'),(56,'z')]
	value_label_list.sort()
	value_label_list.reverse()
	print "Variables from largest to smallest:"
	for value,label in value_label_list:
		print "     ", label
(This glosses over possible equalities, but so does the original code)
Now the problem changes to one of creating the list.

I took a look at the problem definition.  It simply requests testing one
particular order of variables and returning a boolean - a true / false
value.  I'm not sure where list handling comes in the book.

-- 
Lloyd Kvam
Venix Corp



More information about the Tutor mailing list