Have you ever written a python script, then got to a part where you have to get input from the user? I would assume your code went something like this:
user_input = int(input("Enter number: "))
Then you realized that if the player enters "keyboard spam"
, then your program crashes. So you need to set up a while-loop exception type thing, like this:
while True:
try:
user_input = int(input("Enter number: "))
break
except:
print("you screwed up, only number allowed")
Then, you realize that your inputs must be in between 5 and 23 for the features of your program, so you just code that in too... All in all, this is taking a while and is a pain in the neck!!! What you need is a function that already does all of this for you!
Enter my function:
def get_input(prompt,
# string conditions
*conditions, check_alpha=False, must_be_lowercase=False, must_be_uppercase=False,
conditions_on_input=False, all_conditions_must_be_true=True,
# now int conditions
must_be_an_int=False, highest_range=100000, lowest_range=-100000, must_be_odd=False, must_be_even=False):
if must_be_uppercase and must_be_lowercase:
raise ValueError("The word cannot be both uppercase and lowercase!!!")
if must_be_even and must_be_odd:
raise ValueError("The word cannot be both even and odd!!!")
while True:
word_valid = True
try:
user_input = input(prompt)
if must_be_an_int: # if the value must be an int
try:
number = int(user_input)
if not lowest_range < number < highest_range:
print(f"Values must be between {lowest_range} and {highest_range}!")
word_valid = False
continue
else: # the number is between the specified range
if must_be_even:
if not number % 2 == 0:
print("Number must be even!")
word_valid = False
continue
elif must_be_odd:
if number % 2 == 0:
print("Number must be odd!")
word_valid = False
continue
except Exception:
print("The word must be an integer!", user_input)
word_valid = False
continue
if word_valid:
return user_input
else: # if the value must be a string
if check_alpha: # here we have a boolean. If it is true, we check to make sure only letter are in the
# user input
if not user_input.isalpha():
print("Only alphabetic characters allowed!")
word_valid = False
continue
if conditions_on_input:
if all_conditions_must_be_true:
for condition in conditions:
if condition not in user_input:
print(f"You have not satisfied the condition that there must be '{condition}!'")
word_valid = False
continue
else:
condition_found = False
for condition in conditions:
if condition in user_input:
condition_found = True
break
if not condition_found:
print(f"You must satisfy one of the following conditions: ", end="\n")
for condition in conditions:
print("'", condition, "'", sep="")
print()
word_valid = False
continue
if must_be_lowercase:
for i in user_input:
if not i.islower():
print("Letters must be lowercase! Try again!")
word_valid = False
continue
elif must_be_uppercase:
for i in user_input:
if not i.isupper():
print("Letters must be uppercase! Try again!")
word_valid = False
continue
if word_valid:
return user_input
except Exception:
print("No funny business!! Try again!")
Although my function may be a bit chunky, it gets the job done. Note: All of the below features you use as parameters, not modifications to the function itself...
Features:
first you put in the prompt, nothing special here
next you put in letters (or numbers) you want in the program, each of those as a positional argument. These get classified as conditions. These only apply if the boolean
must_be_an_int
isFalse
.next, if you want to use the conditions, you must set the bool
conditions_on_input
toTrue
, whose default value isFalse
.There are two ways to use the conditions. There is a) all of the conditions must be met, or b) only one of the conditions must be met. The way to toggle this is by use of the keyword
all_conditions_must_be_true
. It's default value isTrue
.Using the keyword
check_alpha
you can check whether or not the value must only be in the alphabet.Setting the keyword
must_be_lowercase
toTrue
makes it so that your must phrase must be completely lowercase, same withmust_be_uppercase
. NOTE: If you set bothmust_be_uppercase
andmust_be_lowercase
toTrue
, the program will raise an error.Those are all of the string conditions. The next keyword is
must_be_an_int
. If it isTrue
, it will NOT check any of the string conditions, it will only check the following integer conditions. If it isFalse
, (its default value), it will only check the string and not the ints.highest_range
andlowest_range
, respectively are the highest and lowest ranges that your number can be. They are by default, one hundred thousand and negative one hundred thousand.must_be_odd
andmust_be_even
. They are self-explanatory. Once again, if both areTrue
, the program will raise an error.
I am thinking of ways to improve this and make this better, thanks for any feedback!