RunestoneServer icon indicating copy to clipboard operation
RunestoneServer copied to clipboard

example "ActiveCode: 7 (intro_8) can't be run interactively

Open dlanznar opened this issue 14 years ago • 3 comments

the example "ActiveCode: 7 (intro_8)" on http://interactivepython.org/courselib/static/pythonds/Introduction/introduction.html#review-of-basic-python

cannot be run interactively at the python prompt, unlike other examples in this section. It is not made clear why that is so.

dlanznar avatar Nov 11 '12 05:11 dlanznar

Can you be more specific about the error you are getting? I tried to run intro_8 in the interactive shell and it worked fine for me.

Brad Miller http://reputablejournal.com

On Nov 10, 2012, at 11:32 PM, dlanznar [email protected] wrote:

the example "ActiveCode: 7 (intro_8)" on http://interactivepython.org/courselib/static/pythonds/Introduction/introduction.html#review-of-basic-python

cannot be run interactively at the python prompt, unlike other examples in this section. It is not made clear why that is so.

— Reply to this email directly or view it on GitHub.

bnmnetp avatar Nov 11 '12 23:11 bnmnetp

OK. Sorry - I should know better than to be vague and not give detailed information when reporting a problem! (it was late...)

First off, I should make clear that I was running the code examples as given at the Python prompt (>>>), and tried this one on both version 2 and 3, on Mac and also on Ubuntu. The issue turned out to be the way the interpreter works - not really a problem, but maybe it could be clarified.

The code is given as:

wordlist = ['cat','dog','rabbit'] letterlist = [ ] for aword in wordlist: for aletter in aword: letterlist.append(aletter) print(letterlist)

I typed it in at the Python prompt following each line with as usual. After each of the first two lines, I get a new prompt - those are complete statements.

Then typing the nested "for" statements, I don't get the regular prompt (>>>) after each line, but the continuation prompt (...). After the 5th line (letterlist.append...) I get the continuation prompt again, but to go ahead and type the last statement (print) at that point -- taking care to not indent -- gives the syntax error, since it's a new statement, I guess, and the interpreter wants to complete the processing of the single nested-for statement.

The solution is just to type a blank line () after the 5th line, which finishes the statement and returns the normal prompt, and then the print statement works as expected. Someone used to using the interpreter interactively probably does it correctly instinctively.

The description of the code does refer to the text box as a "code fragment" and it doesn't show the prompts, suggesting that it isn't to be run as is at the command line, but all the other nearby examples are, so if you're doing examples as you work through the section, using your own Python interpreter instead of (or in addition to) running the built-in examples, you are likely to run into this problem. You might show the prompts, as is done in some other examples, to clarify what's going on.

While I am writing I should also mention one very minor typo I noticed: in the section on Control Structures (still on the Introduction/review-of-basic-python page) in the second code box after ActiveCode 7, I believe there is a colon missing from one of the "else" keywords.

Anyway, otherwise I'm getting a lot out of the course - thanks for making it available. Python coding aside, the "monkeys" problem, particularly the contrast between its behavior with and without "selection," is very illuminating! I'd never thought to try that out before.

Thanks

David Lanznar [email protected]

On Nov 11, 2012, at 6:55 PM, Brad Miller wrote:

Can you be more specific about the error you are getting? I tried to run intro_8 in the interactive shell and it worked fine for me.

Brad Miller http://reputablejournal.com

On Nov 10, 2012, at 11:32 PM, dlanznar [email protected] wrote:

the example "ActiveCode: 7 (intro_8)" on http://interactivepython.org/courselib/static/pythonds/Introduction/introduction.html#review-of-basic-python

cannot be run interactively at the python prompt, unlike other examples in this section. It is not made clear why that is so.

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHub.

dlanznar avatar Nov 12 '12 16:11 dlanznar

Brad Miller http://reputablejournal.com

On Nov 12, 2012, at 10:10 AM, dlanznar [email protected] wrote:

OK. Sorry - I should know better than to be vague and not give detailed information when reporting a problem! (it was late...)

First off, I should make clear that I was running the code examples as given at the Python prompt (>>>), and tried this one on both version 2 and 3, on Mac and also on Ubuntu. The issue turned out to be the way the interpreter works - not really a problem, but maybe it could be clarified.

The code is given as:

wordlist = ['cat','dog','rabbit'] letterlist = [ ] for aword in wordlist: for aletter in aword: letterlist.append(aletter) print(letterlist)

I typed it in at the Python prompt following each line with as usual. After each of the first two lines, I get a new prompt - those are complete statements.

Then typing the nested "for" statements, I don't get the regular prompt (>>>) after each line, but the continuation prompt (...). After the 5th line (letterlist.append...) I get the continuation prompt again, but to go ahead and type the last statement (print) at that point -- taking care to not indent -- gives the syntax error, since it's a new statement, I guess, and the interpreter wants to complete the processing of the single nested-for statement.

The solution is just to type a blank line () after the 5th line, which finishes the statement and returns the normal prompt, and then the print statement works as expected. Someone used to using the interpreter interactively probably does it correctly instinctively.

Yep, but this book isn't written for those people.

Thanks for the feedback. I'll figure out how to word that better for people who are following along in the interactive interpreter.

The description of the code does refer to the text box as a "code fragment" and it doesn't show the prompts, suggesting that it isn't to be run as is at the command line, but all the other nearby examples are, so if you're doing examples as you work through the section, using your own Python interpreter instead of (or in addition to) running the built-in examples, you are likely to run into this problem. You might show the prompts, as is done in some other examples, to clarify what's going on.

While I am writing I should also mention one very minor typo I noticed: in the section on Control Structures (still on the Introduction/review-of-basic-python page) in the second code box after ActiveCode 7, I believe there is a colon missing from one of the "else" keywords.

Anyway, otherwise I'm getting a lot out of the course - thanks for making it available. Python coding aside, the "monkeys" problem, particularly the contrast between its behavior with and without "selection," is very illuminating! I'd never thought to try that out before.

Thanks, I love to use the monkeys problem in class. I always do it the dumb way, and then ask the students to write down how long they think it will take. So I start the program running on a department server, and have it print out the best solution every 100k iterations. After a day goes by I tell them that I'll buy pizza for the whole class if it finishes before the end of the semester. Then we spend a little time figuring out what the chances are of it generating the sentence before the end of the semester and they all figure out what a cheapskate I really am.

Thanks

David Lanznar [email protected]

On Nov 11, 2012, at 6:55 PM, Brad Miller wrote:

Can you be more specific about the error you are getting? I tried to run intro_8 in the interactive shell and it worked fine for me.

Brad Miller http://reputablejournal.com

On Nov 10, 2012, at 11:32 PM, dlanznar [email protected] wrote:

the example "ActiveCode: 7 (intro_8)" on http://interactivepython.org/courselib/static/pythonds/Introduction/introduction.html#review-of-basic-python

cannot be run interactively at the python prompt, unlike other examples in this section. It is not made clear why that is so.

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHub.

bnmnetp avatar Nov 14 '12 23:11 bnmnetp