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 374,613 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,378 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,335
Reputation: vegaseat is on a distinguished road 
Rep Power: 8
Solved Threads: 170
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  
All times are GMT -4. The time now is 8:46 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC