DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   "choose" in Python? (http://www.daniweb.com/forums/thread123458.html)

FreezeBlink May 9th, 2008 7:47 pm
"choose" in Python?
 
There's a function that repeatedly shows up in languages, usually with more or less the following form (Python syntax):
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."
Or something like that. I've also seen it called "switch." Does Python have anything like this?

vegaseat May 9th, 2008 9:37 pm
Re: "choose" in Python?
 
In Python you can set up a dictionary to mimic switch/case ...
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)
Even in C the switch/case statement is not as efficient as multiple if/elif statements, so Python did not implement it.

1337455 10534 May 9th, 2008 10:35 pm
Re: "choose" in Python?
 
That is pretty ingenious!
Does it work without using lambda? e.g
'1':"w/e", etc

vegaseat May 9th, 2008 11:51 pm
Re: "choose" in Python?
 
Actually, in this case you don't need lambda, since you are only returning a string. Please shorten the code to ...
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)
Mildy more elegant ...
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)

FreezeBlink May 10th, 2008 1:04 pm
Re: "choose" in Python?
 
And, um, while we're at it...what does Lambda do, anyway?

vegaseat May 11th, 2008 2:01 am
Re: "choose" in Python?
 
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

kdoiron May 12th, 2008 10:48 am
Re: "choose" in Python?
 
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?

ZZucker May 12th, 2008 11:20 am
Re: "choose" in Python?
 
The standard try/except way would be:
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)
Or straight foreward with dictionary method get():
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)
This is closer to the switch/case default.

kdoiron May 12th, 2008 1:20 pm
Re: "choose" in Python?
 
Thanks, I knew it was something simple that I had seen before.

* SLAPS FOREHEAD *


All times are GMT -4. The time now is 3:16 am.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC