•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 428,200 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 3,212 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: Programming Forums
Views: 430 | Replies: 8
![]() |
•
•
Join Date: Apr 2008
Posts: 26
Reputation:
Rep Power: 1
Solved Threads: 0
There's a function that repeatedly shows up in languages, usually with more or less the following form (Python syntax):
Or something like that. I've also seen it called "switch." Does Python have anything like this?
Python Syntax (Toggle Plain Text)
var1 = raw_input("Input a number between one and three.") choose var1: case "1": print "You inputted one." case "2": print "You inputted two." case "3": print "You inputted three."
Last edited by FreezeBlink : May 9th, 2008 at 7:59 pm. Reason: Added sentance on "switch"
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,476
Reputation:
Rep Power: 10
Solved Threads: 176
In Python you can set up a dictionary to mimic switch/case ...
Even in C the switch/case statement is not as efficient as multiple if/elif statements, so Python did not implement it.
python Syntax (Toggle Plain Text)
def choose(s): return { '1' : lambda:"You inputted one.", '2' : lambda:"You inputted two.", '3' : lambda:"You inputted three." }[s]() var1 = raw_input("Input a number between one and three.") print choose(var1)
Last edited by vegaseat : May 9th, 2008 at 9:37 pm.
May 'the Google' be with you!
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,476
Reputation:
Rep Power: 10
Solved Threads: 176
Actually, in this case you don't need lambda, since you are only returning a string. Please shorten the code to ...
Mildy more elegant ...
python Syntax (Toggle Plain Text)
def choose(s): return { '1' : "You inputted one.", '2' : "You inputted two.", '3' : "You inputted three." }[s] var1 = raw_input("Input a number between one and three.") print choose(var1)
python Syntax (Toggle Plain Text)
def choose(s): return "You entered " + { '1' : "one", '2' : "two", '3' : "three" }[s] var1 = raw_input("Input a number between one and three: ") print choose(var1)
Last edited by vegaseat : May 10th, 2008 at 12:03 am.
May 'the Google' be with you!
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,476
Reputation:
Rep Power: 10
Solved Threads: 176
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
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!
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!
The standard try/except way would be:
Or straight foreward with dictionary method get():
This is closer to the switch/case default.
python Syntax (Toggle Plain Text)
def choose(s): try: return "You entered " + { '1' : "one", '2' : "two", '3' : "three" }[s] except: return "Bad input" var1 = raw_input("Input a number between one and three: ") print choose(var1)
python Syntax (Toggle Plain Text)
def choose2(s): return "You entered " + { '1' : "one", '2' : "two", '3' : "three" }.get(s, "a bad number") var1 = raw_input("Input a number between one and three: ") print choose2(var1)
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Firefox Compatibility help with script (JavaScript / DHTML / AJAX)
- def save(): print "HOW DO I DO THIS?" (Python)
Other Threads in the Python Forum
- Previous Thread: Pymmlib for Python-BioPython
- Next Thread: urllib problems



Linear Mode