Python Tutorial 3: Getting User Input from Keyboard

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter with toptechboy.com and we're here today with lesson number three in our incredible tutorial series where you are going to learn to code in python or you are going to die trying i'm going to need you to pour yourself a nice big cup of black coffee because it is caffeine that fuels the engineering world and i'm going to need you to get out your python idol and as you are firing up your idol as always i want to give a shout out to you guys who are helping me out over at patreon it is your support and your encouragement that keeps this great content coming you guys that are not helping out yet look down in the description there is a link over to my patreon account think about hopping on over there and hooking a brother up but enough of this shameless self-promotion let's jump in and get ready to learn some cool new stuff okay now what we've done in lesson number one you learned how to install python and you learned how to do some basic math operators play around with variables in lesson number two you learned how to do list or arrays a more complicated uh data type a more complicated variable type you learned how to do that but up until this point we have been sort of writing our code one line at a time so i would say something like x is equal to seven and then i would say y is equal to three and then i would say z is equal to x plus y and then i would say print z and that's kind of convenient again python is execution ready it will do one line of code at a time and you can just sit and play with it like that that's kind of actually useful for learning things because you see we can just sit and kind of dabble with it and learn things but then really quickly which is for us right now you get to the point that you actually need to write programs where you stack lines of code together and then run them as a program and that is what we are going to do today and so you get there from your idle you will come up and you will point at file and you will click and come down to new file and when you do new file you have what basically comes up and it is like a blank text editor you see i can just type in it and what do you notice you notice that you don't get that little python prompt where python is waiting for a command here we are going to write a file and then we're going to run the whole file so it's not going to execute it one line at a time it's going to be more like your old you know classical writing a program so let's go ahead and save this so i am going to save as and you need to somewhere create yourself a python folder and i've got probably something here set up already i've got uh inside of my top tech boy uh folder i've got a folder called python you create a folder wherever you want and then you put your python files there and i'm going to call this let's just call this my first python okay and then what you see is it's going to put a dot py file extension so if i say save okay it creates it i'm just going to come and make sure that that's actually there and so i will open this up quick access go to python and boom there is my python my first python dot pi it's in there with my old videos probably not the best place to put it but we will continue to put it there anyway you can put yours wherever you want all right now look at this you see i can say greed greeting is equal to the string greeting is equal to the string hello world okay and then i can say print and then i'm going to print greeting and then now i'm going to come up here and i'm going to say under run i know you guys can see my topping for the coding pretty well but i know that the labels on the window are small so i'll tell you very carefully you go to run you click on run and you come to run module and you say it must be saved okay save it and then you come back to your shell right it prints out to your shell so it opens up that shell window and it prints what hello world shazam our first program that we've written in python as an actual program a boom okay we got that done now i don't want to kill this i'm just going to minimize it so that way it'll pop up the right size in the future okay so now let's learn some new stuff so let's just say that i can say x is equal to 7 and i can say y is equal to 2 2 and then i can say z is equal to x plus y this is like what we were doing earlier just kind of like one line at a time and then i can say print right we want to print it nicely formatted so i can say print x and then print the string space plus space and then comma y and then i want to put in the string space equal space and close that string and then print z like that that looks pretty good doesn't it and then again run and then run module must be saved okay boom seven plus two equal nine we are python program writing machines yes we are python program writing machines okay so that's kind of like what we were doing before right it's like what we were doing before i'm going to come back to the programming window it's like what we were doing before only now we're stacking the lines of code together all right now let's say you wanted to get input from the user the way you get because that's like right off the bat whatever you're going to want to do you've got to get input from the user and remember you guys that are coming from arduino how did we get input from the user in arduino we had to do three things we had to ask for it we had to wait for it and we had to read and so we asked with a print statement and then we waited with a little infinite while loop and then we read it with a uh like a serial.parsen or serial.parse uh parsefloat something like that okay so getting user input from the arduino was a little bit of a three-step process it's much much easier in python and this is how you do it you just say let's say x is equal to and then what you do is you say input and then you open up the input and then you okay put in your prompt what is your prompt please input your number and then you close the prompt and then you close the input parentheses okay like that so x is equal to please input your number so you're getting input from the keyboard you are prompting the user to please input your number and then you are putting the number that they input into x and then what i can do is i can say print and then i can say your number your number is okay and then comma x like that all right does that look good okay so let's run this and this is most annoying that it always makes me save it okay it says please input your number and i will say 7 okay and then it prints hello world that is kind of crazy what did we do on this program we better come back over here x is equal to please input your number your number is x that was kind of crazy let's try running that again run module okay my number is seven and it says your number is seven i don't know what happened up there it's like i got an unexpected result but anyway it's working now so your number is seven all right right off the bat something i would be really really picky on with you guys and this annoys me with the students that i have in my classroom what is wrong with this you see how it said please input your number and then it put the 7 here you see i didn't leave a space there so that it would look nicely i'm not nicely formatting my requests for user input so what would be better if you said please input your number do something like a colon and a space and now let's look at what a big difference that makes as far as nice looking click code that's why i always think about things that make sense things that are organized things that look good to the user so i'm going to say run module now save it okay and now please input your number okay now look you see how i put a space there and now when i put in 7 it looks nice your number is 7. much nicer i must say much much nicer i must say okay so let's come back over here and you see that that actually worked very very well and so let's try this let's see if we can input a string so i will say name is equal to input to get it from the user please enter your name like that okay please enter your name so that's going to go into name and then i'm going to print i'm going to print the string hello and then i'll put a space and then close that and then what name and then a comma right as i'm stacking these prints together you got to put a comma between everything name and then i'm going to say welcome to python land welcome to python land all right i love python land i love it a lot okay so hello name welcome to python so now we are going to run and the most annoying save the program first okay enter your name i am paul oh what did i just do did you see guys i did not put the space in there like i told you to okay but it says hello paul welcome to python land need to do a little bit better punctuation so i will come back to the program and i'm going to say make it nicely formatted like that and then i am going to say hello name and then i think i really need a comma after name like that and then welcome to python land let's put a little pizzazz with an exclamation point and now let's try this run module okay so it asks me for my name i am paul look at that nice formatting uh-huh who's doing cool formatting yes sir okay hello paul that should have had a that should have had a a space after that let's try cleaning that up a little bit and so there should be a space right there and not a there's not a spacer okay so this should work so now we're going to run it run module save it okay my name is paul hello paul welcome to python land all right boom we are making code and i guess i should let you see that exclamation point we are writing code so this is pretty cool okay this has been kind of like a a pretty quick lesson a pretty easy lesson we're just kind of taking a lot of the stuff that we learned earlier and what we are doing is we're stacking it together and now writing programs time for your homework okay you guys really do your homework don't just sit and copy and paste what i'm doing do your homework all right so this is your homework in this lesson lesson number three and then i will show you i will do in lesson number four i will solve it for you and talk to you about it okay so what your program should do is it should ask the user for its for for his first or his or her first number so say please enter your first number okay then ask the user for their second number you know please input your second number okay so you're going to read from the user their first number you're going to read from the user their second number and then you're going to say something like if you read it into x and then you read it into y you're going to say z is equal to x plus y okay you're going to read it and then you're going to very nicely format it format it in something like your answer is x plus y equal 9. so if you enter 7 as x and 2 in the second one then you're going to say 7 plus 2 equal 9. so that's your homework get two numbers from the user and then nicely format nicely print out the answer including the equations to be like seven plus two equal nine okay and then if you i can't imagine you would have any trouble with that but if you do you might go back review the earlier lessons but really guys try to make it work without watching me do it okay because you gotta learn you're not gonna learn unless you do it on your own okay guys i hope you're having as much fun taking these lessons as i'm having making them if you like this make sure to give a thumbs up leave a comment down below and if you haven't already i need you to hit that sub subscribe button when you subscribe make sure you ring the bell so you will get notification when more of these exceptional tutorials come out again this is paul mcquarter from toptechboy.com i will talk to you guys later
Info
Channel: Paul McWhorter
Views: 18,848
Rating: undefined out of 5
Keywords:
Id: E1jlxvt8eDY
Channel Id: undefined
Length: 14min 28sec (868 seconds)
Published: Wed Mar 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.