Python for Absolute Beginners Transcripts
Chapter: Writing your first lines of code
Lecture: Getting input from a user

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Last thing we want to look at is getting user input. Now we were able to type name equals Michael. And if this is our source code for our programming
0:10 if this is what we write in our file, in our Python file it's always going to be Michael. It doesn't matter what the person's real name actually is.
0:21 That's always going to say, Hello Michael. If we write those two lines of code. It would be nice if we could ask, Hey, what's your name?
0:30 And there's a very very simple way to do that in Python. Though, because we're planning on typing here over and over
0:35 it's not as obvious as if were running it separately but let's go ahead and do it here anyway. So, the way you get the input from the user
0:42 you ask them for input is well, you just use this input function. And it takes a string value.
0:49 And a string value is the message want to prompt them with. So we can say something like, What is your name? Give it a little space here
0:56 so they're not typing right on the question mark. If we type this it's going to get the information from the user and then store it back into name.
1:03 Let's watch that. What is your name? Now I'm going to pick a different name. My name is Jeff. Okay, what happened?
1:09 Well, let's see what the value of name is. Guess what? It's Jeff and if we run this print thing Hello, Jeff. Awesome.
1:17 Now this looks really good, right? Let's ask what Jeff's age is. Guess what? Jeff is 42. And that looks good. Age 42.
1:29 Let's figure out what their next age is going to be like. Next year you will be. Let's call it next age. It's going to be age plus one, right?
1:38 That makes a lot of sense. Hmmm, you know, maybe not. That was suppose to work. That was supposed to be 43, but what happened?
1:45 Remember this type stuff we talked about? Says you cannot concatenate strings, but not integers. Or if you're trying to do math with integers
1:54 you shouldn't involve strings in it. Cause there's one final step we have to do here when we do this line. Let's run this again. Run it again.
2:01 And what we need to do is. We need to convert, let's just go and run it. 42, we need to convert the age to say number or whatever is going to be
2:12 take the string age and make it a number specifically I'm going to make it an integer. So you just do that and you just pass age along.
2:19 So you say the integer parentheses, give it the age as a string and what comes out is number. Now, if you say num verses age.
2:27 Notice one has quotes and one doesn't. One is a number, one is a string. Now if we want to do this trick we tried before.
2:37 But we make sure to use the converted number next age. 43. We can give them a cool message like Great next year you'll be.
2:51 Now quick, this isn't going to work. Why is it not going to work? I forgot something. Hmmm, that's not as cool as we want it. Is it?
3:00 Let's give it a little exclamation mark. It'll be super excited. And remember, those evaluations in the curly braces only works for formatted strings.
3:09 So you got to put the F. Great, next year you'll be 43! All right, so that's how we got input from user. We used the input function.
3:16 And then what we get back is always, always, always a string. It doesn't matter if you ask them for a number and they type in number looking things.
3:23 Because it has quotes, you know it's a string. Well, that didn't work. So in order to convert to a number, we do integer of age.
3:31 You want to float like 7.2, you would say. Well, Float or Float input numbers and so on. There it is. That's how we get it.
3:40 Input from users and convert it to a data type that means something we can work with.


Talk Python's Mastodon Michael Kennedy's Mastodon