Advertisement
e_mccormick

Python switch/case Replacement

Jun 30th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. #Slightly advanced version of this.
  2. # It works if the person enters Y, YES, Yes, yES, and so on for
  3. # Yes, No, True, and False.
  4. # The key to this is on two parts.
  5. #    1) There is the dictionary names switch.  This is so named
  6. #       because I am using it like it is a switch statement in
  7. #       other languages.
  8. #    2) The switch.get(user_input.lower()) part is the other key.
  9. #       It takes what the user put in and converts it to lower
  10. #       case, then tests it against the dictionary.  If there is
  11. #       a match, it returns True or False as a boolian.  Otherwise
  12. #       it returns the special type of None and the while loop
  13. #       continues.
  14.  
  15. ## Test run results:
  16. ##
  17. ##>>>
  18. ##Is Lelis Online ? Answer True or False : g
  19. ##Just answer 'True' or 'False'
  20. ##
  21. ##Is Lelis Online ? Answer True or False : t
  22. ##Hi! Desculpa o atraso; ainda podemos fazer algo?
  23. ##>>> ===================================== RESTART =====================================
  24. ##>>>
  25. ##Is Lelis Online ? Answer True or False : FaLsE
  26. ##Sorry! I totally forget you. Muito contente por encontrar um crioulo. Keep in touch.
  27. ##>>>
  28.  
  29. user_input=None
  30. switch = {'true':True,'t':True,'yes':True,'y':True,'false':False,'f':False,'no':False,'n':False}
  31. while user_input==None:
  32.     user_input=raw_input("Is Lelis Online ? Answer True or False : ")
  33.     user_input=switch.get(user_input.lower())
  34.     if user_input==None:
  35.         print "Just answer 'True' or 'False'\n"
  36.  
  37. if user_input == True:
  38.     print "Hi! Desculpa o atraso; ainda podemos fazer algo?"
  39. else:
  40.     print "Sorry! I totally forget you. Muito contente por encontrar um crioulo. Keep in touch."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement