Newbie confusion about 'return' 
Author Message
 Newbie confusion about 'return'

I'm mystified. The same code in IDLE (or at the interactive prompt)
gives a different result compared to entering it into a file and running
it. I'm using python 2.0 from python .org on NT4. I checked on Solaris 8
with python 2.0 (interactive vs running the file) and got the same
result. The code is from "How to think like a computer scientist Chapter
7"
-----------------------------------
IDLE:

Quote:
>>> def find(str, ch):

              index = 0
              while index < len(str):
                      if str[index] == ch:
                             return index
                      index = index + 1
              return -1

Quote:
>>> find ("abcde", "d")

3 (in blue indicating stdout)

The above is what I expected.
------------------------------------

Running python on the file (find.py):

def find(str, ch):
      index = 0
      while index < len(str):
             if str[index] == ch:
                    return index
             index = index + 1
      return -1

find ("abcde", "d")

I ran it as: $ python find.py
*Nothing* was returned (just gave me the command prompt again), instead
of the '3' that I expected.
---------------------------------------

Why the difference? I thought one of the points of the interactive mode
was to test code to see if it works before putting it in a file...
If I add a couple of print statements in the file, I get the expected
result (3) from running 'python find.py':

def find(str, ch):
      index = 0
      while index < len(str):
             if str[index] == ch:
                   print index
                   return
             index = index + 1
      print -1
      return

find ("abcde", "d")

Thanks in advance.



Wed, 03 Sep 2003 07:33:01 GMT  
 Newbie confusion about 'return'

Quote:

>I'm mystified. The same code in IDLE (or at the interactive prompt)
>gives a different result compared to entering it into a file and running
>it. I'm using Python 2.0 from python .org on NT4. I checked on Solaris 8
>with python 2.0 (interactive vs running the file) and got the same
>result. The code is from "How to think like a computer scientist Chapter
>7"
>-----------------------------------
>IDLE:

>>>> def find(str, ch):
>              index = 0
>              while index < len(str):
>                      if str[index] == ch:
>                             return index
>                      index = index + 1
>              return -1

>>>> find ("abcde", "d")
>3 (in blue indicating stdout)

>The above is what I expected.

That's because IDLE itself is a Python program that captures the output
of return.  Try entering *any* Python expression, and its output will be
printed.  You don't want that in a regular program, though, so you need
to be explicit:

Quote:

>Running python on the file (find.py):

>def find(str, ch):
>      index = 0
>      while index < len(str):
>             if str[index] == ch:
>                    return index
>             index = index + 1
>      return -1

>find ("abcde", "d")

Try

print find ("abcde", "d")
--

Androgynous poly {*filter*} vanilla {*filter*} het Pythonista   http://www.*-*-*.com/
Hugs and backrubs -- I break Rule 6

Three sins: BJ, B&J, B&J



Wed, 03 Sep 2003 08:16:25 GMT  
 Newbie confusion about 'return'

<...>

Quote:
> IDLE:
<...>
> >>> find ("abcde", "d")
> 3 (in blue indicating stdout)

> The above is what I expected.
> ------------------------------------

> Running python on the file (find.py):
<...>
> find ("abcde", "d")

> I ran it as: $ python find.py
> *Nothing* was returned (just gave me the command prompt again), instead
> of the '3' that I expected.
> ---------------------------------------

> Why the difference? I thought one of the points of the interactive mode
> was to test code to see if it works before putting it in a file...
> If I add a couple of print statements in the file, I get the expected
> result (3) from running 'python find.py':

The interactive interpreter displays the value of the line when you
hit ENTER, a program will not produce output unless you ask for it
(errors excepted, of course).

Quote:
> def find(str, ch):
>       index = 0
>       while index < len(str):
>              if str[index] == ch:
>                    print index
>                    return
>              index = index + 1
>       print -1
>       return

> find ("abcde", "d")

or do this...
---8< find.py ---
def find(str, ch):
    index = 0
    while index < len(str):
        if str[index] == ch:
             return index
        index = index + 1
    return -1

print find("abcde", 'd')
--->8---

i.e., what you had for the second piece of code, except print out
the returned value.

- Bruce



Wed, 03 Sep 2003 08:13:43 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Newbie 'Absolute Expression' Confusion

2. return -code return doesn't play nice with set

3. NMTVCUSTOMDRAW does not return 'isChecked'

4. Intellimouse Returns 'File Not Open'

5. Clearing Error()'s and Errorcode()'s return values

6. Change request: return value from 'trap'

7. 'each' returning array

8. msvcrt.getch() from IDLE returns '\377'

9. 'read' returns more bytes than requested

10. Define a new specialised 'return' command

11. os.listdir ('.'): Solaris: returns empty list in directory with 215 files

12. WINDOW: 'exec' command returns immediately

 

 
Powered by phpBB® Forum Software