User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 375,228 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,279 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:

"choose" in Python?

Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,337
Reputation: vegaseat is on a distinguished road 
Rep Power: 8
Solved Threads: 171
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: "choose" in Python?

  #2  
May 9th, 2008
In Python you can set up a dictionary to mimic switch/case ...
  1. def choose(s):
  2. return {
  3. '1' : lambda:"You inputted one.",
  4. '2' : lambda:"You inputted two.",
  5. '3' : lambda:"You inputted three."
  6. }[s]()
  7.  
  8. var1 = raw_input("Input a number between one and three.")
  9. print choose(var1)
Even in C the switch/case statement is not as efficient as multiple if/elif statements, so Python did not implement it.
Last edited by vegaseat : May 9th, 2008 at 9:37 pm.
May 'the Google' be with you!
Reply With Quote  
All times are GMT -4. The time now is 3:56 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC