: Thanks very much for the reply, I'm trying to get to grips with what you've said. I'm sure it will come to me... I've ordered a book also on learning Python which I can use along with my growing collection of notes...
: Theres pieces in the code I've not came across yet, such as
: also where it says while 1:
: what does this do/mean? I've came across pieces of code such as
: while number<0:
: while number !=3 etc.... so just trying to understand what while 1: does
My favorite Python book so far is "Practical Python" by Magnus Lie Hetland.
"try" is part of a special kind of block. What it does is tell python "try to run the following lines of code". In the example I sent you, the "try" block was followed by an "except" block. Exceptions are like errors. If something "wrong" occurs, an exception is raised by Python. If you don't catch that exception then the program will terminate. So in the example, "except ValueError" means, "if a ValueError occurred in the previous try block, execute the following lines".
A "while" loop always tests some expression for a True or False result. It will continue looping until the expression equals False. In the two examples you provide, "while number < 0" and "while number != 3", you have the keyword "while" followed by an expression. Each time through the loop, Python evaluates "number < 0" or "number != 3" to see if it is still True. If True, the loop runs again, otherwise the loop stops. The number zero is the same as False. Any other number is True. So, "while 1" means "loop forever" because 1 is True forever. Empty strings (like s = '') are False. All other strings are True. Empty lists, tuples, and dicts are False. Lists, tuples, and dicts that contain any number of items are true.
Here's a typical example:
for line in file('foo.txt'): # line 1
if line.strip(): # line 2
pass # line 3
The first line means "execute the following code once for each line in the file named 'foo.txt'. The second line means "if, after stripping all leading and trailing whitespace off, the line has any characters in it then perform the following code. The third line just contains the "pass" keyword which is Python's "do nothing" statement. I've included it here simply for example (because every block must have at least one statement in it). The point I'm trying to make is that the "if" statement is evaluating a string (the result of calling line.strip()) to determine if line 3 should execute.
I hope this made sense.
$ select * from users where clue > 0
no rows returned