Home arrow Python arrow Page 8 - Python 101 (part 2): If Wishes Were Pythons

Cookie-Cutter Code - Python

Begin your tour of Python with a look at its number and stringtypes, together with examples of how they can be used in simple Pythonprograms. Along the way, you'll also learn how to build conditionalexpressions, slice and dice strings, and accept user input from the commandline

TABLE OF CONTENTS:
  1. Python 101 (part 2): If Wishes Were Pythons
  2. Tax Evasion
  3. Q
  4. Sliced And Diced
  5. Comparing Apples And Oranges
  6. If Only...
  7. Tying Up The Loose Ends
  8. Cookie-Cutter Code
  9. Time For Lunch
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 3
May 31, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- 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

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: