Jump to content
Help! Stuck On Chapter 2/ Page 39 :(
4 replies to this topic
Posted 04 March 2012 - 04:35 PM
After learning about Functions in Chapter 1, I have moved on to Chapter 2, where, in Page 39, I am hoping to create a nesster.py file and CALL it in IDLE, rather than copy paste the text.
I seem to have difficulty understanding this step:
"Rather than typing your function’s code into IDLE’s prompt, bring the nester.py file into IDLE’s edit window,
and then press F5 to run the module’s code:"
I used the File-> Open from my IDLE and located my .py file.
This opened my Function in a new IDLE window. From the new Window, when I hit on Run -> Run Module, I see a "Expected and Indented Block" error message..!
Though I am using different names for my .py files and also different variable names inside my function, I am fairly confident that I used the right indentation and have no where else to go.
Request you all to please look into the attachment and suggest what changes to make.
Thank you so much in advance!!
Posted 05 March 2012 - 06:37 AM
The problem you are having has to do with the way Python treats indentation in your code. Python uses whitespace to denote indentation... and this means that SPACE and TAB can be used (but not interchangeable). If you mix SPACE and TAB in your code the Python interpreter can get confused because it counts the whitespace to work out the level of indentation. This means that code that looks OK to you (visually) will break with an IndentError when Python sees it. One TAB character is one level of indentation, whereas four SPACEs (which looks the same as one TAB to me and you) is *four* levels of indentation... remember: Python counts your whitespace.
To fix this, use IDLE to untabify your code. Choose Edit... Select All..., then choose Format... Untabify Region... from the menus. Set the tab width to 4 when prompted.
I've found that it's a good idea to adjust your editor to always replace a press of the TAB key with four SPACEs when working with Python code. This gets rid of this type of thing reoccurring.
Hope this helps.
Posted 05 March 2012 - 01:41 PM
That worked !! Thanks a lot for taking out some time and explaining in detail.
Posted 06 March 2012 - 12:05 PM
Just an extension to my original question:
In Page 41, where the notes says:
I have learnt that I am supposed to SHIFT+Right Click on the Folder that contains my .py files and choose "Open Command Prompt".
This did work in Wndows 7 for me.
Can some one brief on whether there is another better way to do it AND/OR how to do it on a Linux Operating System? (Ubuntu 11.10)
Edited by ravi84, 06 March 2012 - 12:06 PM.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users