1 00:00:00,570 --> 00:00:01,680 Instructor: Welcome back. 2 00:00:01,680 --> 00:00:04,710 So, up until now, we've learned about integers, 3 00:00:04,710 --> 00:00:08,790 floats, strings and Boolean values. 4 00:00:08,790 --> 00:00:12,600 So let's try and tie some of those things together. 5 00:00:12,600 --> 00:00:15,360 Let's imagine your Facebook. 6 00:00:15,360 --> 00:00:17,340 How do you think Facebook works 7 00:00:17,340 --> 00:00:20,613 underneath the hood when it comes to your profile? 8 00:00:21,630 --> 00:00:24,990 Well, they would have, let's say, a variable 9 00:00:24,990 --> 00:00:29,140 called name and this will have your name 10 00:00:30,390 --> 00:00:33,543 and then they'll probably have a variable with your age. 11 00:00:34,470 --> 00:00:35,580 Let's say 50. 12 00:00:35,580 --> 00:00:37,740 I'm not actually 50, but I'm pretending to be 13 00:00:37,740 --> 00:00:40,173 for this case just so I sound wiser. 14 00:00:41,040 --> 00:00:45,390 And then let's say relationship status. 15 00:00:45,390 --> 00:00:50,070 So again, we also have relationship status on Facebook 16 00:00:50,070 --> 00:00:52,743 and for now, let's say single. 17 00:00:53,610 --> 00:00:57,210 So that's my Facebook profile right there. 18 00:00:57,210 --> 00:00:59,910 Now, if we wanted to perhaps change that, 19 00:00:59,910 --> 00:01:02,880 let's say my relationship status all of a sudden goes 20 00:01:02,880 --> 00:01:05,550 from single to it's complicated, 21 00:01:05,550 --> 00:01:07,530 well, that's an easy fix. 22 00:01:07,530 --> 00:01:11,043 We would just do relationship status equals two. 23 00:01:12,750 --> 00:01:15,450 It's complicated. 24 00:01:15,450 --> 00:01:18,930 And remember, because of the single quote, 25 00:01:18,930 --> 00:01:20,770 we'll have to do back slash 26 00:01:24,360 --> 00:01:26,853 it's complicated. 27 00:01:27,780 --> 00:01:30,663 And now if I print my relationship status, 28 00:01:31,860 --> 00:01:35,340 you'll see that it's been updated to it's complicated. 29 00:01:35,340 --> 00:01:37,473 I am no longer single, hooray. 30 00:01:38,670 --> 00:01:40,950 But this is pretty simple. 31 00:01:40,950 --> 00:01:42,840 Let's do something more complicated, 32 00:01:42,840 --> 00:01:45,240 and this is going to be a fun exercise. 33 00:01:45,240 --> 00:01:48,090 Let's create a program 34 00:01:48,090 --> 00:01:50,223 that guesses your age. 35 00:01:51,210 --> 00:01:55,200 Let's use the input method 36 00:01:55,200 --> 00:01:56,850 that we've seen before. 37 00:01:56,850 --> 00:01:59,650 Remember, the input method allows us to type something 38 00:02:01,080 --> 00:02:04,710 in here and receive it as input, 39 00:02:04,710 --> 00:02:06,600 so we can assign that to a variable. 40 00:02:06,600 --> 00:02:11,600 So, let's say this variable is going to be birth year. 41 00:02:13,110 --> 00:02:16,110 And that's going to equal the input. 42 00:02:16,110 --> 00:02:21,110 That's going to ask when or what year 43 00:02:21,510 --> 00:02:26,073 were you born. 44 00:02:28,590 --> 00:02:32,460 Now from here, I want you to pause the video 45 00:02:32,460 --> 00:02:34,710 and try to solve this yourself. 46 00:02:34,710 --> 00:02:38,820 How can you make it, so that at the end of this program, 47 00:02:38,820 --> 00:02:41,340 you're going to get asked what year were you born? 48 00:02:41,340 --> 00:02:42,930 You're going to type in your year 49 00:02:42,930 --> 00:02:44,850 and then it's going to print out 50 00:02:44,850 --> 00:02:48,780 your age is, whatever the age is. 51 00:02:48,780 --> 00:02:52,230 So pause the video, try and solve this on your own. 52 00:02:52,230 --> 00:02:54,630 Mind you, there is a bit of a trick in here, 53 00:02:54,630 --> 00:02:58,050 so if you get stuck, try to Google around 54 00:02:58,050 --> 00:03:01,590 and figure out what the issue may be. 55 00:03:01,590 --> 00:03:04,470 Again, part of being a programmer is trying 56 00:03:04,470 --> 00:03:07,443 to solve these problems that you don't know the answers to. 57 00:03:09,480 --> 00:03:11,493 All right, let me show you my solution. 58 00:03:12,540 --> 00:03:14,610 We have our birth year here 59 00:03:14,610 --> 00:03:16,610 that we're going to store in a variable. 60 00:03:18,000 --> 00:03:20,160 And now, we're going to calculate the age. 61 00:03:20,160 --> 00:03:23,880 I'm going to say age equals, 62 00:03:23,880 --> 00:03:28,683 well, it's going to equal the current year, let's say 2019. 63 00:03:29,520 --> 00:03:34,520 And then 2019 here is going to get subtracted 64 00:03:34,770 --> 00:03:39,690 from whatever the birth year that you entered. 65 00:03:39,690 --> 00:03:42,003 So let's enter that, 66 00:03:43,200 --> 00:03:48,123 and then we're simply going to say print your age is, 67 00:03:49,110 --> 00:03:51,960 and we want to use an F string here. 68 00:03:51,960 --> 00:03:54,483 So I'm going to add an F here, 69 00:03:56,370 --> 00:04:00,813 and then simply say age. 70 00:04:02,730 --> 00:04:06,300 If I run this, I get, 71 00:04:06,300 --> 00:04:08,010 what year were you born? 72 00:04:08,010 --> 00:04:09,723 I was born in 1986. 73 00:04:12,150 --> 00:04:16,769 Hmm, we get an error, and if you try this yourself 74 00:04:16,769 --> 00:04:19,200 you may have encountered this error. 75 00:04:19,200 --> 00:04:23,220 We get something called a type error, 76 00:04:23,220 --> 00:04:26,550 and there's lots of errors that you can get in Python. 77 00:04:26,550 --> 00:04:29,580 Again, we have a section on error, so don't stress too much 78 00:04:29,580 --> 00:04:31,590 about it, but if you read here it says 79 00:04:31,590 --> 00:04:35,640 unsupported operand type S 80 00:04:35,640 --> 00:04:39,363 for minus int and string. 81 00:04:40,560 --> 00:04:44,220 Hmm, let's try and debug this. 82 00:04:44,220 --> 00:04:46,740 It looks like we're using minus 83 00:04:46,740 --> 00:04:49,530 on an integer and a string. 84 00:04:49,530 --> 00:04:52,120 So let me print out here 85 00:04:53,070 --> 00:04:57,720 the type of birth year. 86 00:04:57,720 --> 00:05:02,223 If I run this, and let's enter 1986. 87 00:05:03,240 --> 00:05:07,770 I get class string, and this is a little gotcha 88 00:05:07,770 --> 00:05:10,150 because what input does 89 00:05:11,010 --> 00:05:13,170 is it asks you for an input, 90 00:05:13,170 --> 00:05:16,680 but that input that I wrote, 1986, 91 00:05:16,680 --> 00:05:21,030 well, that actually gets converted to a string 92 00:05:21,030 --> 00:05:22,743 and assigned to birth year. 93 00:05:23,580 --> 00:05:27,730 So what that means is that we're trying to subtract 94 00:05:28,890 --> 00:05:32,403 a number or a string from a number. 95 00:05:33,510 --> 00:05:35,403 So how do we solve this issue? 96 00:05:36,570 --> 00:05:39,900 Well, you'd have to turn this into an integer 97 00:05:39,900 --> 00:05:44,310 and this is something that we saw previously. 98 00:05:44,310 --> 00:05:47,520 What we need to do is to convert this 99 00:05:47,520 --> 00:05:52,293 into an integer with using the int function. 100 00:05:53,280 --> 00:05:55,890 So let's try that. 101 00:05:55,890 --> 00:05:59,283 If I remove this and click run, 102 00:06:00,540 --> 00:06:05,540 I'll say 1986, and look at that. 103 00:06:05,640 --> 00:06:07,713 Your age is 33. 104 00:06:08,790 --> 00:06:12,420 We've created a program that was able to tell your age 105 00:06:12,420 --> 00:06:14,790 based on the year that you were born. 106 00:06:14,790 --> 00:06:17,550 Now, this may have been tricky to you 107 00:06:17,550 --> 00:06:21,330 but it teaches an important concept in programming. 108 00:06:21,330 --> 00:06:24,060 That is sometimes you're storing data 109 00:06:24,060 --> 00:06:27,720 into different data types, and sometimes 110 00:06:27,720 --> 00:06:31,383 you need those different data types to interact together. 111 00:06:32,280 --> 00:06:35,460 So you'll see a lot of problems where you have 112 00:06:35,460 --> 00:06:38,100 to convert a data type, and this is a very 113 00:06:38,100 --> 00:06:41,850 common one where you take an input that's a string 114 00:06:41,850 --> 00:06:45,123 and then you convert it to something like an integer. 115 00:06:46,080 --> 00:06:49,440 So you can perform a mathematical operation. 116 00:06:49,440 --> 00:06:52,860 And that's why, if you remember, we gave you a list 117 00:06:52,860 --> 00:06:56,640 of all our data types in Python, the fundamental data types 118 00:06:56,640 --> 00:06:58,770 and all of them were blue because, well, 119 00:06:58,770 --> 00:07:01,860 each one of these are actions, are functions 120 00:07:01,860 --> 00:07:04,260 that we can use. 121 00:07:04,260 --> 00:07:07,770 For example, I converted the birth year 122 00:07:07,770 --> 00:07:11,910 into integer but I could have also converted this 123 00:07:11,910 --> 00:07:14,700 to a float, for example. 124 00:07:14,700 --> 00:07:16,710 And if I run this, it would still work 125 00:07:16,710 --> 00:07:19,860 because Python knows that, 126 00:07:19,860 --> 00:07:21,930 well, we can use integers 127 00:07:21,930 --> 00:07:24,330 and floats in mathematical operation, 128 00:07:24,330 --> 00:07:28,350 but you can see here that it's converted into a float now. 129 00:07:28,350 --> 00:07:33,350 If we convert this into a Boolean and I click run. 130 00:07:36,420 --> 00:07:39,090 Your age is 2018. 131 00:07:39,090 --> 00:07:40,950 That's really confusing, right? 132 00:07:40,950 --> 00:07:44,040 Well, that's because this gets turned 133 00:07:44,040 --> 00:07:47,280 into true and underneath the hood, 134 00:07:47,280 --> 00:07:49,380 Python does something weird where 135 00:07:49,380 --> 00:07:53,010 a true value is converted to one. 136 00:07:53,010 --> 00:07:56,190 Because Boolean, remember, it's one or zero. 137 00:07:56,190 --> 00:07:57,960 True or false. 138 00:07:57,960 --> 00:08:00,780 All right, hopefully this didn't confuse you too much. 139 00:08:00,780 --> 00:08:02,940 It's something that we'll encounter throughout the course 140 00:08:02,940 --> 00:08:06,090 so don't worry, we'll get more and more practice. 141 00:08:06,090 --> 00:08:08,670 However, I hope you get to practice this a little bit 142 00:08:08,670 --> 00:08:10,650 to really understand what's going on 143 00:08:10,650 --> 00:08:14,400 because although this is a couple of lines of code, 144 00:08:14,400 --> 00:08:16,140 the principles that we use here, 145 00:08:16,140 --> 00:08:19,293 we're going to use throughout bigger and bigger code bases. 146 00:08:20,190 --> 00:08:21,823 So I guess I'll see you in the next one. 147 00:08:21,823 --> 00:08:22,656 Bye-bye.