94 CHAPTER 5 CONDITIONALS, (Web site optimization) LOOPS, AND SOME

94 CHAPTER 5 CONDITIONALS, LOOPS, AND SOME OTHER STATEMENTS send mail wait one month send mail wait one month send mail wait one month (…and so on) But what if you wanted it to continue doing this until you stopped it? Basically, you want something like this (again, pseudocode): while we aren’t stopped: send mail wait one month Or, let s take a simpler example. Let s say that you want to print out all the numbers from 1 to 100. Again, you could do it the stupid way: print 1 print 2 print 3 . . . and so on. But you didn t start using Python because you wanted to do stupid things, right? while Loops In order to avoid the cumbersome code of the preceding example, it would be useful to be able to do something like this: x = 1 while x <= 100: print x x += 1 Now, how do you do that in Python? You guessed it you do it just like that. Not that complicated is it? You could also use a loop to ensure that the user enters a name, as follows: name = '' while not name: name = raw_input('Please enter your name: ') print 'Hello, %s!' % name Try running this, and then just pressing the Enter key when asked to enter your name: the question appears again because name is still an empty string, which evaluates to false. Tip What would happen if you entered just a space character as your name? Try it. It is accepted because a string with one space character is not empty, and therefore not false. This is definitely a flaw in our little program, but easily corrected: just change while not nameto while not name or name.isspace(), or, perhaps, while not name.strip().
You want to have a cheap webhost for your apache application, then check apache web hosting services.

Comments are closed.