[Tutor] Function returns 'None'

2010-07-11 Thread Dominik Danter

Hello

As en exercise I wrote the following function:


def recursfac(x,carryover=1):
print 'x:',x,'carryover:', carryover
if x > 1:
carryover *= x
recursfac(x-1, carryover)
else:
return carryover

print recursfac(3)

Very much to my surprise I get the following output:

x: 3 carryover: 1
x: 2 carryover: 3
x: 1 carryover: 6
None

Where did I go wrong?

Kind regards
Dominik Danter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Function returns 'None'

2010-07-11 Thread Adam Bark

On 11/07/10 14:59, Dominik Danter wrote:

Hello

As en exercise I wrote the following function:


def recursfac(x,carryover=1):
print 'x:',x,'carryover:', carryover
if x > 1:
carryover *= x
recursfac(x-1, carryover)
else:
return carryover

print recursfac(3)

Very much to my surprise I get the following output:

x: 3 carryover: 1
x: 2 carryover: 3
x: 1 carryover: 6
None

Where did I go wrong?

Kind regards
Dominik Danter


I made a diagram to try to explain

recursfac(3)
recursfac(2, 3) <|
recursfac(1, 6)  _|

As you can see recursfac(1,6) returns it's value (carryover) to 
recursfac(2, 3) which ignores it and completes it's execution ie 
returning None to your original call which then prints out that return 
value.

I hope that's clear.

Adam.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Function returns 'None'

2010-07-11 Thread Alan Gauld


"Dominik Danter"  wrote


def recursfac(x,carryover=1):
print 'x:',x,'carryover:', carryover
if x > 1:
carryover *= x
recursfac(x-1, carryover)


No return value here so when the reursed function returns carryover
we have nowhere to go in the calling function so we return None.


else:
return carryover


This returns a value to the calling function, but in 
the recursive case that returned vaklue uis thrown away 
as the comment above shows.


you need to add a return statement where you call recursively.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Function returns 'None'

2010-07-11 Thread Nick Raptis

On 07/11/2010 04:59 PM, Dominik Danter wrote:

Hello

As en exercise I wrote the following function:


def recursfac(x,carryover=1):
print 'x:',x,'carryover:', carryover
if x > 1:
carryover *= x
recursfac(x-1, carryover)
else:
return carryover

print recursfac(3)

Very much to my surprise I get the following output:

x: 3 carryover: 1
x: 2 carryover: 3
x: 1 carryover: 6
None

Where did I go wrong?

Your problem is that you expect the "return" to exit the recursion 
altogether. Instead, carryover is passed to the previous level of the 
recursion, which has nothing more to execute and returns None.


So the first step to fix this would be to make sure that your function 
returns carryover no matter what:


def recursfac(x,carryover=1):
print 'x:',x,'carryover:', carryover
if x > 1:
carryover *= x
recursfac(x-1, carryover)
return carryover
else:
return carryover

Or simply (to remove the code duplication):

def recursfac(x,carryover=1):
print 'x:',x,'carryover:', carryover
if x > 1:
carryover *= x
recursfac(x-1, carryover)
return carryover

Now there's still one more problem. The output is this:

x: 3 carryover: 1
x: 2 carryover: 3
x: 1 carryover: 6
3

Why is it returning 3 istead of 6?

Well, the function didn't catch the returned carryover value on the way 
up, so it's just returns what it knows: the value of carryover that it 
self has computed.

Instead, you have to do this:

def recursfac(x,carryover=1):
print 'x:',x,'carryover:', carryover
if x > 1:
carryover *= x
carryover = recursfac(x-1, carryover)
return carryover

And this returns
x: 3 carryover: 1
x: 2 carryover: 3
x: 1 carryover: 6
6

Done!

What you should learn from this is that, when doing recursion, figuring 
out what your function should do on the way up is as crucial as what you 
want it to do on the way down.


Nick


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Function returns 'None'

2010-07-11 Thread Nick Raptis

On 07/11/2010 06:28 PM, Nick Raptis wrote:


def recursfac(x,carryover=1):
print 'x:',x,'carryover:', carryover
if x > 1:
carryover *= x
carryover = recursfac(x-1, carryover)
return carryover

And this returns
x: 3 carryover: 1
x: 2 carryover: 3
x: 1 carryover: 6
6

Done!



Also, I realized that my final code may be tough to decipher now.. A 
nicer way to write it would be (the functionality is still exactly the 
same):


def recursfac(x,carryover=1):
print 'x:',x,'carryover:', carryover
if x > 1:
carryover *= x
result = recursfac(x-1, carryover)
else:
# done with recursion, start our way up
result = carryover
return result


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Function returns 'None'

2010-07-11 Thread Luke Paireepinart
I think the new version is harder to understand.

Sent from my iPhone

On Jul 11, 2010, at 10:43 AM, Nick Raptis  wrote:

> On 07/11/2010 06:28 PM, Nick Raptis wrote:
>> 
>> def recursfac(x,carryover=1):
>>print 'x:',x,'carryover:', carryover
>>if x > 1:
>>carryover *= x
>>carryover = recursfac(x-1, carryover)
>>return carryover
>> 
>> And this returns
>> x: 3 carryover: 1
>> x: 2 carryover: 3
>> x: 1 carryover: 6
>> 6
>> 
>> Done!
>> 
> 
> Also, I realized that my final code may be tough to decipher now.. A nicer 
> way to write it would be (the functionality is still exactly the same):
> 
> def recursfac(x,carryover=1):
>print 'x:',x,'carryover:', carryover
>if x > 1:
>carryover *= x
>result = recursfac(x-1, carryover)
>else:
># done with recursion, start our way up
>result = carryover
>return result
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Function returns 'None'

2010-07-11 Thread Nick Raptis

On 07/11/2010 06:50 PM, Luke Paireepinart wrote:

I think the new version is harder to understand.

Sent from my iPhone

On Jul 11, 2010, at 10:43 AM, Nick Raptis  wrote:

Aww! A critic! You humble me (really, I'm not being sarcastic here, I 
welcome it gladly)


I won't argue about it, though. If you prefer it, the last version is 
still there :)


My reasoning is that, with the new variable name ('result') in place, 
now it is evident (to me at least) that 'passover' is passed on the way 
down, while 'result' is passed on the way up. Harder it not, it seems 
more ""clean""


Also, another preference of mine, would you be kind enough to answer to 
the list and cc the original poster if you can? Doing it the other way 
around breaks my (quite stupid I admit) filters, and perhaps others' too.


Thanks for the feedback.
Nick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Function returns 'None'

2010-07-11 Thread Luke Paireepinart
On Jul 11, 2010, at 11:14 AM, Nick Raptis  wrote:

>> 
>> 
>> 
> 
> Also, another preference of mine, would you be kind enough to answer to the 
> list and cc the original poster if you can? Doing it the other way around 
> breaks my (quite stupid I admit) filters, and perhaps others' too.
That's the default email setup for the iPhone. I agree it's silly but it seems 
quite difficult to modify the fields. I see your point about the variable names 
but if it's at least fairly obvious what you're intending to do it seems like a 
bit of a waste to have an extra variable and else block. But you know, potato 
potato. It's negligible from a performance standpoint, it's really just a 
question of readability. Do what makes you ( and the people who will read your 
code) happy.
-luke
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Path?

2010-07-11 Thread Jim Byrnes
I am running Ubuntu.  I downloaded the source code examples for a book I 
purchased.  Some of the examples load image files located in the same 
directory as the program.  If I go to the current directory in the 
terminal the program can use the image files.  However, if I use a 
launcher or the filemanager it pops up an error dialog saying the file 
does not exist even though it is in the same directory.


The program simply uses the files name.  Is there a way without editing 
the source and inserting the full path to run the program from a 
launcher or the filemanager and allow it to see files in the current 
directory?


Thanks,  Jim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] feedback on permutations script

2010-07-11 Thread Isaac
> On Sun, 11 Jul 2010 02:48:38 am Isaac wrote:
> >* This is my interpretation of an algorithm to generate all
*> >* permutations of items in a list. I would appreciate any feedback
*> >* regarding advice for improvements.
**> Steven D'Aprano* wrote:
*Sun Jul 11 05:29:58 CEST 2010*
> Some of this is going to just be personal style, some of this is going
> to be slightly stronger semi-official recommended style (PEP 8 if you
> want to google further), and right at the end some hints regarding the
> algorithm.

Thank you for your vaulable insight and time. I'll make some changes.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Path?

2010-07-11 Thread Adam Bark

On 11/07/10 18:42, Jim Byrnes wrote:
I am running Ubuntu.  I downloaded the source code examples for a book 
I purchased.  Some of the examples load image files located in the 
same directory as the program.  If I go to the current directory in 
the terminal the program can use the image files.  However, if I use a 
launcher or the filemanager it pops up an error dialog saying the file 
does not exist even though it is in the same directory.


The program simply uses the files name.  Is there a way without 
editing the source and inserting the full path to run the program from 
a launcher or the filemanager and allow it to see files in the current 
directory?


Thanks,  Jim

Maybe create a bash script to call the python code something like:

#!/bin/bash

cd /directory/the/scripts/are/in
python script_name

HTH,
Adam.

PS if you want to use the same script for any python script you could 
change the last line to:


python $1

and call the bash script with the python script as the first argument
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] extract a submatrix

2010-07-11 Thread Bala subramanian
Friends,
Excuse me if this question is not appropriate for this forum. I have a
matrix of size 550,550. I want to extract only part of this matrix say first
330 elements, i dnt need the last 220 elements in the matrix. is there any
function in numpy that can do this kind of extraction. I am quite new to
numpy. How can do the same ?

Thank you,
Bala
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Path?

2010-07-11 Thread Steven D'Aprano
On Mon, 12 Jul 2010 03:42:28 am Jim Byrnes wrote:
> I am running Ubuntu.  I downloaded the source code examples for a
> book I purchased.  Some of the examples load image files located in
> the same directory as the program.  If I go to the current directory
> in the terminal the program can use the image files.  However, if I
> use a launcher or the filemanager it pops up an error dialog saying
> the file does not exist even though it is in the same directory.
>
> The program simply uses the files name.  Is there a way without
> editing the source and inserting the full path to run the program
> from a launcher or the filemanager and allow it to see files in the
> current directory?

What file manager are you using? Nautilus? Konqueror? Something else?

What do you mean, "use a launcher"? Use a launcher to do what? What sort 
of launcher?

What pops up an error dialog? The launcher?

Which file does it claim doesn't exist? Python? The Python script? The 
image file? What is the exact error message it gives?

There's probably a way to tell the launcher which working directory to 
use, but of course that depends on the answers to the above questions.



-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor