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