Jump to content


Photo

Syntax Error


  • Please log in to reply
6 replies to this topic

#1 tony7340

tony7340

    New Member

  • Members
  • Pip
  • 5 posts

Posted 27 May 2011 - 06:00 AM

I am having a syntax error with the code from pg 31:

def print_lol(the_list):
for each_item in the_list:
if isinstance(each_item, list):
print_lol(each_item)
else:
print(each_item)

I get file "(stdin)", line 1 and the parser is under the the "m" in item in last line

#2 tony7340

tony7340

    New Member

  • Members
  • Pip
  • 5 posts

Posted 27 May 2011 - 06:06 AM

I am having a syntax error with the code from pg 31:

def print_lol(the_list):
for each_item in the_list:
if isinstance(each_item, list):
print_lol(each_item)
else:
print(each_item)

I get file "(stdin)", line 1 and the parser is under the the "m" in item in last line

the post is not showng the indents I placed in the code

#3 tony7340

tony7340

    New Member

  • Members
  • Pip
  • 5 posts

Posted 27 May 2011 - 05:20 PM

I have attached a screenshot of the IDLE. I am probably missing something simple, but I have retyped and checked it at least a couple of dozen times.

Attached Thumbnails

  • py2.jpg


#4 paulbarry

paulbarry

    Advanced Member

  • O'Reilly Author
  • PipPipPipPipPipPipPipPip
  • 307 posts

Posted 28 May 2011 - 08:44 AM

Sorry you are having so much trouble. Two things to check. Be sure you are using Python 3, not 2. Also, be very, very careful with tabs in the shell (as it gets very fussy). Maybe try opening an edit window in IDLE, typing the code in there, and then running the code by pressing F5?

--Paul.

#5 tony7340

tony7340

    New Member

  • Members
  • Pip
  • 5 posts

Posted 30 May 2011 - 11:50 AM

I have just typed the code in IDLE window and I still get a syntax error. Can you tell me how the automatic indent is supposed to work in the IDLE. I dont get any auto indent at all,so I indented it manually.

#6 dilbertBear

dilbertBear

    Active Member

  • Members
  • PipPip
  • 13 posts

Posted 30 May 2011 - 09:24 PM

As Paul said, IDLE is really picky if you have mixed tabs and spaces in your code. I had to fight IDLE a lot while I was going through the book.

A menu command you might want to try: If you enter your code in a separate window, there is a Format menu. Under the Format menu is an option to Untabify Region Alt+6. Select all your code, then select this menu option (or press Alt 6). That should get rid of any tabs in your code.

#7 tony7340

tony7340

    New Member

  • Members
  • Pip
  • 5 posts

Posted 31 May 2011 - 02:15 PM

Thanks . This was helpful advice. Generally, when I have prblems it is due to some error in typing code. However, I did the HF C# book and found that it is also important to be aware of errata. I just spent 2 hours on the distribution code in Chapter 2 and finally looked up errata to find out that "sudo" is not needed in Windows environment.

As Paul said, IDLE is really picky if you have mixed tabs and spaces in your code. I had to fight IDLE a lot while I was going through the book.

A menu command you might want to try: If you enter your code in a separate window, there is a Format menu. Under the Format menu is an option to Untabify Region Alt+6. Select all your code, then select this menu option (or press Alt 6). That should get rid of any tabs in your code.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users