Python
  Home arrow Python arrow Page 3 - Python: Input and Variables
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Python: Input and Variables
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2007-11-19

    Table of Contents:
  • Python: Input and Variables
  • Beating...I Mean Asking the User for Information
  • Loops
  • For Loop

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Python: Input and Variables - Loops


    (Page 3 of 4 )

    Computers are good at doing repetitive tasks; they don't need to take a lunch break or go to the potty, and unless you program them to, they won't spend seven hours of their eight hour work day ambling aimlessly around the Internet. The way we get computers to do these repetitive tasks is through iteration, or loops.

    The While Loop

    The While Loop basically works like this: Do this, while that is true. Like when my girlfriend nags me. My brain says: Create ulcers, while she complains about me not taking out the garbage. When she stops, so does the ulcer creation. Here it is in code:


    #!/usr/local/bin/python


    my_count = 0

    while my_count < 100:

        my_count = my_count + 1

    print my_count

    This code creates a variable called my_count and stores the value 0 in it. Next it creates a loop that says while the value of my_count is less than 100, print the value of count. It then goes through the loop over and over again, adding 1 to the value of my_count until it reaches 100. The result of this program would be:


      1

      2

      3

      4

      5

      etc

      etc

      etc

      100

    A brief note: When working with Python, it is mandatory to indent by four spaces each level. So if you are doing a loop, there must be four spaces. If you add an if to that loop, it must be eight spaces etc. Here is a pseudocode example:


    #!/usr/local/bin/python


    some code

    while variable == variable

        morecode

        while blah blah

        more code

        print something

    print whatever happens after the while is escaped

    We can also use the loop to force a user to enter some data, as in a password program:


    #!/usr/local/bin/python


    user_password =”something”


    while password !=”something”:

        password = raw_input(“Enter your password to continue nerd: “)

        print “Welcome Captain my Captain! I didn't notice you. Did you gain weight?”

    Notice that I indented the line that contained: password = raw_input. This is because it is the next level under the While statement. Then I did not indent the print statement. That is because the print statement occurs OUTSIDE of the loop and is only triggered when the loop is exited. If the user tried to use this program and did not enter the right password several times, here is what it would look like:

      Enter your password to continue nerd: apples

      Enter your password to continue nerd: oranges

      Enter your password to continue nerd: biscuits

      Enter your password to continue nerd: something

      Welcome Captain my Captain! I didn't notice you. Did you gain weight?

    More Python Articles
    More By James Payne


       · Hey thanks for dropping by and reading my article on Python. Its a pretty cool...
       · In your demonstration of a while loop, you wrote the following:"Next it creates...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway