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 391,596 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,699 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:
Views: 354 | Replies: 8
Reply
Join Date: Apr 2008
Posts: 26
Reputation: FreezeBlink is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
FreezeBlink FreezeBlink is offline Offline
Light Poster

"choose" in Python?

  #1  
May 9th, 2008
There's a function that repeatedly shows up in languages, usually with more or less the following form (Python syntax):
  1. var1 = raw_input("Input a number between one and three.")
  2.  
  3. choose var1:
  4. case "1":
  5. print "You inputted one."
  6. case "2":
  7. print "You inputted two."
  8. case "3":
  9. print "You inputted three."
Or something like that. I've also seen it called "switch." Does Python have anything like this?
Last edited by FreezeBlink : May 9th, 2008 at 7:59 pm. Reason: Added sentance on "switch"
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,394
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 172
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  
Join Date: Oct 2007
Posts: 41
Reputation: 1337455 10534 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
1337455 10534 1337455 10534 is offline Offline
Light Poster

Re: "choose" in Python?

  #3  
May 9th, 2008
That is pretty ingenious!
Does it work without using lambda? e.g
'1':"w/e", etc
"And da wind cry moron." ~ Pearls Before Swine
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,394
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 172
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: "choose" in Python?

  #4  
May 9th, 2008
Actually, in this case you don't need lambda, since you are only returning a string. Please shorten the code to ...
  1. def choose(s):
  2. return {
  3. '1' : "You inputted one.",
  4. '2' : "You inputted two.",
  5. '3' : "You inputted three."
  6. }[s]
  7.  
  8. var1 = raw_input("Input a number between one and three.")
  9. print choose(var1)
Mildy more elegant ...
  1. def choose(s):
  2. return "You entered " + {
  3. '1' : "one",
  4. '2' : "two",
  5. '3' : "three"
  6. }[s]
  7.  
  8. var1 = raw_input("Input a number between one and three: ")
  9. print choose(var1)
Last edited by vegaseat : May 10th, 2008 at 12:03 am.
May 'the Google' be with you!
Reply With Quote  
Join Date: Apr 2008
Posts: 26
Reputation: FreezeBlink is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
FreezeBlink FreezeBlink is offline Offline
Light Poster

Re: "choose" in Python?

  #5  
May 10th, 2008
And, um, while we're at it...what does Lambda do, anyway?
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,394
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 172
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: "choose" in Python?

  #6  
May 11th, 2008
Lambda creates an anonymous function, and is used to create a short inline function.

Lambda calculus is part of functional programming. It simply states that all computational constructs can be defined in terms of lambda. Interested? Read the article in:
http://linuxgazette.net/109/pramode.html
May 'the Google' be with you!
Reply With Quote  
Join Date: May 2008
Location: Toronto
Posts: 37
Reputation: kdoiron is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kdoiron's Avatar
kdoiron kdoiron is offline Offline
Light Poster

Re: "choose" in Python?

  #7  
May 12th, 2008
I'm going to feel really stupid when I see the answer to my question, because I'm sure I've seen it before and I've just forgotten. Anyway, I was just fiddling with this example, and I can't remember how to add a default case. In the example given I want to return the text "Bad input" for any value other than 1, 2, or 3. What do I need to add to the dictionary?
But I don't like SPAM!
Reply With Quote  
Join Date: Jan 2008
Posts: 499
Reputation: ZZucker is on a distinguished road 
Rep Power: 1
Solved Threads: 13
ZZucker's Avatar
ZZucker ZZucker is offline Offline
Posting Pro in Training

Re: "choose" in Python?

  #8  
May 12th, 2008
The standard try/except way would be:
  1. def choose(s):
  2. try:
  3. return "You entered " + {
  4. '1' : "one",
  5. '2' : "two",
  6. '3' : "three"
  7. }[s]
  8. except:
  9. return "Bad input"
  10.  
  11. var1 = raw_input("Input a number between one and three: ")
  12. print choose(var1)
Or straight foreward with dictionary method get():
  1. def choose2(s):
  2. return "You entered " + {
  3. '1' : "one",
  4. '2' : "two",
  5. '3' : "three"
  6. }.get(s, "a bad number")
  7.  
  8. var1 = raw_input("Input a number between one and three: ")
  9. print choose2(var1)
This is closer to the switch/case default.
Last edited by ZZucker : May 12th, 2008 at 11:24 am.
Never argue with idiots, they'll just bring you down to their level and beat you with their experience.
Reply With Quote  
Join Date: May 2008
Location: Toronto
Posts: 37
Reputation: kdoiron is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kdoiron's Avatar
kdoiron kdoiron is offline Offline
Light Poster

Re: "choose" in Python?

  #9  
May 12th, 2008
Thanks, I knew it was something simple that I had seen before.

* SLAPS FOREHEAD *
But I don't like SPAM!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Python Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 11:39 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC