How to Validate User Inputs in Python | Input Validation in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
When you ask a user to enter something you need to  validate the input so you're sure they've entered   a valid value. This is what I'm gonna teach you in  today's video, make sure you stick until the end   because I'm gonna give you lots of examples and  tips to make your life easier. So, here we are,   I've just created validate_user_inputs.py and here  we're gonna have a look at different validations.   So, we're gonna start out with validating if  the user enters a number. So, let's start with user_input is equal to input() "Enter a number" and of course if we want to print So, this wouldn't work because user_input,  the input is actually a string so even if   I wrote three, the three would be a string  not an integer or float so, we need to convert it. We can do something like float   and then user_input or we can do something like  int, integer and we're gonna stick to integer. So now this will work but what if the user  actually enters a letter? This wouldn't work   because int() only converts strings that can  be converted to a number so for example "3",   a string "3" could be converted to 3, number 3.  But if we entered "hello", "hello" wouldn't be   converted to an integer because how can you  convert a word to an integer? You can't. So, we're   going to take advantage of this impossibility  to convert a string, a word to number   and we're going to use it to validate, so, for  example if you do something like try user_input,   int, user_input, except, ValueError print "invalid" "You need to enter a number", something  like that. So here we are trying to convert   what the user entered to a number,  if this error occurs, so ValueError that means that that string couldn't be  converted to a number because it's actually   not a number. So that would be invalid  and the user would be shown this This, so let's actually try if we get invalid.  So, "Enter number", "Invalid: you need to   enter a number". If you enter a number you don't  see invalid you don't see anything. So, this   could be used to check if the user actually  entered the number but we want python to   actually keep asking the user for a number  until they enter a number, so to do that   we're going to use the while True loop. So, we're  gonna get this, actually I'm gonna write that   right here, so, while True, I'm gonna do  this, we're gonna get this and put it in here.   So we're gonna try to convert the number if  the number can't be converted you need to tell   the user that they entered something invalid and  then down here we can actually print the result,   let's say 2 + user_input, because here we are sure  that the user_input is actually a number because   otherwise the loop would keep looping and looping  and looping. By the way, of course you need to   break out the loop because otherwise that would  keep looping anyway so here if the user_input is   converted to an integer correctly then you break.  If the user_input cannot be converted to a number,   then this runs, the except runs and the user  is shown this. So let's try it out real quick so "Enter a number", "r", "Invalid", "e",  "Invalid", 5, 5 + 2, 7. So this is the first   way you can check, you can validate an input  and here you're checking if the user entered   a number, a valid number. You can check  the other way around so if the user entered   anything but a number and that's really easy  because you can just do something like this,   so let's copy that paste it here. So  here you copy this and you place it here and down here you break. So what happens here?  Here, you try to convert it so if the string   can be converted to a number that means that  the user entered a number, we don't want them   to enter a number, so "Enter anything but a  number", so we want anything like a special   character whatever but not a number. So here as I  said we try to convert it, if the user entered 1,   which is not valid, this works because of  course you can convert the number to an integer,   so we need to print "Invalid" and the while True  will start again. But if this throws an error,   that means that the user actually entered  something different from a number and we   take advantage of the ValueError, so here in this  case we want the error to occur so that we know   that the user entered something else not a number  and we break out of it. If that makes sense. So,   here we can't do something like 2 + because that  would throw an error. Let's actually try that out so "Enter anything but a number", "3", "Invalid,  you need to enter..." ah, of  course here you need to enter "Anything...." so that would work "4", "Anything but a number", "2", "Anything but  a number", "hello", "hello", perfect! That works!   Another thing we can do is we can check  the presence of an invalid character in   the user_input, so for example when setting up  a nickname sometimes you don't want the user   to enter certain characters and so you can do  something like this, so, let's comment this out.   So first of all you start out with a list of  characters that you don't want to allow so dot slash semicolon, I'm just making this up   so it doesn't really make a lot of sense. So  here we print a little space and then nickname something like, input, "Enter a nickname" this, this, this not allowed,   something like that. So the user knows that  they cannot enter these characters then, invalid counter. And we are going to use this later on to  check if there is at least one invalid character   inside of the user_input if there is we're  going to tell the user "wait you entered   something wrong, enter a valid nickname".  So, then, for char in not_allowed_characters if char in nickname, invalid_chars_counter plus  equal 1, we add one. So basically here we loop   through these and for each one of these we  check if this is included in the nickname,   if this is included in the nickname, if this  is included in nickname. If so we add one   to the invalid_chars_counter. Then, after the  loop we do something like if invalid_chars_counter   is greater than 0, which means that  we've encountered an invalid character,   we print something to the user  and we do something like "Invalid the nickname cannot contain dot slash semicolon" else, we break. And down here we can do something  like, print and print "welcome plus nickname".   So basically here we've got the characters that we  don't want the user to have in the nickname, here   we get the user_input, we've got the counter of  the invalid characters, we go through this list,   if one of these is inside, so we check one by  one, if this is inside of nickname let's say   fabio dot something. That would increase the counter   and if the counter is greater than zero  then we've got an invalid character,   if not we break and we can use the nickname  down below. So, let's try that out,   let's clear. "Enter a nickname", let's  say "fabio.", "Invalid", let's say "programmer/" or something like  that and "fabioprogrammer",  something like that, "Welcome fabioprogrammer",  because we didn't write any of these characters   in the nickname, so this is working perfectly. Then, last but not least, maybe you want the  user to enter only specific set of letters or   values for example, five letters of the alphabet  or maybe all the letters of the alphabet but not   like special characters and stuff like that.  Or for example you want the user to enter just   one letter and not just three or four letters  or a name or something like that, like for the   hangman game which I've made a video about and  here I'm gonna show you how you can do that. So   I'm going to comment this out again, let's  go down here, like that. So first of all you   need a list of valid characters you want to set  I'm going to copy and paste the whole alphabet.   Something like that. And then I'm  going to start with while True print, then ,same user_input, input,  "Enter one letter", just one letter. Then   if user_input is not in list_valid_characters,  actually let's write chars which makes more sense . So here I'm checking  a lot of things. I'm checking the length   because I've got just one letter per element  which means that if the user enters "ab" in   the same input, that wouldn't work because  we don't have an "ab" element in here just   an "a" and a "b". So we're checking the  length, we're checking if the user enters   the right letters and stuff like that,  so we're checking a lot of things   just by doing this. So, if user_input not in  list_valid_chars we're gonna print "Invalid: enter a letter of the alphabet" Something like that. So then, if  the user_input is in the list,   in this list, which is actually  a tuple, let's call it tuple. A list would work as well so, no worries.  We're going to break, of course, we are going   to break and then down here we can use the  the letter to do whatever we want to do, so, "You entered user_input", I'm using the f string.   So here we are giving the user a list of options  and only if the user enters one of these options   does the program actually go ahead, otherwise  it keeps looping and looping until they enter   the right letter, the right character. So let's  try that out, "Enter one letter", "3", "Invalid". As you can see it doesn't work, doesn't work until  I enter just one letter "You entered r". Perfect!
Info
Channel: Fabio Musanni - Programming Channel
Views: 36,824
Rating: undefined out of 5
Keywords: python, python3, programming, coding, tutorial, education, input, validating inputs, check inputs, validate inputs list, while loop validation, while loop input validation, validate input in python, while true loop validation python, valdate inpts python, input validation loop python, python loop validation, validate user input python, validate user inptu python, validation of inputs python, how to validate user input in python, how to choose option python, choose option python
Id: LUWyA3m_-r0
Channel Id: undefined
Length: 14min 14sec (854 seconds)
Published: Wed Jul 27 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.