SunQuest
 
       Python
  Home arrow Python arrow Page 8 - Python 101 (part 2): If Wishes Were Py...
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 
Actuate Whitepapers 
VeriSign Whitepapers 
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 101 (part 2): If Wishes Were Pythons
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2001-05-31

    Table of Contents:
  • Python 101 (part 2): If Wishes Were Pythons
  • Tax Evasion
  • Q
  • Sliced And Diced
  • Comparing Apples And Oranges
  • If Only...
  • Tying Up The Loose Ends
  • Cookie-Cutter Code
  • Time For Lunch

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Python 101 (part 2): If Wishes Were Pythons - Cookie-Cutter Code


    (Page 8 of 9 )

    The "if-else" construct certainly offers a smidgen more flexibility than the basic "if" construct, but still limits you to only two possible courses of action. If your Python script needs to be capable of handling more than two possibilities, you should reach for the "if-elif-else" construct, which is a happy combination of the two constructs you've just been reading about.

    if first condition is true: do this! elif second condition is true: do this! elif third condition is true: do this! ... and so on ... else: do this!
    There's one important point to be noted here - as soon as one of the "if" statements within the block is found to be true, Python will execute the corresponding code, skip the remaining "if" statements in the block, and jump immediately to the lines following the entire "if-elif-else" block.

    Take a look at it in action:

    #!/usr/bin/python print "Welcome to the Amazing Fortune Cookie Generator"; # get input day = raw_input("Pick a day, any day: "); # check day and assign fortune if day == "Monday": fortune = "Never make anything simple and efficient when a way can be found to make it complex and wonderful." elif day == "Tuesday": fortune = "Life is a game of bridge -- and you've just been finessed." elif day == "Wednesday": fortune = "What sane person could live in this world and not be crazy?" elif day == "Thursday": fortune = "Don't get mad, get interest." elif day == "Friday": fortune = "Just go with the flow control, roll with the crunches, and, when you get a prompt, type like hell." else: fortune = "Sorry, closed on the weekend" # output print fortune
    You'll notice that in this case, I've used the raw_input() function instead of the regular input() function. The difference between the two is minor: raw_input() stores user input as is, while input() attempts to convert user input into a Python expression.

    Since Python does not support PHP- or JSP-style "switch/case" conditional statements, the "if-elif-else" construct is the only way to route program control to multiple code blocks.

    More Python Articles
    More By Vikram Vaswani, (c) Melonfire


     

       

    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 4 hosted by Hostway