Skip to content

Instantly share code, notes, and snippets.

@JackKell
Last active March 7, 2020 20:07
Show Gist options
  • Save JackKell/a5d09556d560f7df60b378d02872755d to your computer and use it in GitHub Desktop.
Save JackKell/a5d09556d560f7df60b378d02872755d to your computer and use it in GitHub Desktop.
# getting an integer
def getIntegerInput(prompt: str, lowerBound: int = None, upperBound: int = None) -> int:
while True:
userInput = input(prompt)
try:
userInput = int(userInput)
if lowerBound and userInput < lowerBound:
print(f"Input must be greater than or equal to {str(lowerBound)}")
elif upperBound and userInput > upperBound:
print(f"Input must be less than or equal to {str(upperBound)}")
except ValueError:
print(f"{userInput}: is not a valid integer")
continue
return userInput
# Example usages
Day = getIntegerInput("Enter the day which you were born in: ", 1, 31)
Month = getIntegerInput("Enter the number of the month you were born in: ", 1, 12)
Year = getIntegerInput("Enter the year you were born in: ", 1950)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment