Python
  Home arrow Python arrow Page 8 - Python 101 (part 2): If Wishes Were Pythons
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PYTHON

Python 101 (part 2): If Wishes Were Pythons
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 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

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek