Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Readability
Source Link
Mateen Ulhaq
  • 27.9k
  • 21
  • 122
  • 155
def sanitised_input(prompt, type_=None, min_=None, max_=None, range_=None):
 if min_ is not None and max_ is not None and max_ < min_:
 raise ValueError("min_ must be less than or equal to max_.")
 while True:
 ui = input(prompt)
 if type_ is not None:
 try:
 ui = type_(ui)
 except ValueError:
 print("Input type must be {0}.".format(type_.__name__))
 continue
 if max_ is not None and ui > max_:
 print("Input must be less than or equal to {0}.".format(max_))
 elif min_ is not None and ui < min_:
 print("Input must be greater than or equal to {0}.".format(min_))
 elif range_ is not None and ui not in range_:
 if isinstance(range_, range):
 template = "Input must be between {0.start} and {0.stop}."
 print(template.format(range_))
 else:
 template = "Input must be {0}."
 if len(range_) == 1:
 print(template.format(*range_))
 else:
 print(template.format("expected or= ".join((", or ".join(map(str,
 ", ".join(str(x) for x in range_[:-1]),
 str(range_[:-1])),
 ))
 strprint(range_[-1])))template.format(expected))
 else:
 return ui
def sanitised_input(prompt, type_=None, min_=None, max_=None, range_=None):
 if min_ is not None and max_ is not None and max_ < min_:
 raise ValueError("min_ must be less than or equal to max_.")
 while True:
 ui = input(prompt)
 if type_ is not None:
 try:
 ui = type_(ui)
 except ValueError:
 print("Input type must be {0}.".format(type_.__name__))
 continue
 if max_ is not None and ui > max_:
 print("Input must be less than or equal to {0}.".format(max_))
 elif min_ is not None and ui < min_:
 print("Input must be greater than or equal to {0}.".format(min_))
 elif range_ is not None and ui not in range_:
 if isinstance(range_, range):
 template = "Input must be between {0.start} and {0.stop}."
 print(template.format(range_))
 else:
 template = "Input must be {0}."
 if len(range_) == 1:
 print(template.format(*range_))
 else:
 print(template.format(" or ".join((", ".join(map(str,
 range_[:-1])),
 str(range_[-1])))))
 else:
 return ui
def sanitised_input(prompt, type_=None, min_=None, max_=None, range_=None):
 if min_ is not None and max_ is not None and max_ < min_:
 raise ValueError("min_ must be less than or equal to max_.")
 while True:
 ui = input(prompt)
 if type_ is not None:
 try:
 ui = type_(ui)
 except ValueError:
 print("Input type must be {0}.".format(type_.__name__))
 continue
 if max_ is not None and ui > max_:
 print("Input must be less than or equal to {0}.".format(max_))
 elif min_ is not None and ui < min_:
 print("Input must be greater than or equal to {0}.".format(min_))
 elif range_ is not None and ui not in range_:
 if isinstance(range_, range):
 template = "Input must be between {0.start} and {0.stop}."
 print(template.format(range_))
 else:
 template = "Input must be {0}."
 if len(range_) == 1:
 print(template.format(*range_))
 else:
 expected = " or ".join((
 ", ".join(str(x) for x in range_[:-1]),
 str(range_[-1])
 ))
 print(template.format(expected))
 else:
 return ui
deleted 6 characters in body
Source Link
Trenton McKinney
  • 63.2k
  • 41
  • 170
  • 214

The simplest way to accomplish this would beis to put the input method in a while loop. Use continue when you get bad input, and break out of the loop when you're satisfied.

The simplest way to accomplish this would be to put the input method in a while loop. Use continue when you get bad input, and break out of the loop when you're satisfied.

The simplest way to accomplish this is to put the input method in a while loop. Use continue when you get bad input, and break out of the loop when you're satisfied.

"except", not "catch"
Source Link
wjandrea
  • 34k
  • 10
  • 69
  • 107

Use trytry and catchexcept to detect when the user enters data that can't be parsed.

Use try and catch to detect when the user enters data that can't be parsed.

Use try and except to detect when the user enters data that can't be parsed.

Bounty Awarded with 350 reputation awarded by Aran-Fey
Small edit of the example usage in section "putting it all together"
Source Link
Loading
Removed spaces at end of lines
Source Link
Antoine
  • 4.1k
  • 2
  • 29
  • 48
Loading
Bounty Awarded with 100 reputation awarded by Martijn Pieters
added 90 characters in body
Source Link
jonrsharpe
  • 123.4k
  • 31
  • 278
  • 489
Loading
Bounty Awarded with 300 reputation awarded by Jon Clements
Added an example of custom validation
Source Link
jonrsharpe
  • 123.4k
  • 31
  • 278
  • 489
Loading
Adding generalised input function
Source Link
jonrsharpe
  • 123.4k
  • 31
  • 278
  • 489
Loading
Source Link
Kevin
  • 76.5k
  • 13
  • 141
  • 168
Loading
Post Made Community Wiki by Kevin
lang-py

AltStyle によって変換されたページ (->オリジナル) /