The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Quick Python question

supabeastsupabeast Registered User regular
edited November 2008 in Help / Advice Forum
I’m reading the book Learning Python and having trouble with some of the code examples as soon as it gets to nested statements. The code in the book generates syntax errors when run interactively, but not when I save it as a file and run it. Here’s an example:

>>>while True:
reply = raw_input('Enter Text')
if reply == 'stop': break
print reply.upper()
print 'Bye'
SyntaxError: invalid syntax

Why does 'print 'Bye' generate a syntax error here? I realize that this is probably irrelevant as I don’t really need to run all of the example code, but I’m generally confused as to WTF is going on.

supabeast on

Posts

  • grungeboxgrungebox Registered User regular
    edited November 2008
    It shouldn't give you a syntax error, you must have a typo or something when you're entering it in.

    grungebox on
    Quail is just hipster chicken
  • DocDoc Registered User, ClubPA regular
    edited November 2008
    Does it run through the same interpreter each time? Could they be different versions?

    Doc on
  • supabeastsupabeast Registered User regular
    edited November 2008
    No, I’m not entering typos. I can get a syntax error in the interpreter, copy/paste the failed code to a text file, save the text file, and then run it from the command line with no error. When I run it on my machine it’s always the same interpreter. I also tried running it using the Python interpreter on my remote FreeBSD account on a remote server and I have the same problem there.

    supabeast on
  • ArkArk Registered User regular
    edited November 2008
    I just tried running that in IDLE's interactive mode and it worked perfectly. Are you entering the code into the interpreter exactly as you have it here? If you are, try indenting every line after the "while True" line. That's pretty much the only thing I can think of.

    Ark on
  • Alistair HuttonAlistair Hutton Dr EdinburghRegistered User regular
    edited November 2008
    The forums have fubarred the formatting of your code. I assume it is formatted like so in the file you are trying to run:
    while True:
        reply = raw_input('Enter Text')
        if reply == 'stop': break
        print reply.upper()
    print 'Bye'
    

    I have done the indentation with 4 spaces, not tabs. If your code does look like this I have no idea why it's giving you a Syntax error.

    EDIT: Oh, you are getting an error while running it interactively! Sorry, misread what you said. Yeah, I just tried entering the prog into IDLE's interactive shell and it threw a syntax error. Curious. It's an IDLE quirk certainly as I entered it into the python command line interpreter and it worked as expected.

    Yeah,it's some bizzare IDLE quirk, when I made the program:
    >>> for i in range(0,1):
    	while True:
    		reply = raw_input('Enter Text')
    		if reply == 'stop':
    			break
    		print reply.upper()
    	print "Bye"
    

    It worked as expected without a Syntax Error. Use python24.exe (or whatever version of python you've got) for running stuff interactively, it actually works. Something odd is going on with the final line.

    Alistair Hutton on
    I have a thoughtful and infrequently updated blog about games http://whatithinkaboutwhenithinkaboutgames.wordpress.com/

    I made a game, it has penguins in it. It's pay what you like on Gumroad.

    Currently Ebaying Nothing at all but I might do in the future.
  • ArkArk Registered User regular
    edited November 2008
    Oh, I misunderstood what the program was supposed to do. I think maybe you can only enter one statement into the interpreter in IDLE at a time. So when you give it a while statement followed by a print statement, it gives a syntax error.

    Ark on
  • JaninJanin Registered User regular
    edited November 2008
    The interpreter is more limited than reading from a script; a blank line is needed before every top-level statement. If you enter this:
    >>>while True:
        reply = raw_input('Enter Text')
        if reply == 'stop': break
        print reply.upper()
    
    print 'Bye'
    

    it should work correctly

    Janin on
    [SIGPIC][/SIGPIC]
Sign In or Register to comment.