Re: [Tutor] Re Help with this script

2005-04-29 Thread John Carmona
Hi Alan, I did not receive personally your last email but I have read it on 
the forum.

OK i understand now what you were talking about, sorry it took such a long 
time for me to see the solution, the good thing about it is that I am 
learning tons.

I will probably post soon again once I hit a wall on my next exercise. 
Thanks a million for your help (also Kent and John - I will try to rewrite 
your poem/exercise once I get a minute, thanks)

Regards
JC
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Re Help with this script

2005-04-28 Thread Alan Gauld

 OK Alan, I thing I have seen the light!!.

Almost. :-)

 
-
 def print_options():
print --
print Options:
print a. print options
print f. quit the programme
print --

 print_options()
 choice = 0
 while choice != 'f':
 print
 choice = raw_input(Choose an option: )
 if choice == 'a':
 print Here we go again
 print_options()
 if choice == 'f': break

That should be all you need.

 print_options()

This shouldn't be needed.

 Is it that if you use while 1: you create a recursive function?
Hope I am
 right.

NO the recursive bit is where the function calls itself.
In the previous version you had the while loop inside
the function so that you called print_options while you
were still inside print_options, like this:

def print_options():
   print --
   print Options:
   print a. print options
   print f. quit the programme
   print --
choice = 0
while choice != 'f':
  print
  choice = raw_input(Choose an option: )
  if choice == 'a':
  print Here we go again
  print_options()  ## THIS CALL IS INSIDE THE FUNCTION
  if choice == 'f': break

It was the fact that the call was inside the function that made
it recursive. When you selected f to quit you simply quit that
call to the function and returned to the higher level call and
had to select f again until you eventually got back to the top
level.. I'll try to draw it:

print_options()
choice = a
print_options()
   choice = a
   print_options()
 choice = a
 print_options()
choice = f
 choice = f
   choice = f
choice = f
exit

You needed to select f to exit each call to print_options.

Any clearer?

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Re Help with this script

2005-04-27 Thread John Carmona
OK Alan, I thing I have seen the light!!. Here is the script that Kent and 
you asked me to look at modified:

-
def print_options():
  print --
  print Options:
  print a. print options
  print f. quit the programme
  print --
print_options()
choice = 0
while choice != 'f':
   print
   choice = raw_input(Choose an option: )
   if choice == 'a':
   print Here we go again
   print_options()
   if choice == 'f': break
print_options()
-
Is it that if you use while 1: you create a recursive function? Hope I am 
right.

Thanks
JC

PREVIOUS EMAIL
OK the situation is that I haven't still found out what the answer
is, I
have noticed in the other hand that if I select the option a let's
say 4
times, I need to enter the option f 4 times. I am curious to know
what the
solution is. I have read your chapter on recursion but that did not
clear
anything.
OK, basically the problem is that you have unintentionally created
a recursive function. Every time you call it you create a new copy
of the function. When you exit the function you wind up back in
the previous copy. So as many times as you call the function you
have to exit it the same number of times to get back to the top
of your program.
Have a think about it, and see if that makes sense.
Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Re Help with this script

2005-04-27 Thread jfouhy
Quoting John Carmona [EMAIL PROTECTED]:

 Is it that if you use while 1: you create a recursive function? Hope I
 am right.

No ...

Remember how functions can call other functions?

def add(x, y):
 Add two integers together. 
return x+y

def mul(x, y):
 Multiply two integers together. 
if x == 0:
return 0
if x  0:
x = -x
product = y
while x  1:
product = add(product, y)
x = add(x, -1)

The mul function calls add to do some of its work for it.

A recursive functio nis just a function that calls itself.  For example, we
could rewrite add as:

def increment(x):
 Increment an integer. 
return x + 1

def decrement(x):
 Decrement an integer. 
return x - 1

def add(x, y):
 Add two integers together. 
if x == 0:
return y
if x == 1:
return increment(y)
if x == -1:
return decrement(y)
if x  0:
return add(-1, add(add(1, x), y))
else:
return add(1, add(add(-1, x), y))

In this case, add is a recursive function, because it makes calls to itself. 
There's nothing magical about recursive functions or recursion; you just have to
be a bit careful to make sure that your program will end.  

Example:

def poem():
print 'A mad metaprogrammer wrote a mad metaprogram,\n which started: ',
poem()
print 'sort of close, were the words that the programmer finally chose\nTo
bring his mad program to some sort of close.'

The poem() function will call the poem() function which will call the poem()
function which will call the poem() function, with no end in sight...

Another example:

def qsort(lst):
 Quicksort a list. 
if len(lst) = 1:
return lst
pivot = lst[0]
return qsort([x for x in lst if x  pivot]) + [pivot] + qsort([x for x in
lst if x  pivot])

This will stop because the argument in each recursive call to qsort is smaller
than the original, and because there is a smallest value ([]).

Does this help?

[Bonus questions:

1. Can you rewrite the recursive add() so that you only need one of (increment,
decrement)?

2. Do you recognise the inspiration for poem()?
]

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Re Help with this script

2005-04-26 Thread John Carmona
Hi Alan, sorry for not replying sooner I am right in the middle of setting 
up a network at home. Thanks for your email.

OK the situation is that I haven't still found out what the answer is, I 
have noticed in the other hand that if I select the option a let's say 4 
times, I need to enter the option f 4 times. I am curious to know what the 
solution is. I have read your chapter on recursion but that did not clear 
anything.

As soon as i get a minute I will look again.
Thanks
JC
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Re Help with this script

2005-04-26 Thread Alan Gauld
 OK the situation is that I haven't still found out what the answer
is, I
 have noticed in the other hand that if I select the option a let's
say 4
 times, I need to enter the option f 4 times. I am curious to know
what the
 solution is. I have read your chapter on recursion but that did not
clear
 anything.

OK, basically the problem is that you have unintentionally created
a recursive function. Every time you call it you create a new copy
of the function. When you exit the function you wind up back in
the previous copy. So as many times as you call the function you
have to exit it the same number of times to get back to the top
of your program.

Have a think about it, and see if that makes sense.

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Re Help with this script

2005-04-25 Thread John Carmona
Thanks Kent, as far as I can see I get the same problem that on my script, i 
need to enter f 3 to 4 times before I exit the programme. Hmmm, why, I 
can't see it really, i thought that the fact to have the break command 
would terminate the script straight away. If I enter f first then the 
programme ends.  What am i missing?

JC
John Carmona wrote:
	 	Thanks for the help Kent, Noel and Alan. Here is my final script (it 
seems to be working ok but sometimes if I select quit the programme, I 
need to enter that option 2 or 3 times before it works, is this a bug

Try this program. Choose option a a few times, then choose f enough times to 
exit. See if you can figure out what is going on.

def print_options():
  print --
  print Options:
  print a. print options
  print f. quit the programme
  print --
  while 1:
  choice = raw_input(Choose an option: )
  if choice == 'a':
  print 'About to call print_options'
  print_options()
  print 'Finished calling print_options'
  if choice == 'f': break
print_options()
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Re Help with this script

2005-04-25 Thread Alan Gauld
 can't see it really, i thought that the fact to have the break
command
 would terminate the script straight away.

break terminates the current loop, which is inside your print_options
function. print_options is called from inside print_options.

Research the term recursion and see if you can see where the problem
lies.
(Try my online tutor for one explanation...)

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


If I enter f first then the
 programme ends.  What am i missing?

 JC

 John Carmona wrote:

 Thanks for the help Kent, Noel and Alan. Here is my final script (it
 seems to be working ok but sometimes if I select quit the
programme, I
 need to enter that option 2 or 3 times before it works, is this a
bug


 Try this program. Choose option a a few times, then choose f enough
times to
 exit. See if you can figure out what is going on.

 def print_options():
print --
print Options:
print a. print options
print f. quit the programme
print --
while 1:
choice = raw_input(Choose an option: )
if choice == 'a':
print 'About to call print_options'
print_options()
print 'Finished calling print_options'
if choice == 'f': break

 print_options()

 Kent





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Re Help with this script

2005-04-24 Thread John Carmona
Thanks for the help Kent, Noel and Alan. Here is my final script (it seems 
to be working ok but sometimes if I select quit the programme, I need to 
enter that option 2 or 3 times before it works, is this a bug (I am running 
Win XP), please feel free to comment if you thing that something could be 
improved. Time to do the next exercise.
JC


#By J Carmona
#Programme that compute volumes or surfaces
##First menu is for the calculation of area
##Second menu is for the calculation of volume
##First ask the user what he wants to do
running = True
def area_rect():
   length = input(Length: )
   width = input (Width: )
   print The area is: ,length*width
def area_circ():
   radius = input(What is the radius?: )
   print The area is approximately: , 3.14159*(radius**2)
def area_squ():
   side = input (What is the length of one side?: )
   print The area is: , side*side
def area_tgle():
   base = input (What is the base of the triangle?: )
   heigth = input (What is the heigth of the triangle?: )
   print The area is: ,base*heigth/2
def vol_sph():
   radius = input(What is the radius?: )
   print The volume is: , (4*3.14159*radius**3)/3
def vol_cube():
   side = input(Side: )
   print The volume is: ,side**3
def vol_box():
   width = input (What is the width of the box?: )
   length = input (What is the length of the box?: )
   depth = input (What is the depth of the box?: )
   print The volume is: , width*length*depth
def vol_cone():
   radius = input (What is the radiux of the base of the cone?: )
   heigth = input (What is the heigth of the cone?: )
   print The volume is: , 0.*3.144159*(radius**2)*heigth
def task_options():
   print ---
   print Options:
   print a. Print options: 
   print b. Do you want to calculate areas?: 
   print c. Do you want to calculate volumes?: 
   print d. Quit the programme
   print ---
   choice = raw_input(Choose an option: )
   if choice == 'a':
   print task_options()
   elif choice == 'b':
   print print_options()
   elif choice == 'c':
   print print_options_2()
   elif choice == 'd':
   running = False
def print_options():
   print_options
   print --
   print Options:
   print a. print options
   print b. calculate circle area
   print c. calculate square area
   print d. calculate rectangle area
   print e. calculate triangle area
   print f. quit the programme
   print --
   while 1:
   choice = raw_input(Choose an option: )
   if choice == 'a':
   print_options()
   elif choice == 'b':
   area_circ()
   elif choice == 'c':
   area_squ()
   elif choice == 'd':
   area_rect()
   elif choice == 'e':
   area_tgle()
   if choice == 'f': break
def print_options_2():
   print --
   print Options:
   print a. print options
   print b. calculate the volume of a sphere
   print c. calculate the volume of a cube
   print d. calculate the volume of a box
   print e. calculate the volume of a cone
   print f. quit the programme
   print --
   while 1:
   choice = raw_input(Choose an option: )
   if choice == 'a':
   print_options()
   elif choice == 'b':
   vol_sph()
   elif choice == 'c':
   vol_cube()
   elif choice == 'd':
   vol_box()
   elif choice == 'e':
   vol_cone()
   elif choice == 'e':
   print_options()
   if choice == 'f': break
#Call starting menu
task_options()

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Re Help with this script

2005-04-24 Thread Kent Johnson
John Carmona wrote:
Thanks for the help Kent, Noel and Alan. Here is my final script (it 
seems to be working ok but sometimes if I select quit the programme, I 
need to enter that option 2 or 3 times before it works, is this a bug
Try this program. Choose option a a few times, then choose f enough times to exit. See if you can 
figure out what is going on.

def print_options():
   print --
   print Options:
   print a. print options
   print f. quit the programme
   print --
   while 1:
   choice = raw_input(Choose an option: )
   if choice == 'a':
   print 'About to call print_options'
   print_options()
   print 'Finished calling print_options'
   if choice == 'f': break
print_options()
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor