Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. #note: Python 2.7 users should use `raw_input`, the equivalent of 3.X's `input`
  2. age = int(input("Please enter your age: "))
  3. if age >= 18:
  4. print("You are able to vote in the United States!")
  5. else:
  6. print("You are not able to vote in the United States.")
  7.  
  8. while True:
  9. try:
  10. # Note: Python 2.x users should use raw_input, the equivalent of 3.x's input
  11. age = int(input("Please enter your age: "))
  12. except ValueError:
  13. print("Sorry, I didn't understand that.")
  14. #better try again... Return to the start of the loop
  15. continue
  16. else:
  17. #age was successfully parsed!
  18. #we're ready to exit the loop.
  19. break
  20. if age >= 18:
  21. print("You are able to vote in the United States!")
  22. else:
  23. print("You are not able to vote in the United States.")
  24.  
  25. while True:
  26. data = input("Please enter a loud message (must be all caps): ")
  27. if not data.isupper():
  28. print("Sorry, your response was not loud enough.")
  29. continue
  30. else:
  31. #we're happy with the value given.
  32. #we're ready to exit the loop.
  33. break
  34.  
  35. while True:
  36. data = input("Pick an answer from A to D:")
  37. if data.lower() not in ('a', 'b', 'c', 'd'):
  38. print("Not an appropriate choice.")
  39. else:
  40. break
  41.  
  42. while True:
  43. try:
  44. age = int(input("Please enter your age: "))
  45. except ValueError:
  46. print("Sorry, I didn't understand that.")
  47. continue
  48.  
  49. if age < 0:
  50. print("Sorry, your response must not be negative.")
  51. continue
  52. else:
  53. #age was successfully parsed, and we're happy with its value.
  54. #we're ready to exit the loop.
  55. break
  56. if age >= 18:
  57. print("You are able to vote in the United States!")
  58. else:
  59. print("You are not able to vote in the United States.")
  60.  
  61. def get_non_negative_int(prompt):
  62. while True:
  63. try:
  64. value = int(input(prompt))
  65. except ValueError:
  66. print("Sorry, I didn't understand that.")
  67. continue
  68.  
  69. if value < 0:
  70. print("Sorry, your response must not be negative.")
  71. continue
  72. else:
  73. break
  74. return value
  75.  
  76. age = get_non_negative_int("Please enter your age: ")
  77. kids = get_non_negative_int("Please enter the number of children you have: ")
  78. salary = get_non_negative_int("Please enter your yearly earnings, in dollars: ")
  79.  
  80. def sanitised_input(prompt, type_=None, min_=None, max_=None, range_=None):
  81. if min_ is not None and max_ is not None and max_ < min_:
  82. raise ValueError("min_ must be less than or equal to max_.")
  83. while True:
  84. ui = input(prompt)
  85. if type_ is not None:
  86. try:
  87. ui = type_(ui)
  88. except ValueError:
  89. print("Input type must be {0}.".format(type_.__name__))
  90. continue
  91. if max_ is not None and ui > max_:
  92. print("Input must be less than or equal to {0}.".format(max_))
  93. elif min_ is not None and ui < min_:
  94. print("Input must be greater than or equal to {0}.".format(min_))
  95. elif range_ is not None and ui not in range_:
  96. if isinstance(range_, range):
  97. template = "Input must be between {0.start} and {0.stop}."
  98. print(template.format(range_))
  99. else:
  100. template = "Input must be {0}."
  101. if len(range_) == 1:
  102. print(template.format(*range_))
  103. else:
  104. print(template.format(" or ".join((", ".join(map(str,
  105. range_[:-1])),
  106. str(range_[-1])))))
  107. else:
  108. return ui
  109.  
  110. age = sanitised_input("Enter your age: ", int, 1, 101)
  111. answer = sanitised_input("Enter your answer", str.lower, range_=('a', 'b', 'c', 'd'))
  112.  
  113. data = input("Please enter a loud message (must be all caps): ")
  114. while not data.isupper():
  115. print("Sorry, your response was not loud enough.")
  116. data = input("Please enter a loud message (must be all caps): ")
  117.  
  118. def get_non_negative_int(prompt):
  119. try:
  120. value = int(input(prompt))
  121. except ValueError:
  122. print("Sorry, I didn't understand that.")
  123. return get_non_negative_int(prompt)
  124.  
  125. if value < 0:
  126. print("Sorry, your response must not be negative.")
  127. return get_non_negative_int(prompt)
  128. else:
  129. return value
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement