#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    [2.7.3] Return doesn't work properly


    Hi

    I am new to Python and programming generally. I am currently learning from some youtube vids made by thenewboston/bucky. Some of his tutorials doesn't seem to work though.
    I give a few examples:

    Code:
    def plusten(y):
       return y+10
    
    print plusten(44)
    This gives me: SyntaxError: invalid syntax

    and
    Code:
    def simple():
       print 5
       return 10
    
    simple()
    It gives me the same error

    If I just write:
    Code:
    return "hi"
    It gives me:
    SyntaxError: 'return' outside function

    Did I misunderstand something regarding use of return or did they change it in 2.7.3? I am using IDLE on a WinXP(32bit) laptop.

    Thanks in advance


    P.S. Is there an easier way of writing (or copy-pasting) the code in here and force it to do the intentations automatically?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    484
    Rep Power
    33
    " SyntaxError: invalid syntax" is not enough info. Post the entire error message. I am guessing that it has to do with the print statement as the programs run on my computer, but can not tell from the post.
    Last edited by dwblas; March 10th, 2013 at 04:11 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0
    There is no more error than that. I am not hiding anything
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    You are using python3. The program will work in python2. The problem is that print was changed between versions from a statement to a function.

    plusten = lambda y: y+10

    print(plusten(44))



    Automatic indentation? Write your code with emacs.

    PS. return works correctly.


    Sorry, that wasn't nice.
    Code:
    def plusten(a):
        return a+10
    
    print(plusten(202))
    Last edited by b49P23TIvg; March 10th, 2013 at 07:57 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    484
    Rep Power
    33
    The OP says they are using 2.7.3 which is why we have to have a complete error message to see if it is the print statement or something else like mixing tabs and spaces.
    or did they change it in 2.7.3
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    Hmm, I now see what you mean. The OP aleque is either wrong about the version

    >>> import sys
    >>> sys.version

    or like you say, the information is incorrect or incomplete. The error message is certainly incomplete. The post says syntax error, not indentation error. We both identify the print statement as primary suspect in this puzzle.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0
    I could not recreate the error using
    Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32

    seems to work perfectly for me.
  14. #8
  15. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65
    Perhaps the OP is on the python shell and has used print as a variable name somewhere before that code.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    But in python2 you cannot reassign print. It's a statement.
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0
    Please look at this screenshot. I also forgot to say that before installing 2.x version I have installed 3.x version of Python. Then I decided to uninstall the 3.x version and stick with 2.x

    dl.dropbox.com/u/10484650/pythonbug.JPG
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    484
    Rep Power
    33
    I understand now. The command line does things a text processor does not. You have to hit the Enter before keying in the final line, "simple()", as the command line thinks you are still in the function. Note the prompt change from "..." to ">>>". Better to use Idle instead of the command line and then go on to a text processor or IDE when you are ready. You forget things when you don't use them for years.
    Last edited by dwblas; March 11th, 2013 at 04:31 PM.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0
    haha that fixed the problem, thanks! Such a simple thing, as always. So I guess that "return" can only be used within methods and functions?
  24. #13
  25. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    python command line works as a desk calculator. If you want 5 you'd press the two keys "5" "<Enter>".

    Or

    >>> TAU = math.pi*2
    >>> (TAU * 5**2)/2
    78.whatever
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    Originally Posted by aleque
    haha that fixed the problem, thanks! Such a simple thing, as always. So I guess that "return" can only be used within methods and functions?
    Correct.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    customizing IDLE colors


    oops. I was supposed to make a new topic and not reply..

IMN logo majestic logo threadwatch logo seochat tools logo