[Tutor] where is this returning from??
Kent Johnson
kent_johnson at skillsoft.com
Wed Aug 25 16:17:06 CEST 2004
It's the line "print chantTwice(p1, p2)"
This is the same as
val = chantTwice(p1, p2)
print val
chantTwice prints "this is part onethis is part two this is part onethis is
part two" before it returns. So what is val? It is the value returned from
chantTwice. You don't return a value from chantTwice, so val is set to
None. (In Python, every function returns a value. If you don't explicitly
tell it what to return, it returns None.) Then "print val" will output "None".
To make this clear, change chantTwice to this:
def chantTwice(p1,p2):
cat = p1 + p2
printTwice(cat)
return 'chantTwice return value'
Now the output looks like this:
What next?
home
this is part onethis is part two this is part onethis is part two
chantTwice return value
To make the program do what you originally intended, don't print the result
of chantTwice(), just call it without the print.
Kent
At 09:47 AM 8/25/2004 -0400, Reed L. O'Brien wrote:
>OK silly question. I am going throught a book about Python so I can learn
>it. Following (loosely) the book I made a script. When I run it all is
>as expected. Exept when calling the first branch of the if statement that
>calls chantTwice that calls printTwice. It always returns the appropriate
>answer followed by \n None (outputbelow script). I can seem to find where
>it is getting none from!! Can someone tell me what I am missing so I can
>move on. I just am not seeing it.
>
>
>#!/usr/local/bin/python
>
>p1 = 'this is part one'
>p2 = 'this is part two'
>verb = 'run'
>noun = 'home'
>nickname = 'abode'
>feeling = 'home sweet home'
>alien = 'work'
>name = raw_input("What next?\n")
>#name = input(prompt)
>def printTwice(output):
> print output, output
>
>def chantTwice(p1,p2):
> cat = p1 + p2
> printTwice(cat)
>
>def thought(name):
> if name == noun:
> print chantTwice(p1, p2)
> elif name == verb + ' ' + noun:
> print feeling
> elif name == nickname:
> print 'The most beautiful place ever is my ' + nickname
> else:
> print 'I want to be home!!!'
>
>thought(name)
>
>
>
>
>%python why.py
>What next?
>home
>this is part onethis is part two this is part onethis is part two
>None
>
>--
>4.6692016090
>'cmVlZEBpbnRlcnNpZWdlLmNvbQ==\n'.decode('base64')
>
>_______________________________________________
>Tutor maillist - Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list