I am currently making a function for simple online shop which requires the user to check whether their age is 18 above or not. I am however, cannot figure out how to check if the user have inputted integer or string data type and return back to check age function. If any of you guys can help, I would appreciate it very much.
def input_age():
age = int(input('Please enter your age\n'))
if age <= 18:
print('Sorry, you are too young. Don\'t shop with us.')
quit()
elif isinstance(age,int): #Check whether age is int
print('Please insert numbers only')
input_age()
elif age > 18:
name = str(input('Please type your name\n'))
print('Welcome ' name.title() '.\n')
input_age() #Execute age function
CodePudding user response:
Put it in a try-except block
def input_age():
try :
age = int(input('Please enter your age\n'))
if age <= 18:
print('Sorry, you are too young. Don\'t shop with us.')
quit()
elif isinstance(age,int): #Check whether age is int
print('Please insert numbers only')
input_age()
elif age > 18:
name = str(input('Please type your name\n'))
print('Welcome ' name.title() '.\n')
except Exception as e:
print("Please enter an integer")
If user doesn't enter an integer, an Exception will be thrown.
Please enter your age
sss
Please enter an integer
CodePudding user response:
There are multiple methods that could be used to determine data types in this situation - however, each is slightly different. Python has the Type()
function. Though this function has three parameters type(object, bases, dict)
, only one is required, the object. If you would like to learn about the other parameters, look here. You can use an if else statement to determine if the object type is a string -
import sys #to be used for error message
user_input = input("prompt")
input_type = Type(user_input)
if input_type == int:
do_whatever()
else:
sys.exit("Please try again with an integer input")
However, due to the nature of the input() function, this will not work in this instance, as input() always returns a string - however, the other two methods are still valid.
Another method is to use an Exception, as mentioned by Sruthi V. When converting to an integer, if a string is inputted, an error will occur.
try:
user_input = input("prompt")
user_input = int(user_input)
except ValueError:
print("Please run the program again and enter an integer")
Finally, you can use the isinstance()
function - essentially an alternative to the Type function. isinstance()
takes two values, (object, type)
. A bool of true or false will be returned, depending on if the type of object is the same as the parameter type
.
import sys
user_input = input("prompt")
if isinstance(user_input, int):
do_whatever()
else:
sys.exit('Run the program again with an integer input')
However, you already check for int at the beginning of your file - an error will occur if any value that is not an int is detected. Therefore, in your specific situation, there is no need to check if the value is int. You may use exceptions to print custom messages on input of a non-integer value, but checking if the value is an int again is repetitive, and uses unnecessary resources.