aneroid

http://stackoverflow.com/q/38299137/1431750

Jul 11th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. # for http://stackoverflow.com/q/38299137/1431750
  2.  
  3. user_input = raw_input('Please provide your name:')
  4. while user_input != "Q" and user_input != "q":
  5.     if user_input == "":
  6.         print "Your input is required"
  7.         user_input = raw_input('Please provide your name:')
  8.     else:
  9.         print "Youre name has been recorded:", user_input
  10.         break
  11.  
  12. In action:
  13.  
  14. >>> user_input = raw_input('Please provide your name:')
  15. Please provide your name:
  16. >>> while user_input != "Q" and user_input != "q":
  17. ...     if user_input == "":
  18. ...         print "Your input is required"
  19. ...         user_input = raw_input('Please provide your name:')
  20. ...     else:
  21. ...         print "Youre name has been recorded:", user_input
  22. ...         break
  23. ...
  24. Your input is required
  25. Please provide your name:
  26. Your input is required
  27. Please provide your name:
  28. Your input is required
  29. Please provide your name:A
  30. Youre name has been recorded: A
  31. >>> # it has exit the program
  32. ...
  33. >>> user_input = '' # reset `user_input` and run the while loop again
  34. >>> while user_input != "Q" and user_input != "q":
  35. ...     if user_input == "":
  36. ...         print "Your input is required"
  37. ...         user_input = raw_input('Please provide your name:')
  38. ...     else:
  39. ...         print "Youre name has been recorded:", user_input
  40. ...         break
  41. ...
  42. Your input is required
  43. Please provide your name:
  44. Your input is required
  45. Please provide your name:q
  46. >>>
  47.  
  48. # Ideally, what you could do instead is:
  49.  
  50. while True:
  51.     user_input = raw_input('Please provide your name:')
  52.     if user_input == "Q" or user_input == "q":
  53.         break
  54.     elif user_input == "":
  55.         print "Your input is required"
  56.         continue  # unnecessary but clearer since there's no other code which will run for this block
  57.     else:
  58.         print "Youre name has been recorded:", user_input
  59.         break
  60.  
  61. # And the shortest way to do it with clarity:
  62.  
  63. while True:
  64.     user_input = raw_input('Please provide your name:')
  65.     if user_input in ("Q", "q"):
  66.         break
  67.     if user_input == "":
  68.         print "Your input is required"
  69.         continue  # necessary in this case
  70.     print "Youre name has been recorded:", user_input
  71.     break
  72.  
  73. Please provide your name:
  74. Your input is required
  75. Please provide your name:
  76. Your input is required
  77. Please provide your name:q
  78. >>>
  79. >>> # run the loop again to test valid input
  80. ...
  81. Please provide your name:
  82. Your input is required
  83. Please provide your name:A
  84. Youre name has been recorded: A
  85. >>>
Add Comment
Please, Sign In to add comment