WEBVTT 1 00:00:00.450 --> 00:00:02.220 line:15% Hi guys and welcome back. 2 00:00:02.220 --> 00:00:03.780 line:15% In this video, we're going to learn about 3 00:00:03.780 --> 00:00:06.560 line:15% actually getting data from our users, 4 00:00:06.560 --> 00:00:10.040 so that our programmes can start being a bit more dynamic. 5 00:00:10.040 --> 00:00:11.853 Imagine you've got your name, 6 00:00:12.970 --> 00:00:16.053 and it's Jose, and you want the user's name. 7 00:00:16.980 --> 00:00:19.540 But, how do you get it? 8 00:00:19.540 --> 00:00:21.920 Do you expect the user to edit the programme 9 00:00:21.920 --> 00:00:23.783 so they can put their name in here? 10 00:00:25.010 --> 00:00:27.620 Finally, what you'll want to do is to print, 11 00:00:27.620 --> 00:00:30.763 you know something like "Hello {your_name}. 12 00:00:32.217 --> 00:00:34.327 "My name is {my_name}." 13 00:00:34.327 --> 00:00:37.140 Let's say we have a simple programme like this. 14 00:00:37.140 --> 00:00:40.240 So how do you get the user's name? 15 00:00:40.240 --> 00:00:42.590 Because if you run this right now, 16 00:00:42.590 --> 00:00:44.657 you'll get "Hello, empty string. 17 00:00:44.657 --> 00:00:45.897 "My name is Jose." 18 00:00:47.230 --> 00:00:50.570 If the user has to come in and edit the programme, 19 00:00:50.570 --> 00:00:53.680 then, you know, this'll work, right? 20 00:00:53.680 --> 00:00:56.780 But, how are you gonna share this with users 21 00:00:56.780 --> 00:00:58.660 that don't know any programming? 22 00:00:58.660 --> 00:01:00.790 So, what we have to do is we have to first 23 00:01:00.790 --> 00:01:02.960 ask the user for their name, 24 00:01:02.960 --> 00:01:06.740 and allow them to type something here in this text console 25 00:01:06.740 --> 00:01:10.100 and then use that name inside our programme. 26 00:01:10.100 --> 00:01:13.963 So here is where the input function comes in. 27 00:01:15.580 --> 00:01:19.255 So we will type input("Enter your name: ") 28 00:01:19.255 --> 00:01:20.660 and what this is going to do is it's going to 29 00:01:20.660 --> 00:01:23.190 open up a prompt here in the console 30 00:01:23.190 --> 00:01:25.480 and notice that the programme is stopped 31 00:01:25.480 --> 00:01:28.300 until we press the Enter key. 32 00:01:28.300 --> 00:01:32.557 So if I type Rolf now, then you'll see that we get back 33 00:01:32.557 --> 00:01:33.390 "Hello Rolf. 34 00:01:33.390 --> 00:01:34.597 "My name is Jose." 35 00:01:35.670 --> 00:01:38.550 This tells you one thing about the input function, 36 00:01:38.550 --> 00:01:42.240 which is that the thing that the user typed 37 00:01:42.240 --> 00:01:46.143 came back to us, and it must have been a string. 38 00:01:47.260 --> 00:01:50.650 Because it contains characters, it is not an integer 39 00:01:50.650 --> 00:01:54.800 and it is not a float, otherwise it would have to be numbers 40 00:01:54.800 --> 00:01:56.500 and you would be able to do maths on it. 41 00:01:56.500 --> 00:01:58.790 So it must be a string. 42 00:01:58.790 --> 00:02:00.490 And that is something that always happens 43 00:02:00.490 --> 00:02:02.260 with the input function. 44 00:02:02.260 --> 00:02:05.560 Anything the user types, even if it is 45 00:02:05.560 --> 00:02:08.980 the number 35, is a string. 46 00:02:08.980 --> 00:02:12.690 A string that contains the characters three and five. 47 00:02:12.690 --> 00:02:15.590 With that in mind, let's try something else. 48 00:02:15.590 --> 00:02:18.343 Let's try asking the user for their age. 49 00:02:19.540 --> 00:02:21.577 And then we're going to print 50 00:02:21.577 --> 00:02:26.443 "You have lived for {age * 12} months." 51 00:02:27.550 --> 00:02:30.040 So a very popular first programme, 52 00:02:30.040 --> 00:02:32.440 we're gonna ask the user for their age, 53 00:02:32.440 --> 00:02:34.590 and then we're going to say, for example, 54 00:02:34.590 --> 00:02:36.610 they might in there enter three there, 55 00:02:36.610 --> 00:02:39.670 we're gonna say, "You have lived for 36 months." 56 00:02:39.670 --> 00:02:40.503 Right? 57 00:02:41.470 --> 00:02:46.470 But remember, that the age variable has a string in it. 58 00:02:46.780 --> 00:02:50.130 The input function always gives you back a string, 59 00:02:50.130 --> 00:02:52.470 even if you enter a number. 60 00:02:52.470 --> 00:02:55.890 So, we aren't turning this into a number. 61 00:02:55.890 --> 00:02:59.913 What is going to happen when we multiply a string by 12? 62 00:03:02.260 --> 00:03:03.860 Well, let me type three in here. 63 00:03:05.260 --> 00:03:07.363 And you get 333333333333. 64 00:03:09.025 --> 00:03:09.858 You get 12 threes. 65 00:03:11.180 --> 00:03:14.630 Because age times 12 is more or less the same as 66 00:03:14.630 --> 00:03:18.210 age plus age plus age plus age 12 times. 67 00:03:18.210 --> 00:03:21.260 And remember what happens when we add two strings together? 68 00:03:21.260 --> 00:03:22.800 They get joined together. 69 00:03:22.800 --> 00:03:24.740 They get concatenated. 70 00:03:24.740 --> 00:03:27.340 So, by multiplying a string by a number, 71 00:03:27.340 --> 00:03:30.553 really what you're getting is 12 of those strings. 72 00:03:31.750 --> 00:03:35.520 What you have to do is you have to turn it into an integer. 73 00:03:35.520 --> 00:03:40.520 So, the same way that we used S-T-R to turn a integer, 74 00:03:40.970 --> 00:03:43.530 or another number into a string, 75 00:03:43.530 --> 00:03:48.530 we can use I-N-T or int to turn a string into an integer. 76 00:03:49.880 --> 00:03:52.470 So, we will turn age into an integer 77 00:03:52.470 --> 00:03:54.140 and put it inside age_num, 78 00:03:55.130 --> 00:03:57.763 and then we will do age_num times 12. 79 00:03:59.040 --> 00:04:00.750 And that works just as well. 80 00:04:00.750 --> 00:04:02.570 Now something important about Python 81 00:04:02.570 --> 00:04:04.220 that you're gonna see quite a lot 82 00:04:05.110 --> 00:04:10.110 is that when you evaluate one of these things here, 83 00:04:10.670 --> 00:04:15.100 that becomes the value that the user has given you. 84 00:04:15.100 --> 00:04:16.730 So, here's what I mean. 85 00:04:16.730 --> 00:04:19.060 This text that I've got selected here 86 00:04:19.060 --> 00:04:22.710 is three when your programme runs. 87 00:04:22.710 --> 00:04:27.220 So, if the user is going to enter three in your programme, 88 00:04:27.220 --> 00:04:30.060 this entire text I've got selected 89 00:04:30.060 --> 00:04:35.060 is exactly identical to you just typing three in there. 90 00:04:35.530 --> 00:04:37.230 Means exactly the same thing. 91 00:04:37.230 --> 00:04:40.860 Of course, the user can enter something different, 92 00:04:40.860 --> 00:04:44.700 if they'd like, so this is why this is a dynamic value. 93 00:04:44.700 --> 00:04:47.620 But, if they enter three, it's the same thing 94 00:04:47.620 --> 00:04:48.930 as having three in there. 95 00:04:48.930 --> 00:04:52.750 If they enter 35, it's the same thing as having 35 in there. 96 00:04:52.750 --> 00:04:57.523 So, we could do age equal int of 35, right? 97 00:04:58.930 --> 00:05:02.070 And then we would not need this age_num there, 98 00:05:02.070 --> 00:05:03.740 and we could do this. 99 00:05:03.740 --> 00:05:06.580 But, how do we get the dynamic value 100 00:05:06.580 --> 00:05:10.320 instead of the static 35 string? 101 00:05:10.320 --> 00:05:13.393 We use the input function that we had just a moment ago. 102 00:05:14.820 --> 00:05:16.440 So you can do this in Python 103 00:05:17.630 --> 00:05:22.100 because the things inside the brackets always run first. 104 00:05:22.100 --> 00:05:25.290 So, the input function will run, 105 00:05:25.290 --> 00:05:28.550 and the programme will wait until the user presses Enter. 106 00:05:28.550 --> 00:05:32.200 And then, this entire selected text will be replaced 107 00:05:32.200 --> 00:05:33.630 by what the user enters. 108 00:05:33.630 --> 00:05:36.120 For example, the string three. 109 00:05:36.120 --> 00:05:39.500 And then we will pass that to the int function, 110 00:05:39.500 --> 00:05:42.280 which will give us an integer equivalent 111 00:05:42.280 --> 00:05:44.490 to the string that we got passed. 112 00:05:44.490 --> 00:05:46.730 So we will get the integer three. 113 00:05:46.730 --> 00:05:49.570 And then we will put that inside age. 114 00:05:49.570 --> 00:05:53.790 This type of nesting is very common 115 00:05:53.790 --> 00:05:56.690 and you will be doing this quite often. 116 00:05:56.690 --> 00:05:59.900 However, it's also easy to go a bit overboard with it. 117 00:05:59.900 --> 00:06:01.340 You don't wanna be doing that. 118 00:06:01.340 --> 00:06:03.770 You don't want to nest like 20 times, 119 00:06:03.770 --> 00:06:06.000 otherwise the code gets almost unreadable. 120 00:06:06.000 --> 00:06:08.500 But a couple of functions inside each other 121 00:06:08.500 --> 00:06:09.790 can actually shorten your code 122 00:06:09.790 --> 00:06:12.740 and make it a bit easier to read. 123 00:06:12.740 --> 00:06:16.390 Remember what we had before is we have an age variable. 124 00:06:16.390 --> 00:06:19.820 And then we also have an age_num variable. 125 00:06:19.820 --> 00:06:24.020 But, the age variable was only useful 126 00:06:24.020 --> 00:06:25.700 to calculate age_num. 127 00:06:25.700 --> 00:06:28.180 It wasn't used anywhere else in our programme. 128 00:06:28.180 --> 00:06:30.850 So it's kinda pointless to keep it around. 129 00:06:30.850 --> 00:06:32.690 We might as well nest these here 130 00:06:32.690 --> 00:06:36.350 and only have one variable that we actually want and use, 131 00:06:36.350 --> 00:06:40.010 and not have two variables, one of which is pointless. 132 00:06:40.010 --> 00:06:43.200 Now instead of doing the multiplication inside the f-string, 133 00:06:43.200 --> 00:06:47.980 which you can do, we can separate this into a new variable. 134 00:06:47.980 --> 00:06:49.583 Why might you do that? 135 00:06:50.470 --> 00:06:53.850 Well the main reason for separating a value like that 136 00:06:53.850 --> 00:06:55.540 into a variable is so that you can 137 00:06:55.540 --> 00:06:58.570 give it a name that has meaning. 138 00:06:58.570 --> 00:07:01.120 Now when you read this programme, it's pretty obvious 139 00:07:01.120 --> 00:07:03.170 that first you're getting the user's age, 140 00:07:03.170 --> 00:07:05.950 then you're calculating how many months that equals to, 141 00:07:05.950 --> 00:07:07.940 and then you're printing that out. 142 00:07:07.940 --> 00:07:08.960 And it's quite obvious 143 00:07:08.960 --> 00:07:11.690 because your variable is called months. 144 00:07:11.690 --> 00:07:13.690 If your variable was called x, 145 00:07:13.690 --> 00:07:15.590 then it might not be so obvious 146 00:07:15.590 --> 00:07:17.960 until you read through the entire thing. 147 00:07:17.960 --> 00:07:21.470 Giving variables good names, good descriptive names, 148 00:07:21.470 --> 00:07:24.520 is a bit of an art form and it can be quite tricky at times, 149 00:07:24.520 --> 00:07:25.800 but it's really important. 150 00:07:25.800 --> 00:07:28.210 You will get better at it over time. 151 00:07:28.210 --> 00:07:30.730 In the next lecture, we've got a small exercise for you, 152 00:07:30.730 --> 00:07:33.630 which is to, instead of printing the number of months 153 00:07:33.630 --> 00:07:36.910 the user has lived for, you can print the number of seconds 154 00:07:36.910 --> 00:07:38.410 they have lived for. 155 00:07:38.410 --> 00:07:40.270 Just slightly different maths here, 156 00:07:40.270 --> 00:07:42.090 but more or less the same. 157 00:07:42.090 --> 00:07:44.390 Do try to complete that exercise on your own 158 00:07:44.390 --> 00:07:46.110 without looking back at this code, 159 00:07:46.110 --> 00:07:47.900 and if you've forgotten how to do it, 160 00:07:47.900 --> 00:07:49.600 then do look back of course 161 00:07:49.600 --> 00:07:51.440 so that you can complete the exercise. 162 00:07:51.440 --> 00:07:53.950 Through practise, and through typing code, 163 00:07:53.950 --> 00:07:55.790 you're going to learn much faster 164 00:07:55.790 --> 00:07:57.653 than just by watching the videos. 165 00:07:58.510 --> 00:07:59.820 That's everything for this one though. 166 00:07:59.820 --> 00:08:02.120 Thank you for joining me, and I'll see you in the next one. 167 00:08:02.120 --> 00:08:03.520 line:15% Good luck with the exercise.