Understanding Python: User Input

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back to understanding python my name is jake and today i'll be teaching you how to get user input in python if you are interested in writing amazing interactive command line applications understanding input is essential as we go if you learned something new let me know by liking the video now let's get started the built-in function that python has that allows us to get user input is simply called input now input doesn't have to have an argument passed into it but the argument that it does accept is called a prompt so this is effectively the question that you're asking a user in this example we'll ask what is your name and what this will do is it'll print the question what is your name to the screen and then wait for the user to provide a response once it does it's going to return that value of the response in the form of a string so we'll save that to a new variable called name so name is going to store a string that should be their name as long as they're truthful and for a variation of hello world we'll simply print that back to them here we print an f string hello name injecting the name that we got above into the string that we're printing back out to them easy enough so let's run this in here at the bottom we see we have a prompt what is your name and right now the program's not doing anything else but waiting for me to give some form of response i'll be truthful and tell my name and we get hello jake and then the program exits now an alternate version of this is to print the message first then prompt below it now to demonstrate this let's ask them their age so first we'll print what is your age and then below that question is where we're going to ask for our input so we'll store the age in a variable called age and here we're just going to prompt them a little bit more a line that says age with a colon and then a space just so it looks nice when they see that and then print out u are age years old so let's run this and see what that looks like what is your name jake hello jake what is your age and then down here we see we have a new line that is prompting me for my age and here again i'll tell the truth say 33 you are 33 years old but what if you want to handle the case where a baby is using your application and it is just one year old well one way we can handle this is within the string that we're printing itself follow me here here we're going to say s if age is greater than one else nothing now this looks good but we're going to have a problem and let's see if you catch it when i run this was your name jake what's your age 33. oh we get a type error it says greater than is not supported between instances of string and int because remember that input returns a string so if you want to store that as something else you're going to have to explicitly convert that at some point here we'll just do it right after we get the input so now we have age equals integer input age if we save and run that then we'll say jake age 33 you are 33 years old run it again baby what is your age one you are one year old while we're on the subject of handling different types of responses there's another problem we can encounter with this kind of pattern let's say we have a user that is just hitting enter going through things hello whatever was your age hit enter again whatever now we get a value error because it can't convert an empty string into an integer so with this what we could do up here input is say input age or and then we can give a default value since they want to be sassy we'll give them an age of 100. so this just lets our program handle the single event of someone hitting enter and not actually providing a value and the way that this works is since input is returning an empty string as we see down here empty string is a falsy value so in python when you say one value or another value if the first value is falsy then it's going to go with the second value so let's save and run this again what's your name hit enter hello blank what's your age hit enter you are 100 years old shame on me now since the values being returned are strings all the different string manipulation techniques that are available in python are also available to you when you're accepting input from users so to demonstrate this let's get a list of hobbies and that's going to be equal to input and we're going to say what are your hobbies and we want this to be a comma separated list so in here we'll just give a little bit of hinting to the user comma separated now what we're going to do here is we're going to combine a few different techniques so let's build those techniques up so if we want to split a string into a list with each value that's separated by those commas we can do that by doing split so hoppies.split with a comma but one user might think that a comma separated list has no spaces so one two three and another user might think that a comma separated list does have spaces just like if you type it out in the sentence one two 3 well we can handle that both by then performing a strip on the values after they're split and to do that we're going to use map as covered in my last video so here we're going to say hobbies is equal to map we'll put a comma there and then as map iterates over each value we can perform a strip by doing that in a lambda function lambda s s here standing for the particular string in this list s dot strip so this is going to remove any extra white space at the beginning or the end of that particular string nice so we just accepted a comma separated list from a user we split it apart based on where those commas are and then we clean up all the extra white space speaking about cleaning up extra white space let's go ahead and get rid of that extra space right there now we could do a number of interesting things with these but since that's kind of out of the scope of this video we'll just print those right back out to the user your hobbies are and then we're just going to iterate over hobbies for hobby in hobbies print hobby and if we want to be a little bit more fancy we can capitalize it all right we'll save that and run it what's your name jake what's your age 33 what are your hobbies we'll say programming video games making videos and music perfect and here we see your hobbies are programming video games making videos and music now before we go i'm going to show you one way of how you can make an interactive multi-choice menu and for that we're going to combine input and while loops to create that classic choice menus that you may have seen in the past to make things easier for us later we're going to define a new function called menu and all this is going to do is print out the text of our menu i'll go ahead and write this out real quick and bring you back in when i'm done okay now we have the text of our menu so we have options one through five that lets a user set their name age address print out the information or exit now the next thing we're going to do is initialize name age and address we can do this all in one line set them all equal to none if you're confused about how this works check out my video on variable unpacking there's a lot of good information in there and i think you're going to like it we'll run menu which will print out our menu next we're going to do a while loop now this is actually going to use a feature that's new in python 3.8 which is a little controversial but is perfect for situations like this we're going to have a variable called choice and we're going to use the walrus operator or the assignment operator to get our input so we're going to pull this into an end because if you see our indexes up there we have one through five we want to evaluate that on one through five then we're going to get our input and this is going to be a simple prompt that's just the pound sign with the space letting them know to choose a number if they don't choose a number and just hit enter we'll default to five which will escape and then the not is going to be equal to five so while the choice they make is not equal to five we're going to go ahead and do the different things that is said above now to act on this choice we'll make a series of if lf statements so if choice is equal to 1 we're going to store the name being another input call and we're simply going to prompt for name and then we can do the same thing for age and address so we'll copy this we'll put in l there out there two becomes age three becomes address now our next elif is going to be choice 4 which is going to print out the information and here we're just going to print out a formatted string again using a python 38 feature where we can say a variable equals something which will give us name equals and then the value of name we're going to separate these by newline characters age equals and address equals and we'll also follow that with another new line just to give us a little bit more space when we print the menu again speaking of which at the very end we're going to print the menu again oh and before i forget let's make sure we do an l up there now you may be thinking why don't you have an lift choice equals five well that's going to be captured at the very top line there so if someone enters five will immediately escape okay to reduce noise let's go ahead and comment out all the stuff above if you're using an editor like vs code you can just highlight everything and hit control forward slash and then to avoid confusion later let's make sure we change these to all reference to proper variables alright we'll save it and run it and here we see we have our menu one through five exactly as we expected with our prompt at the bottom which is that pound sign followed by space so here we'll put one to set our name name jake after we enter we're presented with the menu again so 2 would be set age so we'll set our age equal to 33 set address we do choice number 3 address will say 123 mockingbird lane and then print the information we'll hit four and here we see name equals jake age 33 address 123 mockingbird lane and because this is a loop we can change anything just by making the choice and then making the change we'll go ahead and make myself 20. that's nice and of course if we enter five it'll exit and since we set the default choice to 5 if we just hit enter here it'll also exit perfect so that's how you make a basic multi-choice menu system in python by simply using a while loop combined with input and that wraps up this video now that you understand how to get user input you can use this in numerous creative ways what is your favorite trick for input or was it something i showed today leave a comment down below to let me know as always today's code will be added to the understanding github repo so check the description for a link and of course if you have any questions or suggestions for topics you'd like me to cover leave a comment for me to keep up with this series please consider subscribing thanks for watching [Music] you
Info
Channel: Jake Callahan
Views: 59
Rating: undefined out of 5
Keywords: understanding python, python map, python beginner, intermediate python, python tutorial, python builtins, python for beginners, advanced python, python functions, learning python, python programming, python coding, python automation, data science, python data, python basics, python course, data manipulation, python lists, python iterators, python iterables, python data science, python projects, python, python input, python user input, python menu, interactive menu
Id: oaAXapPg1-0
Channel Id: undefined
Length: 15min 12sec (912 seconds)
Published: Thu Aug 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.