![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2007
Posts: 14
Rep Power: 0
![]() |
Question Number 1
Hi All,
I am new. I have posted in the introduction forums. You can read my post there to get to know why I am here. As it says, I am a newbie. Teaching myself (with your help) Python. This is my first question, very basic but I'm starting right from the beginning. I am reading through a beginners book that was linked to another thread in this forum. It got me to create the following script def newLine():
print
def threeLines():
newLine()
newLine()
newLine()
def nineLines():
threeLines()
threeLines()
threeLines()
def twentysevenLines():
nineLines()
nineLines()
nineLines()
print "First Line."
twentysevenLines()
print "Second Line."No dramas at all. It just prints the first line, then 27 blank lines and then the second line. As an excercise the book gets me to: "move the last three lines of this program to the top, so the function calls appear before the definitions. Run the program and see what error message you get." This should be like this right? print "First Line."
twentysevenLines()
print "Second Line."
def newLine():
print
def threeLines():
newLine()
newLine()
newLine()
def nineLines():
threeLines()
threeLines()
threeLines()
def twentysevenLines():
nineLines()
nineLines()
nineLines()The only problem is I dont get any errors. It prints the first line, then 27 blank lines and then the second line just like the original script. What am I missing here? Should there be an error? |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Re: Question Number 1
That works because it's making a pass through the code to pick up the definitions and then a pass to execute. Not all languages or assemblers or compilers or interpreters give you that facility. It's fairly common for interpreters, though. They have to emit the actual runnable code, so they often generate all of it, then run it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 | |
|
Newbie
Join Date: Nov 2007
Posts: 14
Rep Power: 0
![]() |
Re: Question Number 1
Quote:
Sorry to waste your time with such simple questions, but I want to be thorough and learn this properly. By the way, the book doesn't tell me how to use the program. I had to figure that out myself. Am I doing this right? I create a new script for the above example, then go into the interactive window that is there when Python first loads up and I imported the script. |
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Re: Question Number 1
Suppose I read your second script to you one line at a time. When I said, "Print 27 lines" you wouldn't know how to do it, because you don't know how to print even one line.
On the other hand, if I let you read the script in advance, you know how to print one line, three lines, nine lines, and twenty-seven lines. It's that simple. If your interpreter sees and understands the entire script before it's asked to execute any part of it, you're home free. If it doesn't, you aren't. It isn't magic; it's implementation. You might want to read some basic "How it works" stuff for computers and computer languages and how they get the job done in a few but various ways.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 | |
|
Newbie
Join Date: Nov 2007
Posts: 14
Rep Power: 0
![]() |
Re: Question Number 1
Quote:
Google might help me. Thanks again. |
|
|
|
|
|
|
#6 |
|
Expert Programmer
|
Re: Question Number 1
The phrase "run the program and see what error message you get" does not necessarily imply that an error message will occur. You are probably not missing anything; the author simply wanted you to realize that you would get an error in most other languages, but not Python.
Another possibility, though unlikely, is that the book you are reading was written a very long time ago and the author was using an older version of Python that behaved differently. The Python language is constantly evolving. |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,826
Rep Power: 5
![]() |
Re: Question Number 1
I assume you're using IDLE? I do get the error with IDLE in Python 2.5.
I'm thinking your interpreter cached the functions when you were writing and running it. Then, when you moved the bottom three lines to the top, it had already known where the functions are because they were cached by the precompiler. Try saving the finalized script as a new file, then run that. You should see the error. If not, you simply have a different implementation of Python, or one that behaves differently on a different machine. For all we know, mine and the author's Python could be the ones behaving "incorrectly". Nevertheless... don't worry about it. The main point of that section in the tutorial was to reinforce the notion:
When the interpreter already does that for you, it merely becomes a coding convention. |
|
|
|
|
|
#8 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Re: Question Number 1
I think the key is in the OP's second post:
Quote:
UnboundLocalError: local variable 'twentysevenLines' referenced before assignment modules = {}
def main ():
print "First Line."
twentysevenLines()
print "Second Line."
def newLine():
print
def threeLines():
newLine()
newLine()
newLine()
def nineLines():
threeLines()
threeLines()
threeLines()
def twentysevenLines():
nineLines()
nineLines()
nineLines()
if __name__ == '__main__':
main()from precmod import *
modules ={'precmod': [0, '', 'precmod.py']}
def main ():
print "First Line."
twentysevenLines()
print "Second Line."
if __name__ == '__main__':
main()def newLine():
print
def threeLines():
newLine()
newLine()
newLine()
def nineLines():
threeLines()
threeLines()
threeLines()
def twentysevenLines():
nineLines()
nineLines()
nineLines()
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#9 |
|
Newbie
Join Date: Nov 2007
Posts: 14
Rep Power: 0
![]() |
Re: Question Number 1
I saved
print "First Line."
twentysevenLines()
print "Second Line."
def newLine():
print
def threeLines():
newLine()
newLine()
newLine()
def nineLines():
threeLines()
threeLines()
threeLines()
def twentysevenLines():
nineLines()
nineLines()
nineLines()as a new script, and then this time I clicked run instead of import. It still worked. Although I do understand the order in the way things work, I don't understand the terminology you are using and I don't understand what goes on in the background to make it work. My understanding in plain English: 1. Show it how to do something 2. Tell it to do it I'm having no problems with this. I guess in a way CSS is similar. You write all the rules in the CSS document, link to it in your header and then call on those rules in your page. When I looked at this exercise, the first thing I thought (once again in plain english) was "Ok, im telling it what to do before it knows how to do it, I wonder what the error will be". I did the exercise and there was no error. My problem is I don't know why there was no error. I need to take the previous advice and learn about how things work. I tried Googling a few terms but haven't had much success. It's crazy. Here I am having problems with not having an error. It should be the opposite. |
|
|
|
|
|
#10 |
|
Expert Programmer
|
Re: Question Number 1
There is no reason to spend so much time obsessing about not getting an error message. You are making a mountain out of a molehill. Move on already.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| c-unix-childprocesses-random number | programmingnoob | C | 7 | Feb 6th, 2007 8:39 PM |
| very simple question. make button change number in edit box | nickm | Delphi | 2 | Apr 29th, 2006 10:47 PM |
| One more question infinite! | massive-war | C++ | 8 | Apr 11th, 2005 12:11 AM |
| non repeating random number generation | gencor45 | C# | 2 | Feb 9th, 2005 12:11 AM |
| Number Systems... | Ade | Coder's Corner Lounge | 7 | Jan 19th, 2005 5:32 AM |