1 00:00:00,000 --> 00:00:01,744 2 00:00:01,744 --> 00:00:03,170 SPEAKER 1: Well, hello world! 3 00:00:03,170 --> 00:00:05,490 These are CS50's New Year's seminars. 4 00:00:05,490 --> 00:00:09,300 So first and foremost, a very happy 2021 to everyone. 5 00:00:09,300 --> 00:00:11,252 We hope this finds everyone healthy and well. 6 00:00:11,252 --> 00:00:13,460 This is the very first time that we've done something 7 00:00:13,460 --> 00:00:14,720 like this on the internet. 8 00:00:14,720 --> 00:00:18,530 I teach this course called CS50, which is Harvard University's introduction 9 00:00:18,530 --> 00:00:20,990 to the intellectual enterprises of computer science 10 00:00:20,990 --> 00:00:22,330 in the arts of programming. 11 00:00:22,330 --> 00:00:25,580 And this, as well as a number of other courses taught by some of my colleagues 12 00:00:25,580 --> 00:00:28,047 at Harvard, are freely available on edX and beyond. 13 00:00:28,047 --> 00:00:31,130 So odds are you've discovered at least one of those classes at some point, 14 00:00:31,130 --> 00:00:34,670 or you're here with a friend or family member or colleague, in which case, 15 00:00:34,670 --> 00:00:36,860 welcome to CS50's community. 16 00:00:36,860 --> 00:00:41,240 The goal of these seminars is to give everyone truly a taste of CS50, 17 00:00:41,240 --> 00:00:44,360 and a taste of a number of topics, today's being Python. 18 00:00:44,360 --> 00:00:47,330 And so in a bit, I'll introduce you to today's teacher, 19 00:00:47,330 --> 00:00:49,650 my dear colleague at Harvard, Brian Yu. 20 00:00:49,650 --> 00:00:56,320 So for this first seminar on Python, and for our subsequent seminars on SQL 21 00:00:56,320 --> 00:00:59,270 for databases and for Linux on computing, 22 00:00:59,270 --> 00:01:01,520 and ultimately for game development, we're pleased 23 00:01:01,520 --> 00:01:05,209 and we're thrilled to say that we've had over 10,000 registrants for these 24 00:01:05,209 --> 00:01:06,590 and the upcoming seminars. 25 00:01:06,590 --> 00:01:11,540 And more amazingly too, we've had to more than 500 volunteers join us today 26 00:01:11,540 --> 00:01:13,730 and in the coming days as mentors, who you'll 27 00:01:13,730 --> 00:01:18,590 see some of in the chat, some of on ed, the online Q&A platform 28 00:01:18,590 --> 00:01:20,070 that we'll use as well. 29 00:01:20,070 --> 00:01:22,370 And let me emphasize here, if you are completely 30 00:01:22,370 --> 00:01:26,600 new to Python, to programming, to CS50, to Zoom, 31 00:01:26,600 --> 00:01:30,420 certainly do not worry if at any point you fall a little bit behind today. 32 00:01:30,420 --> 00:01:32,750 If you have any trouble following along, we'll 33 00:01:32,750 --> 00:01:36,197 tend to try to keep as many people on the same page as we can. 34 00:01:36,197 --> 00:01:38,030 But as you might recall from the real world, 35 00:01:38,030 --> 00:01:40,790 it's hard enough to keep 20 students in a physical classroom 36 00:01:40,790 --> 00:01:43,070 perhaps on the same page, let alone 1,000. 37 00:01:43,070 --> 00:01:45,380 So that's why we have this other platform, ed, 38 00:01:45,380 --> 00:01:49,100 so that you can ask questions at your own pace, either during the seminar 39 00:01:49,100 --> 00:01:50,090 or after. 40 00:01:50,090 --> 00:01:53,270 The chat window will likely get a bit out of control with so many people 41 00:01:53,270 --> 00:01:57,380 commenting and asking questions here, so please do feel welcome to tune 42 00:01:57,380 --> 00:02:01,740 in via ed after today's seminar, in order to ask your questions as well, 43 00:02:01,740 --> 00:02:05,570 and get more comfortable ultimately with the world of computer science. 44 00:02:05,570 --> 00:02:08,440 Without further ado, allow me to introduce my colleague, 45 00:02:08,440 --> 00:02:13,880 CS50's own Brian Yu, who teaches not only CS50's Introduction to Web 46 00:02:13,880 --> 00:02:17,480 Programming, but also CS50's Introduction to Artificial Intelligence 47 00:02:17,480 --> 00:02:20,960 here at Harvard, which also is available online. 48 00:02:20,960 --> 00:02:23,827 CS50's own Brian Yu. 49 00:02:23,827 --> 00:02:25,160 BRIAN YU: Thanks so much, David. 50 00:02:25,160 --> 00:02:26,827 It's so good to see everyone here today. 51 00:02:26,827 --> 00:02:30,272 Welcome everyone to the very first of CS50's New Year's seminars. 52 00:02:30,272 --> 00:02:33,230 As David mentioned today, we'll be introducing the programming language 53 00:02:33,230 --> 00:02:33,942 Python. 54 00:02:33,942 --> 00:02:36,650 So if you've never programmed before, this will be an opportunity 55 00:02:36,650 --> 00:02:39,440 to see what it's all about and to get your hands, 56 00:02:39,440 --> 00:02:42,860 get some experimenting with writing your very first programs. 57 00:02:42,860 --> 00:02:46,670 So while I'm presenting, definitely feel free to ask any questions in the Zoom 58 00:02:46,670 --> 00:02:47,178 chat. 59 00:02:47,178 --> 00:02:49,220 If you go down to the bottom of your Zoom window, 60 00:02:49,220 --> 00:02:51,110 you'll probably see a button that says Chat, 61 00:02:51,110 --> 00:02:53,360 which you can click to say hello to other classmates 62 00:02:53,360 --> 00:02:54,830 and ask questions if you have any. 63 00:02:54,830 --> 00:02:56,810 We have a fantastic group of mentors that 64 00:02:56,810 --> 00:02:58,602 are here in the meeting with us today, that 65 00:02:58,602 --> 00:03:00,690 are here to help answer questions too. 66 00:03:00,690 --> 00:03:02,412 And in addition to that, we also have ed, 67 00:03:02,412 --> 00:03:04,370 which David mentioned is another platform where 68 00:03:04,370 --> 00:03:05,495 you can ask your questions. 69 00:03:05,495 --> 00:03:08,790 And I'll go ahead and paste that link into the chat now. 70 00:03:08,790 --> 00:03:12,390 So feel free to go there in order to ask questions as well. 71 00:03:12,390 --> 00:03:15,800 And the staff can definitely be around to help you there, too. 72 00:03:15,800 --> 00:03:18,350 Before we get started, as was mentioned in the email 73 00:03:18,350 --> 00:03:21,600 but if you haven't already, be sure to sign up for a GitHub account 74 00:03:21,600 --> 00:03:22,850 if you don't already have one. 75 00:03:22,850 --> 00:03:25,310 You'll use your GitHub account as a means of signing 76 00:03:25,310 --> 00:03:27,170 into some of the course's tools. 77 00:03:27,170 --> 00:03:29,160 And if you go on to take CS50 after the fact, 78 00:03:29,160 --> 00:03:31,440 you'll do a little more work with GitHub itself. 79 00:03:31,440 --> 00:03:35,270 So you can go to GitHub.com/join, the link to which I just pasted 80 00:03:35,270 --> 00:03:38,720 into the chat, to join GitHub if you don't already have an account. 81 00:03:38,720 --> 00:03:42,470 And we're going to be spending most of today inside of an IDE, 82 00:03:42,470 --> 00:03:46,250 or Integrated Development Environment, called CS50 IDE. 83 00:03:46,250 --> 00:03:48,290 And all that means is it's a tool that lives 84 00:03:48,290 --> 00:03:51,320 in the cloud, which will be a place where you can write and run 85 00:03:51,320 --> 00:03:52,960 Python programs on the internet. 86 00:03:52,960 --> 00:03:54,920 And so we'll get an opportunity to explore 87 00:03:54,920 --> 00:03:56,745 that if you've never used it before. 88 00:03:56,745 --> 00:03:59,120 But what you can do is go to this URL here that I've just 89 00:03:59,120 --> 00:04:03,702 pasted into the chat, ide.cs50.io. 90 00:04:03,702 --> 00:04:06,410 And once you go there, you can sign in with your GitHub username. 91 00:04:06,410 --> 00:04:08,810 And in a moment, I'll introduce that platform to you, 92 00:04:08,810 --> 00:04:12,590 show you how to use it, and we'll go about writing our very first Python 93 00:04:12,590 --> 00:04:14,060 programs. 94 00:04:14,060 --> 00:04:19,269 And so with that, let's go ahead and dive into a taste of Python. 95 00:04:19,269 --> 00:04:22,990 So in a moment, once you go to ide.cs50.io 96 00:04:22,990 --> 00:04:26,115 and sign in with your GitHub username, you 97 00:04:26,115 --> 00:04:28,240 should ultimately end up seeing an environment that 98 00:04:28,240 --> 00:04:29,540 looks something like this. 99 00:04:29,540 --> 00:04:33,590 This is CS50 IDE, which is a cloud-based platform where you can write and run 100 00:04:33,590 --> 00:04:34,090 code. 101 00:04:34,090 --> 00:04:35,965 And that's what we're going to be using today 102 00:04:35,965 --> 00:04:39,430 in order to write programs in this programming language called Python. 103 00:04:39,430 --> 00:04:42,700 Now, it's possible to run Python programs just on your own computer 104 00:04:42,700 --> 00:04:44,530 without using CS50 IDE. 105 00:04:44,530 --> 00:04:47,740 But today, with so many students, to make sure everyone's on the same page, 106 00:04:47,740 --> 00:04:51,610 we're using CS50 IDE to make sure everyone's in a consistent environment, 107 00:04:51,610 --> 00:04:55,070 to make sure everyone's seeing the same thing at the same time. 108 00:04:55,070 --> 00:05:00,130 And so over here, on the left hand side of CS50 IDE, this is our file browser. 109 00:05:00,130 --> 00:05:04,270 Any of the files that we create and add to our IDE, they will appear here. 110 00:05:04,270 --> 00:05:08,257 Right now, if you've never used CS50 IDE before, this is probably empty. 111 00:05:08,257 --> 00:05:10,840 But over time today, we're going to be adding some more files, 112 00:05:10,840 --> 00:05:14,140 and you'll be adding some more files to this yourself, too. 113 00:05:14,140 --> 00:05:18,400 Up in this top panel of CS50 IDE is the text editor. 114 00:05:18,400 --> 00:05:22,120 This is where we're actually going to be writing our Python code. 115 00:05:22,120 --> 00:05:25,570 And then down below is what we call the terminal window. 116 00:05:25,570 --> 00:05:28,600 This is a place where we're going to be writing text-based commands, 117 00:05:28,600 --> 00:05:30,880 telling our IDE what we want it to do. 118 00:05:30,880 --> 00:05:34,940 So ultimately, we're going to be writing code in this top section here. 119 00:05:34,940 --> 00:05:38,710 And then down below, we're going to write commands in order to actually run 120 00:05:38,710 --> 00:05:42,020 that Python code as well. 121 00:05:42,020 --> 00:05:43,900 And so I see that a couple people, if you're 122 00:05:43,900 --> 00:05:46,108 finding that the IDE is taking a bit of time to load, 123 00:05:46,108 --> 00:05:48,580 just give it some time to load until you see this screen. 124 00:05:48,580 --> 00:05:50,413 But certainly, if you're having any trouble, 125 00:05:50,413 --> 00:05:52,750 feel free to make a post on ed, or post in the chat. 126 00:05:52,750 --> 00:05:56,560 And the mentors can definitely help out with whatever 127 00:05:56,560 --> 00:05:58,150 challenges you might be having. 128 00:05:58,150 --> 00:06:02,900 And otherwise, you can feel free to follow along with what I'm doing here. 129 00:06:02,900 --> 00:06:05,920 And so to start writing our very first Python program, 130 00:06:05,920 --> 00:06:10,850 I'm going to go up to the File menu in CS50 IDE, and click on New File. 131 00:06:10,850 --> 00:06:13,585 We're just going to create a new empty text file. 132 00:06:13,585 --> 00:06:15,460 And this text file is going to be where we're 133 00:06:15,460 --> 00:06:20,337 going to start by writing our very first Python program. 134 00:06:20,337 --> 00:06:22,670 And the simplest of Python programs we're going to write 135 00:06:22,670 --> 00:06:24,790 is going to look something like this. 136 00:06:24,790 --> 00:06:28,192 I can write print, and then ("hello, world"). 137 00:06:28,192 --> 00:06:30,150 And I'm going to actually save this file first. 138 00:06:30,150 --> 00:06:32,060 I'm going to go to File, Save. 139 00:06:32,060 --> 00:06:36,200 And I'm going to save this in a file called hello.py. 140 00:06:36,200 --> 00:06:37,200 That py. 141 00:06:37,200 --> 00:06:39,020 extension is just a convention. 142 00:06:39,020 --> 00:06:42,560 When you're writing programs in Python, they'll very often end with .py. 143 00:06:42,560 --> 00:06:46,670 That way we and the computer will know that it is in fact a Python program. 144 00:06:46,670 --> 00:06:50,120 So I'm going to name this file hello.py. 145 00:06:50,120 --> 00:06:52,590 And I'm going to click on Save. 146 00:06:52,590 --> 00:06:54,810 And this, again, is my very first Python program. 147 00:06:54,810 --> 00:06:56,447 It is one line of code. 148 00:06:56,447 --> 00:06:58,530 And the way to decipher this, the way to read this 149 00:06:58,530 --> 00:07:01,800 as you might imagine, this program is going to print, in other words 150 00:07:01,800 --> 00:07:04,980 display to the screen, the words hello, world. 151 00:07:04,980 --> 00:07:07,290 And the way this works is I've written this word print. 152 00:07:07,290 --> 00:07:10,500 This is what we in the programming world would call a function. 153 00:07:10,500 --> 00:07:14,250 It is some code that is going to run, that is going to perform some task, 154 00:07:14,250 --> 00:07:18,330 and the task that this function print performs, is that it prints or displays 155 00:07:18,330 --> 00:07:21,480 and some text to the screen. 156 00:07:21,480 --> 00:07:24,990 And in between these parentheses that come after the word print, 157 00:07:24,990 --> 00:07:27,120 comes the input to the function. 158 00:07:27,120 --> 00:07:30,890 Functions can take input, some information that the functions use. 159 00:07:30,890 --> 00:07:32,640 And right now the input that I'm providing 160 00:07:32,640 --> 00:07:35,970 to the print function inside of these quotation marks 161 00:07:35,970 --> 00:07:37,800 are the words hello, world. 162 00:07:37,800 --> 00:07:40,590 These are the words that I want to be displayed 163 00:07:40,590 --> 00:07:45,240 to the screen when I run this program. 164 00:07:45,240 --> 00:07:47,730 And so once I've written this program, I write it 165 00:07:47,730 --> 00:07:50,970 in this top section inside of this file called hello.py 166 00:07:50,970 --> 00:07:54,480 I need to be sure to save the file, if you haven't already, by going to File 167 00:07:54,480 --> 00:07:56,190 and then Save. 168 00:07:56,190 --> 00:07:58,420 Now, I can actually run this program. 169 00:07:58,420 --> 00:08:02,010 And the way that you run a program is by going into the terminal window, which 170 00:08:02,010 --> 00:08:05,010 is in this bottom half of CS50 IDE. 171 00:08:05,010 --> 00:08:07,860 And here I can write commands that my computer, 172 00:08:07,860 --> 00:08:10,740 that my CS50 IDE is going to follow. 173 00:08:10,740 --> 00:08:13,050 And the command I'm going to use here is a command 174 00:08:13,050 --> 00:08:16,800 called python, which is a command that will run a Python program. 175 00:08:16,800 --> 00:08:21,540 And then after the word python, I'm going to specify the name of the file 176 00:08:21,540 --> 00:08:23,070 that I want to run. 177 00:08:23,070 --> 00:08:26,190 And you might recall that the file that I opened from before 178 00:08:26,190 --> 00:08:28,140 is a file called hello.py. 179 00:08:28,140 --> 00:08:31,230 This is the file where I've been writing this code so far. 180 00:08:31,230 --> 00:08:33,990 And so I'm going to write Python, then a space, 181 00:08:33,990 --> 00:08:39,090 and then hello.py as the name of the file that I would like to run. 182 00:08:39,090 --> 00:08:40,860 And it's going to be a Python program. 183 00:08:40,860 --> 00:08:45,330 And once I've written that command, python space hello.py, 184 00:08:45,330 --> 00:08:48,900 I can just press the Return button to run this program. 185 00:08:48,900 --> 00:08:52,020 And what you'll see, if you've been following what I've done, 186 00:08:52,020 --> 00:08:56,140 is you'll see the words hello, world printed to the screen. 187 00:08:56,140 --> 00:08:59,310 And so I'll give you all a moment to try and copy what I've done here. 188 00:08:59,310 --> 00:09:03,810 All I've done so far is go to ide.cs50.io. 189 00:09:03,810 --> 00:09:07,530 I've created a new file by going to File, New File, 190 00:09:07,530 --> 00:09:10,530 inside of which I've just written one line of code-- 191 00:09:10,530 --> 00:09:15,300 print, and then in parentheses the words hello, world in quotation marks. 192 00:09:15,300 --> 00:09:18,900 I've saved that in a file called hello.py. 193 00:09:18,900 --> 00:09:21,360 And then down here in the terminal window, 194 00:09:21,360 --> 00:09:25,770 I've written the command python hello.py to run that Python program. 195 00:09:25,770 --> 00:09:28,590 And that is going to cause the IDE to run my Python code, 196 00:09:28,590 --> 00:09:31,320 reading it one line at a time, top to bottom. 197 00:09:31,320 --> 00:09:34,290 Right now, there's just one line of code inside of my program. 198 00:09:34,290 --> 00:09:36,870 And that one line of code prints the words 199 00:09:36,870 --> 00:09:39,900 hello, world to my terminal screen. 200 00:09:39,900 --> 00:09:43,890 201 00:09:43,890 --> 00:09:47,876 All right, I'll give everyone a moment to try that for themselves. 202 00:09:47,876 --> 00:09:56,790 203 00:09:56,790 --> 00:09:57,290 All right. 204 00:09:57,290 --> 00:10:01,077 So, let's try to do something a little bit more interesting now. 205 00:10:01,077 --> 00:10:03,410 We've seen now one line of code, one line of Python code 206 00:10:03,410 --> 00:10:05,810 that uses this function called print. 207 00:10:05,810 --> 00:10:07,970 And print is a function that's built into Python, 208 00:10:07,970 --> 00:10:09,340 it's part of Python itself. 209 00:10:09,340 --> 00:10:11,090 And so once you start using Python, you'll 210 00:10:11,090 --> 00:10:14,750 be able to use this print function that was written by the developers of Python 211 00:10:14,750 --> 00:10:17,690 that we can use to display information to the screen. 212 00:10:17,690 --> 00:10:20,740 But there are other functions that are part of Python as well 213 00:10:20,740 --> 00:10:23,630 that we can use inside of our Python programs. 214 00:10:23,630 --> 00:10:26,900 And another function is called the input function. 215 00:10:26,900 --> 00:10:29,000 And the input function is a function that 216 00:10:29,000 --> 00:10:31,580 asks the user, whoever is running your program, 217 00:10:31,580 --> 00:10:35,280 to type in some input that they're going to provide to the program. 218 00:10:35,280 --> 00:10:37,530 And so we'll see a look at that in a moment, too. 219 00:10:37,530 --> 00:10:39,680 I see a question asked about whether you need 220 00:10:39,680 --> 00:10:41,510 a semicolon at the end of the line. 221 00:10:41,510 --> 00:10:43,342 Turns out in Python, that's not necessary. 222 00:10:43,342 --> 00:10:45,050 If you've programmed a little bit before, 223 00:10:45,050 --> 00:10:48,178 maybe seen languages like C or Java for example, 224 00:10:48,178 --> 00:10:50,720 you might have put semicolons at the end of all of your lines 225 00:10:50,720 --> 00:10:52,520 that are printing things out, for example. 226 00:10:52,520 --> 00:10:54,270 In Python, we don't need those semicolons. 227 00:10:54,270 --> 00:10:57,620 In Python here, we just have to say the name of the function print, 228 00:10:57,620 --> 00:10:59,830 and then in parentheses hello, world. 229 00:10:59,830 --> 00:11:03,660 But that's an excellent question, so thank you for asking that. 230 00:11:03,660 --> 00:11:05,810 So let me try now creating another program that's 231 00:11:05,810 --> 00:11:09,080 going to use this other function called the input function, 232 00:11:09,080 --> 00:11:13,200 so that we can see how that input function works, as well. 233 00:11:13,200 --> 00:11:17,020 So I'll go ahead and go to the File menu and create another new file. 234 00:11:17,020 --> 00:11:20,450 And this file I'm going to save by going to File, Save. 235 00:11:20,450 --> 00:11:22,285 I'm going to call this file greet.py. 236 00:11:22,285 --> 00:11:25,660 237 00:11:25,660 --> 00:11:27,470 And you'll see why in a moment. 238 00:11:27,470 --> 00:11:30,430 But inside of this new program greet.py, I'm 239 00:11:30,430 --> 00:11:33,430 going to, instead of using the print function, which would print out 240 00:11:33,430 --> 00:11:36,010 some text to the screen, which we've already seen, 241 00:11:36,010 --> 00:11:38,680 we're going to use the input function. 242 00:11:38,680 --> 00:11:41,800 And here I'm going to have a prompt, prompt 243 00:11:41,800 --> 00:11:44,130 asking the user to type something in as input. 244 00:11:44,130 --> 00:11:47,005 And the prompt I'm going to use is something like ("What's your name? 245 00:11:47,005 --> 00:11:47,910 ") your name? 246 00:11:47,910 --> 00:11:49,660 And then I'll include a space in a moment, 247 00:11:49,660 --> 00:11:51,580 for reasons you'll see in just a second. 248 00:11:51,580 --> 00:11:53,930 And again, this text could be anything you want. 249 00:11:53,930 --> 00:11:57,400 You can write whatever text you want in between those quotation marks. 250 00:11:57,400 --> 00:11:59,530 In Python, and in programming more general, 251 00:11:59,530 --> 00:12:02,750 any text inside of quotation marks is called a string. 252 00:12:02,750 --> 00:12:05,590 A string is just a fancy word for some text, like words 253 00:12:05,590 --> 00:12:07,420 or sentences or paragraphs. 254 00:12:07,420 --> 00:12:10,360 And here this string, which is in quotation marks, 255 00:12:10,360 --> 00:12:13,000 is just asking a question, what's your name? 256 00:12:13,000 --> 00:12:15,400 And because it's in the input function, that 257 00:12:15,400 --> 00:12:18,440 is going to prompt the user to type in some input. 258 00:12:18,440 --> 00:12:22,910 In this case, I'm prompting the user to type in their name, for example. 259 00:12:22,910 --> 00:12:25,160 And I'm seeing a couple of people in the chat that are 260 00:12:25,160 --> 00:12:27,770 having some trouble loading CS50 IDE. 261 00:12:27,770 --> 00:12:30,380 If you are having trouble loading CS50 IDE, 262 00:12:30,380 --> 00:12:33,560 my recommendation would be to first make sure you're using the latest 263 00:12:33,560 --> 00:12:35,767 version of Google Chrome or Firefox. 264 00:12:35,767 --> 00:12:38,600 If you're using a different browser or using an older version of one 265 00:12:38,600 --> 00:12:40,808 of those browsers, you'll probably have the best luck 266 00:12:40,808 --> 00:12:44,600 with CS50 IDE by using the latest version of Chrome or Firefox. 267 00:12:44,600 --> 00:12:47,780 And the other thing you'll want to check is make sure that any ad blockers 268 00:12:47,780 --> 00:12:50,900 or other browser extensions, if you have those turned on, 269 00:12:50,900 --> 00:12:53,870 be sure to disable those for using CS50 IDE, 270 00:12:53,870 --> 00:12:57,870 just to maximize the chances that you'll be able to get in without any problems. 271 00:12:57,870 --> 00:13:00,530 And if you're still having issues, try reloading the IDE 272 00:13:00,530 --> 00:13:02,980 or asking a question in the chat. 273 00:13:02,980 --> 00:13:06,470 And the staff and the mentors will be around to try to help you with that, as 274 00:13:06,470 --> 00:13:07,950 well if you're struggling. 275 00:13:07,950 --> 00:13:09,742 But if you are having problems, you're also 276 00:13:09,742 --> 00:13:12,650 welcome to just follow along with what I'm writing in my IDE. 277 00:13:12,650 --> 00:13:15,960 And then you can try it in your own IDE after the fact, if you would like to. 278 00:13:15,960 --> 00:13:18,440 So, if you are having trouble with the IDE, 279 00:13:18,440 --> 00:13:22,080 those might be good suggestions for trying to fix that. 280 00:13:22,080 --> 00:13:26,110 But let's turn our attention now back to this program greet.py, 281 00:13:26,110 --> 00:13:29,900 where I'm writing this function input, asking the question, what's your name, 282 00:13:29,900 --> 00:13:31,250 and let's now try this. 283 00:13:31,250 --> 00:13:32,718 This program's not done yet. 284 00:13:32,718 --> 00:13:34,760 There's more that I want to do with this program. 285 00:13:34,760 --> 00:13:37,610 But one strategy that I use when writing programs is not 286 00:13:37,610 --> 00:13:40,430 to try writing many, many lines all at once and then run it, 287 00:13:40,430 --> 00:13:42,468 but to build my programs a little bit at a time. 288 00:13:42,468 --> 00:13:44,510 And in a moment, I'll give you all an opportunity 289 00:13:44,510 --> 00:13:46,332 to try writing some programs of your own. 290 00:13:46,332 --> 00:13:48,290 And I'd recommend following that same strategy. 291 00:13:48,290 --> 00:13:52,620 Write everything a little bit at a time, and try it as you go. 292 00:13:52,620 --> 00:13:55,580 And so here, I'm going to, in my terminal window, 293 00:13:55,580 --> 00:13:59,720 write python, and then the new file that I would like to run, which in this case 294 00:13:59,720 --> 00:14:00,920 is greet.py. 295 00:14:00,920 --> 00:14:03,540 That was the name of the file that I just created. 296 00:14:03,540 --> 00:14:09,170 And when I run python greet.py, I see printed to the screen What's your name? 297 00:14:09,170 --> 00:14:13,530 And notice that my cursor is now right next to What's your name after a space. 298 00:14:13,530 --> 00:14:17,450 This is why I included the space up here on this very first line of my program. 299 00:14:17,450 --> 00:14:18,920 And my program is now paused. 300 00:14:18,920 --> 00:14:21,770 It's waiting for me, the user of this program, 301 00:14:21,770 --> 00:14:24,110 to provide some input to the program. 302 00:14:24,110 --> 00:14:26,438 So it's asking me what's your name? 303 00:14:26,438 --> 00:14:27,980 So I'll go ahead and type in my name. 304 00:14:27,980 --> 00:14:30,105 And you can do the same thing, typing in your name. 305 00:14:30,105 --> 00:14:32,570 I'll type in Brian down here at the terminal. 306 00:14:32,570 --> 00:14:34,610 And then I'll just go ahead and press Return 307 00:14:34,610 --> 00:14:37,370 to send that input to the program. 308 00:14:37,370 --> 00:14:42,300 And at that point, what you'll notice is that this dollar sign appears again. 309 00:14:42,300 --> 00:14:44,660 This dollar sign is just called the prompt. 310 00:14:44,660 --> 00:14:48,350 The prompt is telling you, you can type in another command to the program now. 311 00:14:48,350 --> 00:14:51,590 And usually when you see the prompt after running a Python program, 312 00:14:51,590 --> 00:14:53,180 that means the Python program is over. 313 00:14:53,180 --> 00:14:55,940 You've run the Python program, it's run to completion, 314 00:14:55,940 --> 00:14:58,340 and the prompt means the program is now done. 315 00:14:58,340 --> 00:15:00,230 And you can now run another program if you 316 00:15:00,230 --> 00:15:03,470 would like to, by running Python followed by whatever 317 00:15:03,470 --> 00:15:06,690 file name you would like to run. 318 00:15:06,690 --> 00:15:11,240 And so this greet.py program now is asking the user to type in some input. 319 00:15:11,240 --> 00:15:12,978 And we can type in some input. 320 00:15:12,978 --> 00:15:15,020 But we're not yet doing anything with that input. 321 00:15:15,020 --> 00:15:17,780 I would like for my program to be able to do something like, 322 00:15:17,780 --> 00:15:21,830 when I say my name is Brian, maybe my program can respond to me and say, 323 00:15:21,830 --> 00:15:26,330 hello, Brian, for example, using whatever it is that I typed in as input 324 00:15:26,330 --> 00:15:29,598 and using that later inside of the program. 325 00:15:29,598 --> 00:15:31,640 And so here, we're going to introduce a new idea. 326 00:15:31,640 --> 00:15:34,300 We're going to introduce the notion of a variable. 327 00:15:34,300 --> 00:15:37,790 A variable in Python is just going to be someplace 328 00:15:37,790 --> 00:15:39,760 where we can store information. 329 00:15:39,760 --> 00:15:41,510 Oftentimes, our programs are going to want 330 00:15:41,510 --> 00:15:45,590 to store information that they're going to use later inside of that program. 331 00:15:45,590 --> 00:15:47,840 You might imagine if you're writing a program that's 332 00:15:47,840 --> 00:15:50,090 going to be like a calculator that can do math, 333 00:15:50,090 --> 00:15:52,610 you might want variables to keep track of what numbers 334 00:15:52,610 --> 00:15:54,780 you're adding or multiplying or subtracting. 335 00:15:54,780 --> 00:15:58,430 And in this program, where we are asking the user for their name, 336 00:15:58,430 --> 00:16:00,620 we probably want some variable where we're 337 00:16:00,620 --> 00:16:03,320 going to store whatever it is the user typed in, 338 00:16:03,320 --> 00:16:06,470 whatever name the user provided as input. 339 00:16:06,470 --> 00:16:08,720 So how do we create a variable in Python? 340 00:16:08,720 --> 00:16:11,090 Well, the first thing we need to do is decide on what 341 00:16:11,090 --> 00:16:12,800 we're going to call that variable. 342 00:16:12,800 --> 00:16:15,410 And we can choose to call the variable whatever we want. 343 00:16:15,410 --> 00:16:18,410 But it's usually helpful to give it a descriptive name, some name that's 344 00:16:18,410 --> 00:16:20,160 going to be meaningful or something you'll 345 00:16:20,160 --> 00:16:22,640 remember later on in your program. 346 00:16:22,640 --> 00:16:24,950 And so because I want to use this variable 347 00:16:24,950 --> 00:16:28,010 to store the name of whoever is using my program, 348 00:16:28,010 --> 00:16:30,760 I'm just going to call this variable name. 349 00:16:30,760 --> 00:16:36,080 And so what I'll change in line one is I'm going to say name equals, 350 00:16:36,080 --> 00:16:38,940 and then this input line that I had before. 351 00:16:38,940 --> 00:16:43,290 So all I've done is add name equals to the beginning of this line. 352 00:16:43,290 --> 00:16:45,890 And so now what this line of code is saying is 353 00:16:45,890 --> 00:16:48,023 I'm going to ask the user for some input, 354 00:16:48,023 --> 00:16:50,690 prompting them to type in their name by asking what's your name, 355 00:16:50,690 --> 00:16:53,960 and then waiting for the user to type in their name. 356 00:16:53,960 --> 00:16:56,570 And then this equals is what in Python we call assignment. 357 00:16:56,570 --> 00:16:58,850 It means we are going to assign whatever was typed 358 00:16:58,850 --> 00:17:02,420 in by the user to this variable name. 359 00:17:02,420 --> 00:17:05,240 We're going to take whatever the user typed in and store it 360 00:17:05,240 --> 00:17:08,839 inside of this variable that we are here calling name. 361 00:17:08,839 --> 00:17:12,160 362 00:17:12,160 --> 00:17:15,270 I see a question, do we need to initialize the variable? 363 00:17:15,270 --> 00:17:17,910 And so if you're familiar with other programming languages, 364 00:17:17,910 --> 00:17:21,030 not to worry if you haven't programmed in other languages before, 365 00:17:21,030 --> 00:17:24,990 you might be familiar with having to initially say what type of variable 366 00:17:24,990 --> 00:17:26,160 any variable name is. 367 00:17:26,160 --> 00:17:27,760 In Python, you don't have to do that. 368 00:17:27,760 --> 00:17:30,510 The only thing you need to do to create a new variable 369 00:17:30,510 --> 00:17:33,900 is have the name of the variable, choose whatever name you want to give it, 370 00:17:33,900 --> 00:17:38,730 then an equal sign saying this is what I want this variable to be equal to. 371 00:17:38,730 --> 00:17:41,100 And then what do I want it to be equal to? 372 00:17:41,100 --> 00:17:43,500 Well, I want it to be whatever comes back when 373 00:17:43,500 --> 00:17:47,880 I ask the user to type in their name. 374 00:17:47,880 --> 00:17:51,270 And so now, I can use that variable, that variable 375 00:17:51,270 --> 00:17:54,050 called name, inside of my program. 376 00:17:54,050 --> 00:17:55,840 And so what I'm going to do is on line 2, 377 00:17:55,840 --> 00:17:59,110 we now have a program that has multiple lines of code, and that's OK. 378 00:17:59,110 --> 00:18:00,902 What's going to happen is that our computer 379 00:18:00,902 --> 00:18:03,810 is going to read these lines of code one after another in sequence. 380 00:18:03,810 --> 00:18:08,500 Let's go ahead and print hello and then a comma. 381 00:18:08,500 --> 00:18:12,810 And now let me, on another line for now, let me print (name). 382 00:18:12,810 --> 00:18:15,120 And notice, name here is not in quotation marks, 383 00:18:15,120 --> 00:18:19,350 because I don't want to literally print out the characters N-A-M-E. 384 00:18:19,350 --> 00:18:23,610 But I instead want to print out whatever the contents of this variable name is. 385 00:18:23,610 --> 00:18:26,760 Whatever is stored inside of that variable, 386 00:18:26,760 --> 00:18:29,910 that is what I want to have printed to the terminal. 387 00:18:29,910 --> 00:18:34,710 388 00:18:34,710 --> 00:18:35,580 All right. 389 00:18:35,580 --> 00:18:40,797 So what I can do now is go ahead and run python greet.py again. 390 00:18:40,797 --> 00:18:42,880 I see that it prompts me again for what's my name. 391 00:18:42,880 --> 00:18:46,100 I can type in my name, Brian. 392 00:18:46,100 --> 00:18:49,360 And now I see printed on line 2, I see hello comma. 393 00:18:49,360 --> 00:18:51,965 And then an after that, I see printed the name Brian. 394 00:18:51,965 --> 00:18:55,090 And I see a couple of other people that are saying they were having trouble 395 00:18:55,090 --> 00:18:56,260 accessing the IDE. 396 00:18:56,260 --> 00:19:00,072 If you are you could also try going to sandbox.cs50.io, 397 00:19:00,072 --> 00:19:02,530 which has been suggested by a couple of people in the chat. 398 00:19:02,530 --> 00:19:05,380 That will open up a very similar programming environment to the one 399 00:19:05,380 --> 00:19:06,160 I see here. 400 00:19:06,160 --> 00:19:08,260 It's not going to be exactly the same. 401 00:19:08,260 --> 00:19:09,970 But you'll notice many similar elements. 402 00:19:09,970 --> 00:19:13,330 You'll notice that you'll have a file browser, where you can have some files. 403 00:19:13,330 --> 00:19:16,960 You'll notice a place, where you can type your code inside of a file text 404 00:19:16,960 --> 00:19:17,620 editor. 405 00:19:17,620 --> 00:19:20,020 And you'll also notice a terminal window down below, 406 00:19:20,020 --> 00:19:21,760 where you can run these commands. 407 00:19:21,760 --> 00:19:24,040 Ultimately, it should behave in exactly the same way. 408 00:19:24,040 --> 00:19:26,320 But it just might look a little bit different to what 409 00:19:26,320 --> 00:19:29,080 it is that I'm seeing here, but certainly OK to be 410 00:19:29,080 --> 00:19:34,600 using sandbox.cs50.io if you would like, in order to follow along here as well. 411 00:19:34,600 --> 00:19:37,960 412 00:19:37,960 --> 00:19:38,460 All right. 413 00:19:38,460 --> 00:19:39,180 So I can try this again. 414 00:19:39,180 --> 00:19:40,680 And I can try typing in a different name. 415 00:19:40,680 --> 00:19:42,780 And you'll notice that whatever name I type in, 416 00:19:42,780 --> 00:19:44,557 that gets stored inside of the variable. 417 00:19:44,557 --> 00:19:46,390 And that's going to get printed out instead. 418 00:19:46,390 --> 00:19:49,580 So if I write python greet.py, and instead say What's your name. 419 00:19:49,580 --> 00:19:52,110 Let's try a different name, let's try David. 420 00:19:52,110 --> 00:19:53,790 Then I see hello comma. 421 00:19:53,790 --> 00:19:56,340 And then on the next line I see David. 422 00:19:56,340 --> 00:20:01,200 And so one thing I'm wondering as I'm writing this program is, why is there 423 00:20:01,200 --> 00:20:05,520 a new line in between hello comma and David? 424 00:20:05,520 --> 00:20:07,710 And that's probably not what I would expect. 425 00:20:07,710 --> 00:20:10,020 Like, if I was talking to a person and they were saying hello to me, 426 00:20:10,020 --> 00:20:11,853 they'd probably just include the whole thing 427 00:20:11,853 --> 00:20:14,260 on the same line, just hello comma, and then a space, 428 00:20:14,260 --> 00:20:16,440 and then the name instead of a brand new line. 429 00:20:16,440 --> 00:20:17,940 And so why might that be? 430 00:20:17,940 --> 00:20:21,270 Anyone have a guess as to what might be causing this? 431 00:20:21,270 --> 00:20:23,020 432 00:20:23,020 --> 00:20:26,380 And I see a comment from Andrea, who put in the chat 433 00:20:26,380 --> 00:20:29,710 that Python is just automatically adding a new line for us. 434 00:20:29,710 --> 00:20:33,035 And this is something that Python is trying to do in order to be helpful. 435 00:20:33,035 --> 00:20:36,160 And Python just says that normally if you're trying to print something out, 436 00:20:36,160 --> 00:20:39,730 like I'm printing the word hello, then at the end of printing out 437 00:20:39,730 --> 00:20:41,800 whatever that happens to be, we're just going 438 00:20:41,800 --> 00:20:44,230 to automatically break to a new line. 439 00:20:44,230 --> 00:20:47,680 And so we're saying hello, then a new line, and then David. 440 00:20:47,680 --> 00:20:49,280 So how do I fix that problem? 441 00:20:49,280 --> 00:20:52,330 How do I make it so that it's all on one line instead? 442 00:20:52,330 --> 00:20:55,090 I have a couple of options for how I could do this. 443 00:20:55,090 --> 00:20:57,400 But what I'm going to do now is, if Python 444 00:20:57,400 --> 00:21:02,050 is adding a new line at the end of every time I call this print function, 445 00:21:02,050 --> 00:21:04,540 every time I use the print function, well then, 446 00:21:04,540 --> 00:21:06,910 I'm just going to use the print function once. 447 00:21:06,910 --> 00:21:10,720 It turns out that the print function can take multiple inputs. 448 00:21:10,720 --> 00:21:14,440 I don't just have to provide one thing for the print function to print. 449 00:21:14,440 --> 00:21:17,860 I can provide multiple things for the function to print. 450 00:21:17,860 --> 00:21:19,660 So I could say print hello. 451 00:21:19,660 --> 00:21:24,130 And then after this quotation mark, after the end of the hello string, 452 00:21:24,130 --> 00:21:26,170 I'm going to add a comma. 453 00:21:26,170 --> 00:21:31,130 And then I'm going to write name, which is the name of that variable. 454 00:21:31,130 --> 00:21:35,350 And so now, what I have happening here is 455 00:21:35,350 --> 00:21:41,230 that I now have a line of code that's asking the user to type in their name. 456 00:21:41,230 --> 00:21:44,440 We're storing that inside of this variable called name, 457 00:21:44,440 --> 00:21:47,140 and then I'm printing hello comma. 458 00:21:47,140 --> 00:21:52,145 And then after the comma, we're printing out that name as well. 459 00:21:52,145 --> 00:21:54,520 And so again, there were multiple ways you could do this. 460 00:21:54,520 --> 00:21:57,103 I see a couple people suggesting other ways you could do that. 461 00:21:57,103 --> 00:21:58,600 And that's totally fine, too. 462 00:21:58,600 --> 00:22:01,090 Definitely multiple ways you can solve the same problem. 463 00:22:01,090 --> 00:22:02,810 And that's true of programming more generally, 464 00:22:02,810 --> 00:22:05,352 that when you have something that you're trying to write code 465 00:22:05,352 --> 00:22:08,080 to solve a problem, there is not usually going to be just one 466 00:22:08,080 --> 00:22:09,450 way to solve that problem. 467 00:22:09,450 --> 00:22:11,860 There are going to be multiple ways to solve the problem. 468 00:22:11,860 --> 00:22:14,470 And you can think about which way makes more sense to you, 469 00:22:14,470 --> 00:22:16,610 or which way you think is going to be better. 470 00:22:16,610 --> 00:22:20,290 And so today we'll have an opportunity to explore a couple of different ways 471 00:22:20,290 --> 00:22:21,970 that you could solve that problem. 472 00:22:21,970 --> 00:22:23,890 I see a lot of people suggesting that you 473 00:22:23,890 --> 00:22:28,720 can use plus to combine two different pieces of information in Python. 474 00:22:28,720 --> 00:22:32,980 So I could say hello plus name, and so that might work OK too. 475 00:22:32,980 --> 00:22:36,910 So a couple of different options for how you can accomplish things like this. 476 00:22:36,910 --> 00:22:40,220 But let's try this again now to see how it worked. 477 00:22:40,220 --> 00:22:42,768 I'm going to write python greet.py. 478 00:22:42,768 --> 00:22:44,560 It's going to prompt me to type in my name. 479 00:22:44,560 --> 00:22:45,580 What's my name? 480 00:22:45,580 --> 00:22:47,290 I'll type in Brian. 481 00:22:47,290 --> 00:22:47,920 Press Return. 482 00:22:47,920 --> 00:22:53,530 And now just on one line it says hello Brian, which is what I expect 483 00:22:53,530 --> 00:22:56,150 and what I would want it to do. 484 00:22:56,150 --> 00:22:58,150 So I'll pause here to give people an opportunity 485 00:22:58,150 --> 00:23:02,150 to try to follow along or ask questions if people have any. 486 00:23:02,150 --> 00:23:06,430 I see [? Manvir ?] is asking, do I need to write python greet.py every time 487 00:23:06,430 --> 00:23:07,970 you want to run the code? 488 00:23:07,970 --> 00:23:08,900 And the answer is yes. 489 00:23:08,900 --> 00:23:11,983 You need to write that command python greet.py in order to run it. 490 00:23:11,983 --> 00:23:13,900 Though there is a little bit of a trick, which 491 00:23:13,900 --> 00:23:17,320 is that if you are running the same program multiple times, like here 492 00:23:17,320 --> 00:23:21,160 I'm writing python greet.py, then I've run it again a couple of times, 493 00:23:21,160 --> 00:23:23,260 the trick is that if you're in your terminal 494 00:23:23,260 --> 00:23:26,650 you can usually press the up arrow key on your keyboard, 495 00:23:26,650 --> 00:23:29,230 and that will go to the most recent command 496 00:23:29,230 --> 00:23:30,920 that you have run in the terminal. 497 00:23:30,920 --> 00:23:35,090 So if you just ran python greet.py and you want to run it again, 498 00:23:35,090 --> 00:23:38,240 you can go into your terminal, press the up arrow key, 499 00:23:38,240 --> 00:23:40,890 and then you can run python greet.py. 500 00:23:40,890 --> 00:23:42,800 Again I can try a different name. 501 00:23:42,800 --> 00:23:45,290 For example, I can try hello, David. 502 00:23:45,290 --> 00:23:47,860 And it says hello, David now as well. 503 00:23:47,860 --> 00:23:50,760 504 00:23:50,760 --> 00:23:54,210 How do you open the terminal in the IDE, I see someone is asking. 505 00:23:54,210 --> 00:23:57,872 It should be open for you by default already inside of CS50 IDE. 506 00:23:57,872 --> 00:24:00,330 But if you've closed it by accident for some reason and you 507 00:24:00,330 --> 00:24:02,880 need to get access to a new terminal, not a problem. 508 00:24:02,880 --> 00:24:06,398 You should see a green plus button inside of CS50 IDE. 509 00:24:06,398 --> 00:24:08,190 And if you click on that button, there will 510 00:24:08,190 --> 00:24:10,210 be a button that says a new terminal. 511 00:24:10,210 --> 00:24:13,260 And if you click on that option, you'll get a new terminal window where 512 00:24:13,260 --> 00:24:17,443 you can run your code there as well. 513 00:24:17,443 --> 00:24:20,110 So that is how you could create a new terminal if you wanted to. 514 00:24:20,110 --> 00:24:22,670 515 00:24:22,670 --> 00:24:23,170 All right. 516 00:24:23,170 --> 00:24:27,850 So now that we have the ability to have variables, storing information, 517 00:24:27,850 --> 00:24:29,350 I want to try something out. 518 00:24:29,350 --> 00:24:31,610 I want to try something a little more interesting. 519 00:24:31,610 --> 00:24:33,970 Maybe one of the simplest things I can think of to try 520 00:24:33,970 --> 00:24:35,560 is to try to build a calculator. 521 00:24:35,560 --> 00:24:38,620 Try and build a program that can do some math, like take two numbers 522 00:24:38,620 --> 00:24:41,008 and add them together the way that a calculator could. 523 00:24:41,008 --> 00:24:42,550 So I'm going to try and do just that. 524 00:24:42,550 --> 00:24:47,440 I'm going to write a program that is going to accept two numbers as input, 525 00:24:47,440 --> 00:24:49,120 maybe 1 and 2. 526 00:24:49,120 --> 00:24:51,700 And it's going to add those two numbers together for me 527 00:24:51,700 --> 00:24:54,453 and print out what the result is. 528 00:24:54,453 --> 00:24:56,870 I see a couple of people, before I get to that, asking me, 529 00:24:56,870 --> 00:24:59,170 how do I clear the terminal when I'm done with it, 530 00:24:59,170 --> 00:25:00,920 when I want to make it clean again? 531 00:25:00,920 --> 00:25:04,720 The simplest way to do it is in your terminal, just type the word clear 532 00:25:04,720 --> 00:25:05,830 and press Return. 533 00:25:05,830 --> 00:25:10,847 And that'll clear out the terminal for you if you would like to do that. 534 00:25:10,847 --> 00:25:12,680 So I'm going to create a new Python program, 535 00:25:12,680 --> 00:25:14,513 and feel free to follow along if you'd like. 536 00:25:14,513 --> 00:25:15,900 I'll create a new file. 537 00:25:15,900 --> 00:25:19,340 I'm going to save it as arithmetic.py, because I'm 538 00:25:19,340 --> 00:25:23,510 going to be doing some arithmetic, doing some addition. 539 00:25:23,510 --> 00:25:26,870 And what I want to do is, I want to ask the user 540 00:25:26,870 --> 00:25:29,000 to provide us input two numbers. 541 00:25:29,000 --> 00:25:32,600 And maybe I'll call them x and y as conventions from algebra. 542 00:25:32,600 --> 00:25:33,720 And how might I do that? 543 00:25:33,720 --> 00:25:36,680 Well, I can do input, and then I provide the prompt, 544 00:25:36,680 --> 00:25:38,090 what do I want them to type in. 545 00:25:38,090 --> 00:25:39,290 We'll type in x. 546 00:25:39,290 --> 00:25:43,130 So just write the letter x and then a colon to signal to the user, here's 547 00:25:43,130 --> 00:25:47,000 where I want you to type in the first number, the number x. 548 00:25:47,000 --> 00:25:49,983 And I'll add another line, input y, to mean 549 00:25:49,983 --> 00:25:52,400 all right, now I want the user to type in a second number, 550 00:25:52,400 --> 00:25:53,817 to type in the number y. 551 00:25:53,817 --> 00:25:55,400 So I have two lines of code right now. 552 00:25:55,400 --> 00:25:59,988 Each line is asking the user to type in some input, and let's try it out. 553 00:25:59,988 --> 00:26:02,030 Of course, the program's not done yet, but I just 554 00:26:02,030 --> 00:26:05,480 want to test this to make sure that it's working. 555 00:26:05,480 --> 00:26:08,570 I'll run python arithmetic.py. 556 00:26:08,570 --> 00:26:09,740 It asks me to type an x. 557 00:26:09,740 --> 00:26:11,960 I can type in a number, maybe the number 1. 558 00:26:11,960 --> 00:26:15,110 Then y, I'll type in another number, the number 2. 559 00:26:15,110 --> 00:26:16,670 And the program's over. 560 00:26:16,670 --> 00:26:20,285 I see this prompt again that tells me the program is done. 561 00:26:20,285 --> 00:26:22,160 And of course, my program didn't do anything. 562 00:26:22,160 --> 00:26:24,168 It didn't actually add the numbers together, 563 00:26:24,168 --> 00:26:26,960 because I haven't told the program to add the numbers together yet. 564 00:26:26,960 --> 00:26:29,390 And my Python program will only do what I 565 00:26:29,390 --> 00:26:33,080 tell it to do by writing some programming instructions. 566 00:26:33,080 --> 00:26:36,920 So how would I write a program now that is going to accept two numbers 567 00:26:36,920 --> 00:26:40,280 and print out what the sum of those two numbers is? 568 00:26:40,280 --> 00:26:42,770 Well, the first thing that I'll probably want to do 569 00:26:42,770 --> 00:26:46,562 is store whatever came in as input inside of variables. 570 00:26:46,562 --> 00:26:48,270 And this is a common idea in programming. 571 00:26:48,270 --> 00:26:51,170 Anytime you want to be keeping track of information, 572 00:26:51,170 --> 00:26:55,050 you'll often want a variable to keep track of that information. 573 00:26:55,050 --> 00:26:57,450 And so I'll call this variable x. 574 00:26:57,450 --> 00:27:01,280 So I'll just say x equals whatever that input is. 575 00:27:01,280 --> 00:27:04,520 And then on line 2, I'll add a y equals. 576 00:27:04,520 --> 00:27:07,760 So I now have two variables, x and y. 577 00:27:07,760 --> 00:27:11,660 x is going to be equal to whatever the user typed in on that first line. 578 00:27:11,660 --> 00:27:14,000 Then we're going to prompt the user for another number. 579 00:27:14,000 --> 00:27:17,480 And we're going to save that inside of y. 580 00:27:17,480 --> 00:27:21,230 And then now on this third line, let me try something out. 581 00:27:21,230 --> 00:27:23,150 Let me print (x + y). 582 00:27:23,150 --> 00:27:26,160 583 00:27:26,160 --> 00:27:30,360 So what I would like for this program to do is ask the user for two numbers, 584 00:27:30,360 --> 00:27:35,520 for a number x and a number y, and then print out what is x plus y. 585 00:27:35,520 --> 00:27:36,840 So let's try it out. 586 00:27:36,840 --> 00:27:39,870 Let's try python arithmetic.py. 587 00:27:39,870 --> 00:27:42,030 I'll try typing the number 1. 588 00:27:42,030 --> 00:27:44,458 And then I'm going to type the number 2 as x and y. 589 00:27:44,458 --> 00:27:47,250 And anyone have a guess what's going to happen when I press Return, 590 00:27:47,250 --> 00:27:49,110 when I submit this as input? 591 00:27:49,110 --> 00:27:51,900 What am I going to see? 592 00:27:51,900 --> 00:27:55,290 All right, I see some people saying 3 and some people saying 12. 593 00:27:55,290 --> 00:27:56,460 Yeah, I would hope it's 3. 594 00:27:56,460 --> 00:28:00,210 I want the answer to be 3, because that's what 1 plus 2 is. 595 00:28:00,210 --> 00:28:03,130 But I do for some reason get 12. 596 00:28:03,130 --> 00:28:05,130 Yeah, I see a lot of people in the chat that are 597 00:28:05,130 --> 00:28:07,090 suggesting that that was the answer. 598 00:28:07,090 --> 00:28:08,370 So you predicted it. 599 00:28:08,370 --> 00:28:11,408 And this is maybe the first example of a bug in programming. 600 00:28:11,408 --> 00:28:13,450 And once you start programming, inevitably you're 601 00:28:13,450 --> 00:28:14,408 going to run into bugs. 602 00:28:14,408 --> 00:28:16,380 But that's OK, totally normal. 603 00:28:16,380 --> 00:28:19,560 Whenever you encounter a bug, it's time to do some debugging, trying 604 00:28:19,560 --> 00:28:22,890 to figure out what is it that went wrong when I was writing this program, 605 00:28:22,890 --> 00:28:24,300 and how could I fix it. 606 00:28:24,300 --> 00:28:28,840 What is it that I could change to get this program to work? 607 00:28:28,840 --> 00:28:30,250 So why am I getting this problem? 608 00:28:30,250 --> 00:28:32,270 What should I fix? 609 00:28:32,270 --> 00:28:36,470 All right, yeah, I see a bunch of people saying that I'm dealing with strings. 610 00:28:36,470 --> 00:28:39,920 Remember that I mentioned earlier that a string is just some text. 611 00:28:39,920 --> 00:28:40,640 And that's true. 612 00:28:40,640 --> 00:28:43,550 By default when I ask the user to type in some input, 613 00:28:43,550 --> 00:28:46,160 Python is going to treat that input as a string. 614 00:28:46,160 --> 00:28:48,500 It's just text as characters. 615 00:28:48,500 --> 00:28:52,530 And so I'm storing the character 1 and then the character 2. 616 00:28:52,530 --> 00:28:56,540 And as far as Python is concerned, to add two sets of characters together, 617 00:28:56,540 --> 00:29:00,620 to combine two strings, we're just going to take the first set of characters, 618 00:29:00,620 --> 00:29:04,110 the number 1, and then the number 2 and just mash them together. 619 00:29:04,110 --> 00:29:08,870 I've got 1 and then 2, so it's going to print out 1, 2. 620 00:29:08,870 --> 00:29:12,680 So that's maybe not quite what I wanted for it to do. 621 00:29:12,680 --> 00:29:18,550 So is there a solution, something that I can do to solve this problem? 622 00:29:18,550 --> 00:29:20,710 All right, and I see a couple of people suggesting 623 00:29:20,710 --> 00:29:25,600 that I need to somehow convert these strings, this text, into an integer. 624 00:29:25,600 --> 00:29:28,600 And a couple of people that maybe have programmed in Python before know 625 00:29:28,600 --> 00:29:31,600 that there is another function called int 626 00:29:31,600 --> 00:29:34,420 that will take something like a string and convert it 627 00:29:34,420 --> 00:29:37,510 into an int, otherwise known as an integer, which in this case 628 00:29:37,510 --> 00:29:40,030 is going to be a number, like 1, 2, 3, 4, 5, 629 00:29:40,030 --> 00:29:43,180 or maybe a negative number or maybe 0, but just some whole number 630 00:29:43,180 --> 00:29:46,840 or negative number or 0, that I can then manipulate, I can add together, 631 00:29:46,840 --> 00:29:48,120 for example. 632 00:29:48,120 --> 00:29:49,760 And so how do I do that? 633 00:29:49,760 --> 00:29:55,390 Well, I'm going to on line 1, say int, and then an open parenthesis, 634 00:29:55,390 --> 00:29:57,340 and the whole of this input line is going 635 00:29:57,340 --> 00:30:00,080 to go inside of those parentheses. 636 00:30:00,080 --> 00:30:03,040 So the way to read that is that I'm asking for input. 637 00:30:03,040 --> 00:30:06,340 I'm asking the user to type in some value x. 638 00:30:06,340 --> 00:30:11,290 And that itself is the input to another function called int. 639 00:30:11,290 --> 00:30:13,510 And remember the input to that function goes inside 640 00:30:13,510 --> 00:30:18,550 of the parentheses, which is why that value is inside of those parentheses. 641 00:30:18,550 --> 00:30:21,550 And so this might look a little weird if you've never programmed before, 642 00:30:21,550 --> 00:30:24,460 but what I am now doing is asking for input, 643 00:30:24,460 --> 00:30:28,510 converting that input into an integer, and then storing 644 00:30:28,510 --> 00:30:33,835 that input inside of this variable called x. 645 00:30:33,835 --> 00:30:35,460 And now, I'll do the same thing with y. 646 00:30:35,460 --> 00:30:40,590 I want to take that and convert it into an integer as well. 647 00:30:40,590 --> 00:30:43,400 And now, both x and y are going to be integers. 648 00:30:43,400 --> 00:30:44,570 They're going to be numbers. 649 00:30:44,570 --> 00:30:46,810 Int is short for integer, for [INAUDIBLE],, who 650 00:30:46,810 --> 00:30:49,063 I see is asking about that in the chat. 651 00:30:49,063 --> 00:30:51,230 And then I'm going to add those two values together, 652 00:30:51,230 --> 00:30:54,890 x plus y adding together two integers. 653 00:30:54,890 --> 00:30:56,520 And now, let's give the program a try. 654 00:30:56,520 --> 00:30:59,250 Let's see what happens. 655 00:30:59,250 --> 00:31:02,460 I can try 1 and then 2. 656 00:31:02,460 --> 00:31:07,180 And I do see the result that I expect, which in this case is the number 3. 657 00:31:07,180 --> 00:31:09,403 658 00:31:09,403 --> 00:31:11,820 All right, so I've been able to add two integers together. 659 00:31:11,820 --> 00:31:15,380 And I've been able to resolve the bug that I had before, 660 00:31:15,380 --> 00:31:17,540 where the numbers weren't adding in the way 661 00:31:17,540 --> 00:31:20,248 that I expected for those two numbers to be added. 662 00:31:20,248 --> 00:31:22,790 So I'll pause here to give you an opportunity to try that out 663 00:31:22,790 --> 00:31:26,000 and to ask questions if you have any, questions 664 00:31:26,000 --> 00:31:28,430 about what it is that we've done here. 665 00:31:28,430 --> 00:31:33,370 666 00:31:33,370 --> 00:31:34,370 Oh I see a question. 667 00:31:34,370 --> 00:31:36,740 Can you use single quotes instead of double quotes? 668 00:31:36,740 --> 00:31:40,300 So here around my strings, I'm using these double quotation marks. 669 00:31:40,300 --> 00:31:42,520 It is OK to use single quotation marks, too. 670 00:31:42,520 --> 00:31:44,170 Python does allow you to do that. 671 00:31:44,170 --> 00:31:47,950 I'm going to be using double quotation marks for today just to be consistent, 672 00:31:47,950 --> 00:31:50,000 but it is a matter of personal preference, 673 00:31:50,000 --> 00:31:52,150 and you can choose which one you'd like. 674 00:31:52,150 --> 00:31:57,120 675 00:31:57,120 --> 00:31:58,080 I see another question. 676 00:31:58,080 --> 00:32:00,870 Are these spaces necessary around the equal sign? 677 00:32:00,870 --> 00:32:02,670 No, the spaces are not necessary. 678 00:32:02,670 --> 00:32:05,430 But it's often good style to include those spaces there. 679 00:32:05,430 --> 00:32:08,460 And so style refers to how your code looks. 680 00:32:08,460 --> 00:32:12,240 The computer doesn't really care whether there are spaces around things or not. 681 00:32:12,240 --> 00:32:15,030 But it's often going to be easier for the human, someone reading 682 00:32:15,030 --> 00:32:18,630 your code, to read if your code is well organized, if there's appropriate 683 00:32:18,630 --> 00:32:20,080 spacing, for example. 684 00:32:20,080 --> 00:32:23,943 And so you'll see me often add spacing, not because the computer requires it, 685 00:32:23,943 --> 00:32:25,860 but just because it's helpful to someone who's 686 00:32:25,860 --> 00:32:30,877 reading the code to make it a little bit cleaner as well. 687 00:32:30,877 --> 00:32:32,210 All right, so that was addition. 688 00:32:32,210 --> 00:32:35,990 But Python also allows other arithmetic operators, so to speak, 689 00:32:35,990 --> 00:32:38,300 other operations that I can do on numbers. 690 00:32:38,300 --> 00:32:41,790 I could use minus to do subtraction, for example. 691 00:32:41,790 --> 00:32:45,200 So I could try this out by going to python arithmetic.py. 692 00:32:45,200 --> 00:32:49,490 If I take 5 minus 1, for example, I see the answer is 4. 693 00:32:49,490 --> 00:32:51,290 That's what I would expect it to be. 694 00:32:51,290 --> 00:32:55,220 To do multiplication, you're going to use the asterisk key, which 695 00:32:55,220 --> 00:32:57,630 on many keyboards is above the 8. 696 00:32:57,630 --> 00:33:01,090 So x multiplied by y, via the asterisk. 697 00:33:01,090 --> 00:33:02,510 And so I can try this again. 698 00:33:02,510 --> 00:33:05,400 I'll maybe multiply 7 times 4, for example, 699 00:33:05,400 --> 00:33:09,200 get the number 28 as a result of multiplying those numbers together 700 00:33:09,200 --> 00:33:10,410 as well. 701 00:33:10,410 --> 00:33:13,240 And for division, I can use a slash. 702 00:33:13,240 --> 00:33:16,960 The slash is how I can divide one number by another. 703 00:33:16,960 --> 00:33:19,610 And so I can do this again, python arithmetic.py. 704 00:33:19,610 --> 00:33:26,020 I'll try doing 8 divided by 2, and I see that the answer is 4. 705 00:33:26,020 --> 00:33:29,440 And so you can use these various different arithmetic operators 706 00:33:29,440 --> 00:33:34,060 to perform calculations, by taking numbers and manipulating those numbers. 707 00:33:34,060 --> 00:33:37,450 And in a lot of programs, you'll end up needing to do a little bit of math. 708 00:33:37,450 --> 00:33:40,690 And the computer is very fast at being able to quickly perform 709 00:33:40,690 --> 00:33:43,150 mathematical operations. 710 00:33:43,150 --> 00:33:44,440 Oh, I see a great question. 711 00:33:44,440 --> 00:33:50,770 Why is it that when I divide, the answer is 4.0 and not just 4? 712 00:33:50,770 --> 00:33:54,580 So Python realizes that when I divide one integer by another, 713 00:33:54,580 --> 00:33:56,870 I might not get a perfect whole number. 714 00:33:56,870 --> 00:34:01,900 If I take 3 divided by 2, for example, well, 3 doesn't go evenly into 2. 715 00:34:01,900 --> 00:34:04,728 What I would get is something like 1 and 1/2. 716 00:34:04,728 --> 00:34:06,020 And so let's try this actually. 717 00:34:06,020 --> 00:34:11,290 I'll try 3 divided by 2, and the answer I get is 1.5. 718 00:34:11,290 --> 00:34:15,190 And so Python is assuming when I perform division, that what I might get 719 00:34:15,190 --> 00:34:17,060 is something after the decimal. 720 00:34:17,060 --> 00:34:19,870 So it's actually given me a different type of data, something 721 00:34:19,870 --> 00:34:21,670 called a floating point number. 722 00:34:21,670 --> 00:34:23,770 You'll often hear this just called a float 723 00:34:23,770 --> 00:34:25,420 when programmers are talking about it. 724 00:34:25,420 --> 00:34:27,489 And a floating point number is just a number that 725 00:34:27,489 --> 00:34:29,530 might have something after the decimal. 726 00:34:29,530 --> 00:34:33,460 An integer is always just going to be a whole number or a negative number or 0, 727 00:34:33,460 --> 00:34:37,300 something like negative 1 or 0 or 1 or 2 or 3, 728 00:34:37,300 --> 00:34:39,639 whereas a floating point number could have something 729 00:34:39,639 --> 00:34:42,909 after the decimal, something like 1.5, in this case. 730 00:34:42,909 --> 00:34:44,860 And Python is just being a little bit smart, 731 00:34:44,860 --> 00:34:47,260 noting that when I do division on two integers, 732 00:34:47,260 --> 00:34:53,040 I might end up getting a floating point number instead. 733 00:34:53,040 --> 00:34:54,920 Excellent question there, too. 734 00:34:54,920 --> 00:34:57,600 735 00:34:57,600 --> 00:35:00,350 All right, so let's try and explore some other features of Python. 736 00:35:00,350 --> 00:35:02,540 In a moment, after we do a couple more exercises, 737 00:35:02,540 --> 00:35:06,020 I'll give you an opportunity to try a little bit on your own, too. 738 00:35:06,020 --> 00:35:09,080 But what I'm going to introduce now is the notion of a condition. 739 00:35:09,080 --> 00:35:12,200 And so, so far, when we have been writing Python programs 740 00:35:12,200 --> 00:35:15,590 our programs are just running one line at a time, top to bottom, 741 00:35:15,590 --> 00:35:17,600 running every single line of code. 742 00:35:17,600 --> 00:35:20,240 But our programs can get a lot more interesting than that. 743 00:35:20,240 --> 00:35:23,000 And ideally, what we want our programs to be able to do 744 00:35:23,000 --> 00:35:27,590 is to sometimes run some code, and sometimes not run that code, 745 00:35:27,590 --> 00:35:31,730 to be able to make a decision about whether to run some code or not. 746 00:35:31,730 --> 00:35:34,040 You've probably experienced this on your own, too. 747 00:35:34,040 --> 00:35:37,370 Maybe when you're on a website and only if you click on a button, 748 00:35:37,370 --> 00:35:38,220 something happens. 749 00:35:38,220 --> 00:35:41,630 So if you do something, then some code is going to run, 750 00:35:41,630 --> 00:35:46,078 but only if something is actually true. 751 00:35:46,078 --> 00:35:47,120 So let's give that a try. 752 00:35:47,120 --> 00:35:49,550 How can we introduce this notion of conditions, 753 00:35:49,550 --> 00:35:56,030 this notion of if to be able to conditionally perform some action? 754 00:35:56,030 --> 00:35:58,010 So I'm going to create a new file. 755 00:35:58,010 --> 00:36:02,223 I'm going to call this conditions.py, some new Python program. 756 00:36:02,223 --> 00:36:03,640 And here's what we're going to do. 757 00:36:03,640 --> 00:36:06,580 I'm going to start the same way I started my arithmetic program, 758 00:36:06,580 --> 00:36:08,500 by asking for an integer from the user. 759 00:36:08,500 --> 00:36:10,450 So x is going to be an integer. 760 00:36:10,450 --> 00:36:15,220 I'm going to ask for input from the user, which is going to just be x. 761 00:36:15,220 --> 00:36:17,920 And let's come up with a simple condition now. 762 00:36:17,920 --> 00:36:24,687 I'm going to say if x is greater than 0 and then a colon. 763 00:36:24,687 --> 00:36:26,770 And then now this is going to be a little bit new. 764 00:36:26,770 --> 00:36:29,950 But on a new line, and this line is going to be indented. 765 00:36:29,950 --> 00:36:32,760 And if you're using CS50 IDE or CS50 Sandbox, 766 00:36:32,760 --> 00:36:34,510 you'll probably find that the IDE is going 767 00:36:34,510 --> 00:36:36,338 to automatically indent this line for you, 768 00:36:36,338 --> 00:36:38,380 because it's guessing that you want to indent it. 769 00:36:38,380 --> 00:36:40,640 I'll explain why in a moment. 770 00:36:40,640 --> 00:36:44,650 Let me just print out ("positive"). 771 00:36:44,650 --> 00:36:46,160 So what's going on here? 772 00:36:46,160 --> 00:36:48,820 Here on line 3, we're asking a question. 773 00:36:48,820 --> 00:36:54,040 We're asking, if x is greater than 0, is it true that x, this variable, 774 00:36:54,040 --> 00:36:55,690 is greater than 0? 775 00:36:55,690 --> 00:37:00,190 And this colon is going to introduce some code that I'm going to run, 776 00:37:00,190 --> 00:37:04,730 only if this code is true, only if x is greater than 0, 777 00:37:04,730 --> 00:37:07,360 I'm going to print the word positive. 778 00:37:07,360 --> 00:37:09,460 And the way to read this is that anything that's 779 00:37:09,460 --> 00:37:13,300 indented underneath this line if, is the code 780 00:37:13,300 --> 00:37:17,710 that is only going to run if that condition is true. 781 00:37:17,710 --> 00:37:21,040 And so because I've indented this line underneath the if, this line of code 782 00:37:21,040 --> 00:37:24,970 is only going to run if x is actually greater than 0. 783 00:37:24,970 --> 00:37:27,580 And this gives our code the ability to make a decision, 784 00:37:27,580 --> 00:37:33,320 to decide whether to run this line of code or not. 785 00:37:33,320 --> 00:37:35,090 And so I can give this a try. 786 00:37:35,090 --> 00:37:38,770 I'm going to run this program, Python conditions.py, and watch. 787 00:37:38,770 --> 00:37:40,720 Let me type in at a positive number. 788 00:37:40,720 --> 00:37:43,840 I'll type in the number 2, for example. 789 00:37:43,840 --> 00:37:45,340 And it prints out the word positive. 790 00:37:45,340 --> 00:37:48,350 Great, exactly what I would expect for it to do. 791 00:37:48,350 --> 00:37:52,390 But let me now try to print out a number that's not positive. 792 00:37:52,390 --> 00:37:55,610 Let me try negative 8, for example, a negative number. 793 00:37:55,610 --> 00:37:57,360 And let's see what happens. 794 00:37:57,360 --> 00:37:58,900 And notice nothing gets printed out. 795 00:37:58,900 --> 00:38:01,030 The word positive doesn't get printed out, 796 00:38:01,030 --> 00:38:04,520 even though I have a line of code that says print positive here. 797 00:38:04,520 --> 00:38:08,320 And the reason why is because my Python program asks this question, 798 00:38:08,320 --> 00:38:10,480 is x greater than 0? 799 00:38:10,480 --> 00:38:12,880 If it's true we print out the word positive. 800 00:38:12,880 --> 00:38:16,030 But otherwise, we don't print anything at all. 801 00:38:16,030 --> 00:38:18,970 And the program is just over. 802 00:38:18,970 --> 00:38:21,630 Yeah, for those who are familiar with programming in C, 803 00:38:21,630 --> 00:38:23,820 if you've maybe taken the beginning of CS50, 804 00:38:23,820 --> 00:38:25,530 this looks a little bit different than C. 805 00:38:25,530 --> 00:38:29,340 We don't have those curly braces to indicate that there is a condition. 806 00:38:29,340 --> 00:38:31,740 Python syntax is a little bit different, admittedly. 807 00:38:31,740 --> 00:38:33,760 But the idea is still the same idea. 808 00:38:33,760 --> 00:38:37,800 The idea is that you need to ask the question, 809 00:38:37,800 --> 00:38:42,900 and then inside of the if condition goes whatever it is that we want to print. 810 00:38:42,900 --> 00:38:46,280 811 00:38:46,280 --> 00:38:49,490 Is the indentation necessary, I see someone asking. 812 00:38:49,490 --> 00:38:50,780 In fact, it is necessary. 813 00:38:50,780 --> 00:38:53,630 Watch what happens if I try not to include that indentation. 814 00:38:53,630 --> 00:38:57,200 I'll try putting everything just aligned on the left hand side. 815 00:38:57,200 --> 00:38:58,640 Let's run the program. 816 00:38:58,640 --> 00:39:00,080 And immediately, I get an error. 817 00:39:00,080 --> 00:39:04,490 This is the first Python exception, or error, that we've seen so far. 818 00:39:04,490 --> 00:39:07,670 And my Python error is that there is an indentation error. 819 00:39:07,670 --> 00:39:11,037 We expected an indented block, and Python's even 820 00:39:11,037 --> 00:39:13,620 helpful enough to tell me what line that error is coming from. 821 00:39:13,620 --> 00:39:17,750 It's coming from line 4 of conditions.py. 822 00:39:17,750 --> 00:39:21,800 And so line 4 of conditions.py seems to be having some sort of error. 823 00:39:21,800 --> 00:39:24,080 The error is that we were supposed to indent something 824 00:39:24,080 --> 00:39:25,550 but it wasn't indented. 825 00:39:25,550 --> 00:39:29,330 And so I do, in fact, need to indent this line in order 826 00:39:29,330 --> 00:39:31,290 for my code to work correctly. 827 00:39:31,290 --> 00:39:33,830 So the indentation is necessary in Python. 828 00:39:33,830 --> 00:39:38,100 829 00:39:38,100 --> 00:39:41,320 All right, so let's try and add to this program a little bit. 830 00:39:41,320 --> 00:39:44,530 So right now I'm printing something if the number is positive. 831 00:39:44,530 --> 00:39:47,340 Let's try and add what happens if it's not positive. 832 00:39:47,340 --> 00:39:50,230 So this is what we're going to call an else. 833 00:39:50,230 --> 00:39:53,460 So in programming we can often say if something is true, 834 00:39:53,460 --> 00:39:57,880 do this, but otherwise else, do something else. 835 00:39:57,880 --> 00:40:00,720 So we're going to run one block of code, or we're 836 00:40:00,720 --> 00:40:02,730 going to run some other block of code. 837 00:40:02,730 --> 00:40:05,970 And so underneath the if condition, not indented, 838 00:40:05,970 --> 00:40:10,295 I'm going to write the word else, meaning if that condition wasn't true, 839 00:40:10,295 --> 00:40:11,670 here's what I want to do instead. 840 00:40:11,670 --> 00:40:18,340 And what I want to do instead is I want to print the word negative instead. 841 00:40:18,340 --> 00:40:22,370 And so now the way to read this program is that if x is greater than 0, 842 00:40:22,370 --> 00:40:24,180 I'm going to print the word positive. 843 00:40:24,180 --> 00:40:28,110 Else if it's not greater than 0, I'm going to print negative. 844 00:40:28,110 --> 00:40:30,690 And that way I'll either get one of these blocks of code 845 00:40:30,690 --> 00:40:35,430 to run or the other block of code to run, instead. 846 00:40:35,430 --> 00:40:39,090 And so I'll try running this I'll try running Python conditions.py. 847 00:40:39,090 --> 00:40:43,130 Again, if I provide a positive number, I can type the number 2 848 00:40:43,130 --> 00:40:44,850 and it's going to say positive. 849 00:40:44,850 --> 00:40:48,900 But if instead I type a negative number, I'll try typing the number negative 8, 850 00:40:48,900 --> 00:40:51,070 and I get negative printed out instead. 851 00:40:51,070 --> 00:40:53,220 So I'm never going to get both printed out. 852 00:40:53,220 --> 00:40:56,490 If the condition is true, then line 4 is going to run. 853 00:40:56,490 --> 00:40:59,930 If it's not true this else case is going to run instead. 854 00:40:59,930 --> 00:41:04,800 And so you can add that to handle what's going to happen if the condition is not 855 00:41:04,800 --> 00:41:07,430 true, instead. 856 00:41:07,430 --> 00:41:09,700 Now maybe there's a slight bug in this program. 857 00:41:09,700 --> 00:41:11,730 And I think a couple of people have noticed this bug already, 858 00:41:11,730 --> 00:41:12,813 taking a look at the chat. 859 00:41:12,813 --> 00:41:17,043 But does anyone else see what the maybe bug is in my program right now? 860 00:41:17,043 --> 00:41:18,960 Yeah I see a bunch of people suggesting what's 861 00:41:18,960 --> 00:41:21,420 going to happen if I try the number 0? 862 00:41:21,420 --> 00:41:25,120 And so this is a case maybe I didn't think about yet. 863 00:41:25,120 --> 00:41:27,400 So let's try this out. 864 00:41:27,400 --> 00:41:32,430 What happens if I run this program, but I instead type the number 0? 865 00:41:32,430 --> 00:41:34,070 All right, well the number is 0. 866 00:41:34,070 --> 00:41:38,770 My program somehow thinks is negative, when really 0 isn't really negative, 867 00:41:38,770 --> 00:41:41,650 it's just not positive, it's not negative, it's just 0. 868 00:41:41,650 --> 00:41:44,050 And so I would like to be able to handle now 869 00:41:44,050 --> 00:41:48,130 a situation where I can deal with both positive numbers and negative numbers 870 00:41:48,130 --> 00:41:49,540 and 0. 871 00:41:49,540 --> 00:41:52,300 And so here I'm going to show you how you could chain together 872 00:41:52,300 --> 00:41:56,540 multiple conditions, and the syntax for this is a little bit unusual. 873 00:41:56,540 --> 00:42:00,700 But here on line 5, instead of just saying else, meaning in all other cases 874 00:42:00,700 --> 00:42:04,180 go ahead and print the word negative, let's go ahead 875 00:42:04,180 --> 00:42:07,150 and say elif, which seems a little bit weird. 876 00:42:07,150 --> 00:42:12,850 But elif, which is short for else if, x is less than 0. 877 00:42:12,850 --> 00:42:15,920 This is me now asking another question. 878 00:42:15,920 --> 00:42:20,450 So if x is greater than 0, we're going to print out positive. 879 00:42:20,450 --> 00:42:24,370 Otherwise else, if our variable is less than 0, 880 00:42:24,370 --> 00:42:26,560 we're going to print out negative. 881 00:42:26,560 --> 00:42:30,580 And now at the very end, I'm going to add one more else, where 882 00:42:30,580 --> 00:42:34,270 I'm going to print 0, meaning if the number is not positive 883 00:42:34,270 --> 00:42:37,570 and it's not negative, the only other option is that it's 0. 884 00:42:37,570 --> 00:42:39,830 And so we're going to print out 0 instead. 885 00:42:39,830 --> 00:42:44,440 And so this allows me to make multiple different decisions based on questions 886 00:42:44,440 --> 00:42:47,600 that I might ask inside of my program. 887 00:42:47,600 --> 00:42:50,380 And so I'll run Python conditions.py. 888 00:42:50,380 --> 00:42:52,210 Let me try typing a positive number, 2. 889 00:42:52,210 --> 00:42:53,590 That's positive, very good. 890 00:42:53,590 --> 00:42:55,720 I'll type a negative number, that's negative. 891 00:42:55,720 --> 00:42:58,950 And I'll try typing 0, and that's going to be 0. 892 00:42:58,950 --> 00:43:00,820 And so now I have these different conditions 893 00:43:00,820 --> 00:43:03,430 that can handle these various different situations that 894 00:43:03,430 --> 00:43:06,010 might arise as I run this code. 895 00:43:06,010 --> 00:43:16,510 896 00:43:16,510 --> 00:43:21,400 All right, give people an opportunity to experiment with that. 897 00:43:21,400 --> 00:43:24,930 898 00:43:24,930 --> 00:43:28,180 You do not need parentheses around the if statements, which a couple of people 899 00:43:28,180 --> 00:43:29,050 are asking about. 900 00:43:29,050 --> 00:43:32,260 It is OK to include them, but not necessary in Python. 901 00:43:32,260 --> 00:43:35,080 And so we'll generally not include those parentheses 902 00:43:35,080 --> 00:43:36,670 if ultimately they're not necessary. 903 00:43:36,670 --> 00:43:40,015 The one thing that is important, I see one person ran into an error with this, 904 00:43:40,015 --> 00:43:42,580 is this colon at the end of the if line. 905 00:43:42,580 --> 00:43:44,080 That is, in fact, necessary. 906 00:43:44,080 --> 00:43:46,510 And that just means whatever comes next, this 907 00:43:46,510 --> 00:43:49,093 is going to be part of that condition. 908 00:43:49,093 --> 00:43:52,960 909 00:43:52,960 --> 00:43:55,930 All right so conditions are a very powerful part of programming, 910 00:43:55,930 --> 00:43:59,740 and they're going to allow us to achieve a lot of very interesting things 911 00:43:59,740 --> 00:44:03,140 with our programs that we'll have an opportunity to experiment with as well. 912 00:44:03,140 --> 00:44:06,550 But one other thing I want to demonstrate is the idea of loops, 913 00:44:06,550 --> 00:44:09,420 and we'll see the idea here in a moment. 914 00:44:09,420 --> 00:44:12,220 I'm going to create a new file called loops.py. 915 00:44:12,220 --> 00:44:14,590 Oftentimes in a program, you are going to want 916 00:44:14,590 --> 00:44:19,307 to run some code multiple times, because often the advantage of a computer 917 00:44:19,307 --> 00:44:20,890 is it can do many things very quickly. 918 00:44:20,890 --> 00:44:24,530 It can repeat some code again and again and again. 919 00:44:24,530 --> 00:44:26,600 And so how might I go about doing this? 920 00:44:26,600 --> 00:44:29,170 Well, let's imagine, remember from my very first program 921 00:44:29,170 --> 00:44:30,940 I printed out hello, world. 922 00:44:30,940 --> 00:44:35,710 Imagine I wanted to print out hello, world 10 times, let's say. 923 00:44:35,710 --> 00:44:36,687 How could I do that? 924 00:44:36,687 --> 00:44:38,770 Well, one way would be let's take this line hello, 925 00:44:38,770 --> 00:44:42,730 world and let me just print("hello, world"), print("hello, world"). 926 00:44:42,730 --> 00:44:46,060 And let me just copy paste it 10 times. 927 00:44:46,060 --> 00:44:48,760 Just like that, I've printed out ("hello, world") 10 times. 928 00:44:48,760 --> 00:44:51,162 And let me now run this program. 929 00:44:51,162 --> 00:44:52,870 And I do see hello, world get printed out 930 00:44:52,870 --> 00:44:56,740 10 times, so I have achieved what it is that I wanted to achieve. 931 00:44:56,740 --> 00:44:59,850 But why is this maybe not such a great design for a program? 932 00:44:59,850 --> 00:45:01,860 And now we're introducing this notion of design. 933 00:45:01,860 --> 00:45:05,100 How can you create a better designed program? 934 00:45:05,100 --> 00:45:08,430 Why is this not ideal? 935 00:45:08,430 --> 00:45:12,210 Yeah, I see [? Marciara ?] is asking what if I had to do this 100 times? 936 00:45:12,210 --> 00:45:14,310 Then I'd have to copy this line 100 times. 937 00:45:14,310 --> 00:45:18,750 If I wanted to change the number of times, it's not so easy to do. 938 00:45:18,750 --> 00:45:19,950 And it's not very concise. 939 00:45:19,950 --> 00:45:22,410 I'm repeating myself a lot, so any time you 940 00:45:22,410 --> 00:45:25,200 find yourself repeating yourself a lot, writing lengthy code that 941 00:45:25,200 --> 00:45:27,990 might be hard to read, maybe there's an opportunity 942 00:45:27,990 --> 00:45:30,330 to use something called a loop. 943 00:45:30,330 --> 00:45:32,730 And there's multiple ways to use a loop in Python. 944 00:45:32,730 --> 00:45:35,570 We'll explore a couple of those ways right now. 945 00:45:35,570 --> 00:45:36,800 But here's one way. 946 00:45:36,800 --> 00:45:39,860 I'll write the code first and then explain where it comes from. 947 00:45:39,860 --> 00:45:42,620 Let me give myself a variable called i. 948 00:45:42,620 --> 00:45:45,598 I is just a common variable that's used for keeping track of a loop, 949 00:45:45,598 --> 00:45:46,640 but it's just a variable. 950 00:45:46,640 --> 00:45:48,840 You could name it whatever you want. 951 00:45:48,840 --> 00:45:55,010 And now I'm going to write a line of code that says while i is less than 10, 952 00:45:55,010 --> 00:45:57,950 print hello, world, and this is going to be indented. 953 00:45:57,950 --> 00:46:02,700 And then i equals i plus 1. 954 00:46:02,700 --> 00:46:04,260 So what am I doing here? 955 00:46:04,260 --> 00:46:06,830 I'm creating a variable i equals 0. 956 00:46:06,830 --> 00:46:09,770 And then this line is what we call a while loop. 957 00:46:09,770 --> 00:46:12,410 And a while loop is going to run some code over and over 958 00:46:12,410 --> 00:46:16,970 and over again repeatedly, as long as some condition is true, as 959 00:46:16,970 --> 00:46:20,810 long as some question is answered with a yes. 960 00:46:20,810 --> 00:46:25,230 So as long as this variable i is less than 10, what are we going to do? 961 00:46:25,230 --> 00:46:28,340 We're going to run all of these lines that are indented. 962 00:46:28,340 --> 00:46:30,710 We're going to print("hello, world"). 963 00:46:30,710 --> 00:46:35,150 And then beneath that, I'm running this line i equals i plus 1. 964 00:46:35,150 --> 00:46:36,900 And if you've never seen a line like this, 965 00:46:36,900 --> 00:46:39,067 it might look a little weird from the world of math, 966 00:46:39,067 --> 00:46:40,700 like i is not equal to i plus 1. 967 00:46:40,700 --> 00:46:42,540 Those are two different numbers. 968 00:46:42,540 --> 00:46:46,800 But this equals sign, remember, stores something inside of a variable. 969 00:46:46,800 --> 00:46:50,675 So what I'm doing here is changing what is stored inside of the variable i. 970 00:46:50,675 --> 00:46:55,520 What I would want it to be equal to now, well, whatever i plus 1 is equal to. 971 00:46:55,520 --> 00:47:00,740 So if I right now the number 0, then i plus 1 is going to be the number 1. 972 00:47:00,740 --> 00:47:03,380 And we're going to store that number 1 inside of i. 973 00:47:03,380 --> 00:47:05,360 I'm changing the value of a variable. 974 00:47:05,360 --> 00:47:08,300 Variables don't just stay the same, they can change, 975 00:47:08,300 --> 00:47:13,160 and so I can update the value of i, increasing it by 1, and then try again. 976 00:47:13,160 --> 00:47:16,940 Now, i is 1, which is still less than 10, so we print("hello, world"). 977 00:47:16,940 --> 00:47:19,232 I increases to 2, and we run it again. 978 00:47:19,232 --> 00:47:20,940 And this is going to keep happening. i is 979 00:47:20,940 --> 00:47:25,160 going to be 3, 4, 5, 6, 7, every time printing out hello, world, 980 00:47:25,160 --> 00:47:29,030 until we get to a point where i is equal to 10. 981 00:47:29,030 --> 00:47:31,540 And at that point, i is not less than 10. 982 00:47:31,540 --> 00:47:35,450 10 is not less than 10, and so we're going to exit the loop. 983 00:47:35,450 --> 00:47:37,580 The condition is no longer true. 984 00:47:37,580 --> 00:47:40,260 And the loop is going to be over. 985 00:47:40,260 --> 00:47:45,030 And so if I run this code now, I see hello, world repeated 10 times. 986 00:47:45,030 --> 00:47:49,230 And notice, I only needed to write the line of code print hello one time. 987 00:47:49,230 --> 00:47:52,710 The loop, the while loop, is taking care of the process for me 988 00:47:52,710 --> 00:47:55,260 of counting how many times I've run this so far. 989 00:47:55,260 --> 00:47:59,130 And as long as i is less than 10, we're going to keep printing hello, world 990 00:47:59,130 --> 00:48:00,480 again and again and again. 991 00:48:00,480 --> 00:48:02,940 And this makes my code much more flexible. 992 00:48:02,940 --> 00:48:05,700 If I only wanted to print hello, world three times, 993 00:48:05,700 --> 00:48:07,500 I could change the 10 to 3. 994 00:48:07,500 --> 00:48:10,620 And now, it's only going to print hello, world three times. 995 00:48:10,620 --> 00:48:12,690 If I wanted to print hello, world 1,000 times, 996 00:48:12,690 --> 00:48:14,760 I could change this number to 1,000. 997 00:48:14,760 --> 00:48:18,045 And now, it's going to print hello, world 1,000 times. 998 00:48:18,045 --> 00:48:19,920 And so you can change this number to whatever 999 00:48:19,920 --> 00:48:23,040 we want it to be to decide how many times we 1000 00:48:23,040 --> 00:48:26,748 want to print out hello, world. 1001 00:48:26,748 --> 00:48:29,290 I see a couple of people, including [? Prince ?] in the chat, 1002 00:48:29,290 --> 00:48:32,770 suggesting that there is a more concise way that I can write this line. 1003 00:48:32,770 --> 00:48:35,500 It turns out that updating a variable, like increasing 1004 00:48:35,500 --> 00:48:39,340 a variable by 1 or by some other number, happens so often in programming, 1005 00:48:39,340 --> 00:48:43,120 we do this so much when writing code, that there is a shorthand, a shorter 1006 00:48:43,120 --> 00:48:44,740 way of writing the same thing. 1007 00:48:44,740 --> 00:48:51,145 I can write the line of code i += equals 1, to mean add this value to i. 1008 00:48:51,145 --> 00:48:52,780 What value do I want to add to i? 1009 00:48:52,780 --> 00:48:54,490 I want to add the value 1. 1010 00:48:54,490 --> 00:48:58,300 And that's just a shorter, simpler way of expressing the same idea. 1011 00:48:58,300 --> 00:49:01,660 My code will run exactly the same way, but this is just a little bit more 1012 00:49:01,660 --> 00:49:05,230 of a concise way of expressing that same thing. 1013 00:49:05,230 --> 00:49:08,480 1014 00:49:08,480 --> 00:49:10,750 So this is what we in programming call a while loop, 1015 00:49:10,750 --> 00:49:13,930 and this is useful anytime we want to ask a question 1016 00:49:13,930 --> 00:49:17,080 and then keep running this code as long as the answer to that question 1017 00:49:17,080 --> 00:49:20,382 is yes, as long as the condition is true. 1018 00:49:20,382 --> 00:49:22,090 It turns out there's another type of loop 1019 00:49:22,090 --> 00:49:25,150 I see a couple of people suggesting called a for loop. 1020 00:49:25,150 --> 00:49:28,180 A for loop is very helpful if you know exactly how many times you 1021 00:49:28,180 --> 00:49:31,630 want some code to run, and it's just going 1022 00:49:31,630 --> 00:49:35,350 to be a little bit more concise for this particular case. 1023 00:49:35,350 --> 00:49:38,100 And I'll show you what a for loop would look like. 1024 00:49:38,100 --> 00:49:40,070 A for loop would look like this. 1025 00:49:40,070 --> 00:49:41,760 I'm going to delete all of this code. 1026 00:49:41,760 --> 00:49:50,610 I'm instead going to say for i in range 5, print hello, world. 1027 00:49:50,610 --> 00:49:53,420 And this is going to do exactly the same thing as before. 1028 00:49:53,420 --> 00:49:59,047 What this is saying is range 5 means get me a range of numbers-- 1029 00:49:59,047 --> 00:50:00,380 give me a range of five numbers. 1030 00:50:00,380 --> 00:50:03,800 That's going to give you the number 0, 1, 2, 3, and 4, five different numbers 1031 00:50:03,800 --> 00:50:06,290 starting at 0 going up to 4. 1032 00:50:06,290 --> 00:50:10,380 And for each of those values, go ahead and print out hello, world. 1033 00:50:10,380 --> 00:50:14,000 So five times for all the numbers in the range of five numbers, 1034 00:50:14,000 --> 00:50:15,615 print out hello, world. 1035 00:50:15,615 --> 00:50:17,240 And this is going to do the same thing. 1036 00:50:17,240 --> 00:50:21,510 Five times it's going to print hello, world. 1037 00:50:21,510 --> 00:50:25,177 And so this is an opportunity now to simplify our code a little bit. 1038 00:50:25,177 --> 00:50:26,510 We could have used a while loop. 1039 00:50:26,510 --> 00:50:27,970 That worked perfectly fine. 1040 00:50:27,970 --> 00:50:30,470 But if I know exactly how many times I want the code to run, 1041 00:50:30,470 --> 00:50:33,670 I want it to run five times, oftentimes a for loop 1042 00:50:33,670 --> 00:50:35,600 is just a little bit more concise. 1043 00:50:35,600 --> 00:50:38,620 1044 00:50:38,620 --> 00:50:41,738 I see someone asking in the chat, could we take user input? 1045 00:50:41,738 --> 00:50:43,030 And the answer is yes we could. 1046 00:50:43,030 --> 00:50:44,770 In fact, let's try that now. 1047 00:50:44,770 --> 00:50:48,280 I'm going to add a line of code above my for loops, up at line 1. 1048 00:50:48,280 --> 00:50:50,530 I've moved the for loop down a little bit. 1049 00:50:50,530 --> 00:50:51,643 Let me ask for a number. 1050 00:50:51,643 --> 00:50:53,560 I'll call that number n, but you could call it 1051 00:50:53,560 --> 00:50:55,520 whatever you want, from the user. 1052 00:50:55,520 --> 00:50:59,740 I'm going to use int and input again, which we've seen a couple of times now. 1053 00:50:59,740 --> 00:51:03,730 I'm asking the user to type in a number, and I'm 1054 00:51:03,730 --> 00:51:08,472 going to save whatever the user types in inside of this variable n. 1055 00:51:08,472 --> 00:51:09,930 And now watch what I'm going to do. 1056 00:51:09,930 --> 00:51:14,700 I'm going to replace this number 5 with that variable n. 1057 00:51:14,700 --> 00:51:17,340 So it's not just going to print hello, world 5 times. 1058 00:51:17,340 --> 00:51:22,290 It's going to print hello, world n times, where n is whatever 1059 00:51:22,290 --> 00:51:25,810 it is the user typed in as input. 1060 00:51:25,810 --> 00:51:27,220 So let's give it a try. 1061 00:51:27,220 --> 00:51:29,000 We run python loops.py. 1062 00:51:29,000 --> 00:51:31,290 It's asking me now to type in a number. 1063 00:51:31,290 --> 00:51:33,020 Let me type in the number 3. 1064 00:51:33,020 --> 00:51:35,460 And it prints hello, world three times. 1065 00:51:35,460 --> 00:51:39,690 If instead I tried typing the number 5, well then it's going to print hello, 1066 00:51:39,690 --> 00:51:41,230 world five times. 1067 00:51:41,230 --> 00:51:44,280 And so I can now decide how many times something runs. 1068 00:51:44,280 --> 00:51:47,040 And this is really the power of loops and variables. 1069 00:51:47,040 --> 00:51:50,970 I can store some input in a variable and use that information 1070 00:51:50,970 --> 00:51:56,190 to decide how many times my code is ultimately going to run. 1071 00:51:56,190 --> 00:52:00,750 1072 00:52:00,750 --> 00:52:04,275 All right I'll pause here for questions. 1073 00:52:04,275 --> 00:52:06,900 I see someone asking, why did I put the colon after the number? 1074 00:52:06,900 --> 00:52:08,700 Is that required too? 1075 00:52:08,700 --> 00:52:09,630 It's not. 1076 00:52:09,630 --> 00:52:12,750 This colon on line 3 after the for loop, that is required. 1077 00:52:12,750 --> 00:52:15,480 Much like an if condition, I need a colon 1078 00:52:15,480 --> 00:52:18,870 to say everything indented after this line, that's 1079 00:52:18,870 --> 00:52:20,650 going to be part of the loop. 1080 00:52:20,650 --> 00:52:24,270 This colon inside these quotation marks is just part of the text, 1081 00:52:24,270 --> 00:52:25,740 it's part of the user interface. 1082 00:52:25,740 --> 00:52:29,070 It causes a colon to appear printed out to the user. 1083 00:52:29,070 --> 00:52:31,650 I just use that as a prompt to the user to let them 1084 00:52:31,650 --> 00:52:36,480 know that here now is an opportunity for you to type in a number, for example. 1085 00:52:36,480 --> 00:52:39,440 1086 00:52:39,440 --> 00:52:43,030 So now we've seen a bunch of different features of Python. 1087 00:52:43,030 --> 00:52:46,150 We've seen variables, we've seen printing out values, 1088 00:52:46,150 --> 00:52:49,172 we've seen conditions, we've seen loops. 1089 00:52:49,172 --> 00:52:51,880 I'm going to go ahead and give you your first exercise, actually. 1090 00:52:51,880 --> 00:52:52,980 So I'm going to introduce the exercise. 1091 00:52:52,980 --> 00:52:55,060 And in a moment I'm going to stop talking, 1092 00:52:55,060 --> 00:53:00,520 give you an opportunity to try in CS50 IDE, or inside of the Sandbox 1093 00:53:00,520 --> 00:53:03,280 if you're using that, to try writing a program of your very own. 1094 00:53:03,280 --> 00:53:04,300 Then we'll come back together. 1095 00:53:04,300 --> 00:53:05,290 You don't have to leave the meeting. 1096 00:53:05,290 --> 00:53:08,500 You can stay here, but we'll come back together afterwards, talk about it, 1097 00:53:08,500 --> 00:53:11,330 move on to talk about some other features of Python as well. 1098 00:53:11,330 --> 00:53:15,460 And we'll introduce our very first programming exercise for you. 1099 00:53:15,460 --> 00:53:17,732 So I'll introduce the context for the exercise. 1100 00:53:17,732 --> 00:53:19,690 I've been doing a little bit of running lately. 1101 00:53:19,690 --> 00:53:22,790 And I've wanted to keep track of how many miles have I've been running? 1102 00:53:22,790 --> 00:53:25,330 So maybe I run on 10 different days, and I 1103 00:53:25,330 --> 00:53:27,850 want to add up how far am I running on each of these days? 1104 00:53:27,850 --> 00:53:30,072 How far have I run in total? 1105 00:53:30,072 --> 00:53:31,780 So we're going to try and write a program 1106 00:53:31,780 --> 00:53:34,670 to help me out with that a little bit. 1107 00:53:34,670 --> 00:53:38,920 And so what I'm going to ask you to do is follow these instructions here. 1108 00:53:38,920 --> 00:53:41,020 You're going to write a program called sum. 1109 00:53:41,020 --> 00:53:45,055 That's going to ask the user to type in 10 integers as input. 1110 00:53:45,055 --> 00:53:46,930 And then you're going to print out their sum. 1111 00:53:46,930 --> 00:53:50,800 So the user is going to be prompted 10 times to type in numbers. 1112 00:53:50,800 --> 00:53:53,410 And then at the end, after the user types in 10 numbers, 1113 00:53:53,410 --> 00:53:58,280 you're going to print out the sum of all of those 10 integers. 1114 00:53:58,280 --> 00:54:01,750 And so I am going to go ahead and give you maybe 10 minutes on your own 1115 00:54:01,750 --> 00:54:03,458 to try writing this program for yourself. 1116 00:54:03,458 --> 00:54:05,750 You don't have to leave the meeting, you can stay here. 1117 00:54:05,750 --> 00:54:08,560 But if you do have any questions, if you run into any trouble, 1118 00:54:08,560 --> 00:54:11,140 feel free to ask a question on ed, the discussion forum where 1119 00:54:11,140 --> 00:54:13,223 you can post the code that you've been working on, 1120 00:54:13,223 --> 00:54:16,530 and the mentors who are in ed right now are going 1121 00:54:16,530 --> 00:54:18,160 to be there to help answer questions. 1122 00:54:18,160 --> 00:54:20,890 Valerie has just pasted the link to ed inside 1123 00:54:20,890 --> 00:54:23,540 of the chat, which you can use to ask questions. 1124 00:54:23,540 --> 00:54:24,790 You can create a new post. 1125 00:54:24,790 --> 00:54:28,300 You'll go ahead and, inside of ed, what you'll do if you have a question 1126 00:54:28,300 --> 00:54:32,350 you'll click on New Thread, go ahead and click on Question, 1127 00:54:32,350 --> 00:54:36,280 and then go ahead and click on Python as the category for the category today, 1128 00:54:36,280 --> 00:54:38,770 and then you can ask whatever question you want there 1129 00:54:38,770 --> 00:54:41,390 and the staff will be around to help you out with that too. 1130 00:54:41,390 --> 00:54:48,070 So we will go ahead and give you 10 minutes or so to work on that program. 1131 00:54:48,070 --> 00:54:51,520 And so I will go ahead and throw up a timer. 1132 00:54:51,520 --> 00:54:52,842 You should see the timer here. 1133 00:54:52,842 --> 00:54:54,050 So we'll give you 10 minutes. 1134 00:54:54,050 --> 00:54:57,100 I'm going to stop talking, give you 10 minutes to work on this program. 1135 00:54:57,100 --> 00:54:59,270 And when we come back, we'll talk about it. 1136 00:54:59,270 --> 00:55:01,645 So we'll see you in 10 minutes, feel free to work on this 1137 00:55:01,645 --> 00:55:03,340 and ask questions in ed if you have any. 1138 00:55:03,340 --> 00:55:05,297 All right, welcome back, everyone. 1139 00:55:05,297 --> 00:55:06,880 Hopefully you enjoyed working on that. 1140 00:55:06,880 --> 00:55:09,255 I saw a lot of great questions in the ed discussion forum 1141 00:55:09,255 --> 00:55:11,060 as we were working through that. 1142 00:55:11,060 --> 00:55:12,980 So let's talk about how we approached it. 1143 00:55:12,980 --> 00:55:15,910 Does someone want describe to me what approach they 1144 00:55:15,910 --> 00:55:19,210 took to trying to solve this problem? 1145 00:55:19,210 --> 00:55:22,210 What kinds of programming techniques did you use? 1146 00:55:22,210 --> 00:55:24,370 Actually, yeah [? Minwu, ?] why don't you go ahead. 1147 00:55:24,370 --> 00:55:25,750 How did you approach the problem? 1148 00:55:25,750 --> 00:55:29,170 AUDIENCE: Oh, so for me, I decided to go with an array, 1149 00:55:29,170 --> 00:55:33,610 since I personally don't like working with like 10 variables listed 1150 00:55:33,610 --> 00:55:34,280 at one line. 1151 00:55:34,280 --> 00:55:36,700 So I decided to make an array called number, 1152 00:55:36,700 --> 00:55:39,970 and then set the range so that, for i in range 10, 1153 00:55:39,970 --> 00:55:42,730 I would append each number into the array. 1154 00:55:42,730 --> 00:55:47,770 And I would use the system of using just adding everything in the array 1155 00:55:47,770 --> 00:55:53,677 together, then printing out the final number to make the sum some function. 1156 00:55:53,677 --> 00:55:54,760 BRIAN YU: Yeah, very nice. 1157 00:55:54,760 --> 00:55:57,135 So we haven't actually seen these yet in today's seminar, 1158 00:55:57,135 --> 00:55:59,620 but there are this notion of lists in Python 1159 00:55:59,620 --> 00:56:01,840 that allow you to store a sequence of values. 1160 00:56:01,840 --> 00:56:03,700 And so, as [? Minwu ?] did, you could have 1161 00:56:03,700 --> 00:56:07,090 created a sequence of these 10 values, and then added those 10 values 1162 00:56:07,090 --> 00:56:07,750 together. 1163 00:56:07,750 --> 00:56:13,480 That was definitely one possible option for what you could have done here. 1164 00:56:13,480 --> 00:56:15,730 Any other options that people chose to do, 1165 00:56:15,730 --> 00:56:17,950 other things that you might have tried? 1166 00:56:17,950 --> 00:56:22,660 Let's go to Alexandrina, if you'd like to offer what you tried. 1167 00:56:22,660 --> 00:56:26,630 AUDIENCE: So I [INAUDIBLE] a variable called [INAUDIBLE],, 1168 00:56:26,630 --> 00:56:30,740 and I gave it value of 0 in the beginning. 1169 00:56:30,740 --> 00:56:36,800 Then I have a for loop for i in range of 10. 1170 00:56:36,800 --> 00:56:40,820 For each iteration of the loop, I get a number 1171 00:56:40,820 --> 00:56:46,720 from the user using input, and converting it into an integer. 1172 00:56:46,720 --> 00:56:50,290 And [INAUDIBLE] that I added to the sum. 1173 00:56:50,290 --> 00:56:53,210 Having [INAUDIBLE] for the for loop. 1174 00:56:53,210 --> 00:56:56,170 And then after the for loop, I [INAUDIBLE] the sum. 1175 00:56:56,170 --> 00:56:57,420 BRIAN YU: All right very nice. 1176 00:56:57,420 --> 00:57:00,420 You could have had a variable where you were continuously keeping track, 1177 00:57:00,420 --> 00:57:05,572 as you loop through asking for input, adding to those values as well. 1178 00:57:05,572 --> 00:57:08,030 So that was definitely something you could have tried, too. 1179 00:57:08,030 --> 00:57:11,400 So multiple approaches you could have used to trying to solve this problem. 1180 00:57:11,400 --> 00:57:14,525 So I'll just show you one way that you could have done that, and ultimately 1181 00:57:14,525 --> 00:57:16,560 going to be very similar. 1182 00:57:16,560 --> 00:57:19,880 And definitely, OK if you didn't manage to totally get through the problem. 1183 00:57:19,880 --> 00:57:22,150 Programming takes practice, just takes some getting used to. 1184 00:57:22,150 --> 00:57:24,358 So if you find yourself struggling, not entirely sure 1185 00:57:24,358 --> 00:57:27,470 what to do, just keep up the practice, keep following along, 1186 00:57:27,470 --> 00:57:30,170 and over time you'll start to build an intuition for when 1187 00:57:30,170 --> 00:57:33,030 to use these types of ideas. 1188 00:57:33,030 --> 00:57:36,140 So like Alexandrina suggested, I'm going to start by having a variable. 1189 00:57:36,140 --> 00:57:39,348 I'm just going to call it total, because it's going to keep track of my total 1190 00:57:39,348 --> 00:57:41,710 for each of these 10 times I'm asking for input. 1191 00:57:41,710 --> 00:57:44,000 And if I want to ask for input 10 times, I 1192 00:57:44,000 --> 00:57:50,390 could have an input number line, again and again and again, repeated 10 times. 1193 00:57:50,390 --> 00:57:53,390 But as we've seen before, that's maybe not the best design. 1194 00:57:53,390 --> 00:57:55,610 Maybe if I'm repeating this over and over, 1195 00:57:55,610 --> 00:57:58,440 there's an opportunity there to use a loop, for example. 1196 00:57:58,440 --> 00:58:01,130 And so we've seen that we can use a for loop 1197 00:58:01,130 --> 00:58:07,310 to say something like, for i in range 10, to repeat something 10 times. 1198 00:58:07,310 --> 00:58:10,585 And let me now ask for input from the user. 1199 00:58:10,585 --> 00:58:12,460 That input is going to need to be an integer, 1200 00:58:12,460 --> 00:58:15,110 so I'll convert it into an integer, and I'll 1201 00:58:15,110 --> 00:58:17,843 go ahead and save that inside of a variable, just for now. 1202 00:58:17,843 --> 00:58:19,760 Maybe you didn't do this, but we'll do it just 1203 00:58:19,760 --> 00:58:21,740 to be clear about what we're doing. 1204 00:58:21,740 --> 00:58:23,840 We're asking the user for a number n. 1205 00:58:23,840 --> 00:58:27,740 We're storing that inside of this variable called n. 1206 00:58:27,740 --> 00:58:30,710 And now what I want to do is add this to the total. 1207 00:58:30,710 --> 00:58:34,490 The total starts at 0, but I want to start adding numbers to it now. 1208 00:58:34,490 --> 00:58:38,870 So I could say total equals total plus n. 1209 00:58:38,870 --> 00:58:42,460 Of course, I could shorten that as we've seen before to total plus equals n. 1210 00:58:42,460 --> 00:58:46,370 That's just a shorter way of expressing that same idea. 1211 00:58:46,370 --> 00:58:52,970 And at the very end, I'll go ahead and print out the sum is this total. 1212 00:58:52,970 --> 00:58:55,300 So I start with a total variable that's set to 0. 1213 00:58:55,300 --> 00:58:57,790 And now 10 times, I'm going to repeat the same thing. 1214 00:58:57,790 --> 00:59:02,170 Ask the user to type in a number, store that number inside of the variable n, 1215 00:59:02,170 --> 00:59:05,680 and then add that to my new running total. 1216 00:59:05,680 --> 00:59:09,370 Then at the very end after I've run this code 10 times, 1217 00:59:09,370 --> 00:59:13,000 I'm going to go ahead and print out whatever the sum of all 1218 00:59:13,000 --> 00:59:16,490 of those numbers happens to be. 1219 00:59:16,490 --> 00:59:19,190 So we can try running the program now and see what happens. 1220 00:59:19,190 --> 00:59:21,520 I'll run python sum.py. 1221 00:59:21,520 --> 00:59:26,740 Let's type in 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10. 1222 00:59:26,740 --> 00:59:29,740 And I see that the answer, the sum is 55. 1223 00:59:29,740 --> 00:59:33,260 So I now have the ability to quickly add a whole bunch of numbers together, 1224 00:59:33,260 --> 00:59:36,785 thanks to our Python program and thanks to some loops. 1225 00:59:36,785 --> 00:59:39,410 So if you struggle with that, feel free to take a look at this. 1226 00:59:39,410 --> 00:59:41,837 Feel free to continue to ask questions on ed if there 1227 00:59:41,837 --> 00:59:44,920 are other questions that you have about how you would go about doing this, 1228 00:59:44,920 --> 00:59:46,400 or if there are other approaches. 1229 00:59:46,400 --> 00:59:49,358 And there are definitely other ways you could have solved this problem. 1230 00:59:49,358 --> 00:59:50,140 It's not just one. 1231 00:59:50,140 --> 00:59:52,520 We already heard at least two different ways. 1232 00:59:52,520 --> 00:59:55,820 I'm seeing in the chat, some people that took other approaches as well. 1233 00:59:55,820 --> 01:00:01,360 So definitely multiple ways you could have used to try to solve this problem. 1234 01:00:01,360 --> 01:00:03,880 I saw one question in the chat as to when 1235 01:00:03,880 --> 01:00:08,000 we might want to use a while loop instead of a for loop. 1236 01:00:08,000 --> 01:00:10,180 So a for loop we've seen is very good if I 1237 01:00:10,180 --> 01:00:13,880 know that I want to run this code 10 times, for example. 1238 01:00:13,880 --> 01:00:15,550 When might a while loop come in handy? 1239 01:00:15,550 --> 01:00:19,270 Well, a while loop is particularly useful if you don't necessarily 1240 01:00:19,270 --> 01:00:23,610 know in advance how many times some piece of code is going to run, 1241 01:00:23,610 --> 01:00:27,490 and you want to exit the loop only when something is true, 1242 01:00:27,490 --> 01:00:31,480 or when something happens, you want to exit or break out of that loop. 1243 01:00:31,480 --> 01:00:33,850 So I'm going to show you one additional example of that. 1244 01:00:33,850 --> 01:00:35,900 I'll create a new file. 1245 01:00:35,900 --> 01:00:38,170 I'm going to call this positive.py. 1246 01:00:38,170 --> 01:00:42,610 Let's imagine, for example, that you are writing a program, 1247 01:00:42,610 --> 01:00:45,490 and you want the user to provide you with a positive integer. 1248 01:00:45,490 --> 01:00:48,592 You don't want to allow negative integers, you don't want to allow 0's. 1249 01:00:48,592 --> 01:00:50,300 And this might be the case, for instance, 1250 01:00:50,300 --> 01:00:54,010 if you're writing a program for a bank, and you're writing a program for an ATM 1251 01:00:54,010 --> 01:00:55,720 to allow people to withdraw money. 1252 01:00:55,720 --> 01:00:59,380 You probably only want to allow people to withdraw a positive amount of money 1253 01:00:59,380 --> 01:01:01,390 and not to withdraw a negative amount of money, 1254 01:01:01,390 --> 01:01:03,440 because it probably wouldn't make a lot of sense. 1255 01:01:03,440 --> 01:01:05,898 So you can come up with use cases, where you really do want 1256 01:01:05,898 --> 01:01:08,540 the user to type in a positive integer. 1257 01:01:08,540 --> 01:01:10,220 So how could I do that? 1258 01:01:10,220 --> 01:01:14,200 Well, I want to continue to ask the user to type in a positive integer 1259 01:01:14,200 --> 01:01:16,430 until they give me a positive integer. 1260 01:01:16,430 --> 01:01:21,330 So I can't really say for i in range 10, to ask the user again and again 1261 01:01:21,330 --> 01:01:23,080 like I was doing before with the for loop, 1262 01:01:23,080 --> 01:01:26,710 because I don't necessarily know that it's going to take 10 tries. 1263 01:01:26,710 --> 01:01:29,980 Maybe it takes fewer tries, maybe it takes more tries. 1264 01:01:29,980 --> 01:01:34,210 And so I'm going to introduce some logic that is quite common in Python. 1265 01:01:34,210 --> 01:01:37,750 I'm actually going to have a loop that says while true, 1266 01:01:37,750 --> 01:01:39,400 which might seem a little unusual. 1267 01:01:39,400 --> 01:01:43,420 While true is going to run this loop as long as this condition 1268 01:01:43,420 --> 01:01:45,880 is true, as we've been saying before. 1269 01:01:45,880 --> 01:01:48,285 And therefore, while true is just always going to run. 1270 01:01:48,285 --> 01:01:51,160 It's what we'd call an infinite loop, a loop that's going to go again 1271 01:01:51,160 --> 01:01:52,450 and again and again. 1272 01:01:52,450 --> 01:01:54,650 And normally, that means something would never stop. 1273 01:01:54,650 --> 01:01:58,780 But we'll see in a moment how we can still exit or break out of this loop, 1274 01:01:58,780 --> 01:02:01,373 even though it's running infinitely. 1275 01:02:01,373 --> 01:02:03,290 I'm going to ask the user to type in a number. 1276 01:02:03,290 --> 01:02:05,510 We've seen lines like this several times now, 1277 01:02:05,510 --> 01:02:09,230 but let's ask the user to type in a positive integer. 1278 01:02:09,230 --> 01:02:11,810 And now, when do I want to exit the loop? 1279 01:02:11,810 --> 01:02:14,810 I want to exit the loop as soon as the user gives me a positive integer. 1280 01:02:14,810 --> 01:02:17,685 If they're giving me something else, I need to keep running the loop. 1281 01:02:17,685 --> 01:02:19,760 But as soon as they give me a positive integer, 1282 01:02:19,760 --> 01:02:21,720 I would like to exit this loop. 1283 01:02:21,720 --> 01:02:24,470 So I'm going to say if n is greater than 0, 1284 01:02:24,470 --> 01:02:27,346 we're going to introduce a new statement called break. 1285 01:02:27,346 --> 01:02:33,020 Break just means exit the loop, get out of the loop if n is greater than 0. 1286 01:02:33,020 --> 01:02:36,560 And now at the very end, let's print that variable n. 1287 01:02:36,560 --> 01:02:39,120 1288 01:02:39,120 --> 01:02:43,020 So let's run python positive.py. 1289 01:02:43,020 --> 01:02:46,970 if I type in a negative number, let's say negative 2, 1290 01:02:46,970 --> 01:02:48,678 it asks me to type in a positive integer. 1291 01:02:48,678 --> 01:02:51,137 If I type in another negative integer, it prompts me again. 1292 01:02:51,137 --> 01:02:52,700 If I type in 0, it prompts me again. 1293 01:02:52,700 --> 01:02:56,900 Only once I type in a positive number does the loop exit, 1294 01:02:56,900 --> 01:03:00,920 and then I see that positive number printed out. 1295 01:03:00,920 --> 01:03:03,950 I see someone asking, does the T need to be capitalized? 1296 01:03:03,950 --> 01:03:04,850 Yes it does. 1297 01:03:04,850 --> 01:03:06,530 In Python, these are special keywords. 1298 01:03:06,530 --> 01:03:11,510 Capital T, true, and capital F, false, are just special keywords in Python, 1299 01:03:11,510 --> 01:03:14,450 and the T and the F do, in fact, need to be capitalized 1300 01:03:14,450 --> 01:03:17,480 for that, which might be different from some other programming 1301 01:03:17,480 --> 01:03:20,210 languages you may have used before, if you've programmed before. 1302 01:03:20,210 --> 01:03:24,200 1303 01:03:24,200 --> 01:03:26,190 All right. 1304 01:03:26,190 --> 01:03:31,540 So let's go ahead and try something else. 1305 01:03:31,540 --> 01:03:33,970 Let's try another example of a loop. 1306 01:03:33,970 --> 01:03:36,057 So it is CS50's New Year's seminars. 1307 01:03:36,057 --> 01:03:37,140 We just passed a new year. 1308 01:03:37,140 --> 01:03:40,060 And tradition on New Year's is to count down to the New Year. 1309 01:03:40,060 --> 01:03:43,500 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Happy New Year. 1310 01:03:43,500 --> 01:03:47,220 So let's try and write a Python program that does just that, counts 1311 01:03:47,220 --> 01:03:48,400 down to the Happy New Year. 1312 01:03:48,400 --> 01:03:50,340 We're a few days late, but that's OK. 1313 01:03:50,340 --> 01:03:51,650 I'll create a new file. 1314 01:03:51,650 --> 01:03:54,930 We'll call it countdown.py. 1315 01:03:54,930 --> 01:03:58,000 And if I wanted to count down from 10, what could I do? 1316 01:03:58,000 --> 01:04:05,720 Well, I could do print(10), then print(9), then print(8), then print(7). 1317 01:04:05,720 --> 01:04:09,120 And I could do this again and again, printing all the way down to 1. 1318 01:04:09,120 --> 01:04:11,370 But this, again, probably not the best design. 1319 01:04:11,370 --> 01:04:12,600 I'm repeating myself a lot. 1320 01:04:12,600 --> 01:04:14,580 There's lots of different print lines. 1321 01:04:14,580 --> 01:04:18,520 So I'd like to be able to do this a little bit better. 1322 01:04:18,520 --> 01:04:19,830 And so how might I do that? 1323 01:04:19,830 --> 01:04:24,115 How could I solve this problem where I find myself repeating myself? 1324 01:04:24,115 --> 01:04:26,523 1325 01:04:26,523 --> 01:04:28,690 Yeah, I see a bunch of people in the chat suggesting 1326 01:04:28,690 --> 01:04:30,640 some kind of a loop, where we can use a loop 1327 01:04:30,640 --> 01:04:32,470 to repeat something again and again. 1328 01:04:32,470 --> 01:04:38,110 And so if I say for i in range 10, print i. 1329 01:04:38,110 --> 01:04:40,480 Remember that when I say for i in range 10, 1330 01:04:40,480 --> 01:04:42,670 range 10 is going to give me a range of 10 numbers. 1331 01:04:42,670 --> 01:04:43,060 Perfect. 1332 01:04:43,060 --> 01:04:44,185 That's exactly what I want. 1333 01:04:44,185 --> 01:04:45,940 I want a range of 10 numbers. 1334 01:04:45,940 --> 01:04:48,940 And it's going to loop over each one of those numbers, 1335 01:04:48,940 --> 01:04:52,393 and I'm going to print out that value, i. 1336 01:04:52,393 --> 01:04:54,310 And so I'll go ahead and do that, printing out 1337 01:04:54,310 --> 01:04:56,320 all the values in those number range. 1338 01:04:56,320 --> 01:05:01,640 And at the very end, let's print("Happy New Year!"). 1339 01:05:01,640 --> 01:05:03,872 So I have a for loop to print out 10 numbers, 1340 01:05:03,872 --> 01:05:05,830 and then I'm going to print out Happy New Year. 1341 01:05:05,830 --> 01:05:07,997 What's going to happen if I try to run this program? 1342 01:05:07,997 --> 01:05:09,460 Anyone have a guess? 1343 01:05:09,460 --> 01:05:15,450 What am I going to see if I try and run this program? 1344 01:05:15,450 --> 01:05:16,030 See an error. 1345 01:05:16,030 --> 01:05:17,280 I'm not going to get an error. 1346 01:05:17,280 --> 01:05:21,550 It's not going to crash, but it still might not be exactly what I want. 1347 01:05:21,550 --> 01:05:23,443 Yeah, it's going to start counting up. 1348 01:05:23,443 --> 01:05:26,110 When we're counting on a for loop, we're generally starting at 0 1349 01:05:26,110 --> 01:05:27,530 and moving forward. 1350 01:05:27,530 --> 01:05:31,150 And so if I say python countdown.py, here's what I get-- 1351 01:05:31,150 --> 01:05:35,710 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, Happy New Year. 1352 01:05:35,710 --> 01:05:38,260 Maybe not the way most people ring in the New Year. 1353 01:05:38,260 --> 01:05:39,500 Why did this happen? 1354 01:05:39,500 --> 01:05:44,980 Well I got 10 numbers, but by default loops in Python are going to start at 0 1355 01:05:44,980 --> 01:05:47,200 and then count up. 1356 01:05:47,200 --> 01:05:47,950 We get 10 numbers. 1357 01:05:47,950 --> 01:05:52,330 0 through 9 is 10 total numbers, from 0 all the way up through 9. 1358 01:05:52,330 --> 01:05:54,940 So how could I solve this problem? 1359 01:05:54,940 --> 01:05:57,360 I want for it to count down. 1360 01:05:57,360 --> 01:05:59,990 And again, there are multiple ways I could solve this problem. 1361 01:05:59,990 --> 01:06:03,240 But I'd like a way that's not going to involve any new syntax that we've never 1362 01:06:03,240 --> 01:06:04,810 seen before. 1363 01:06:04,810 --> 01:06:07,960 How can I maybe add a little bit of math to try and solve this problem? 1364 01:06:07,960 --> 01:06:09,490 I see some suggestions. 1365 01:06:09,490 --> 01:06:11,200 And I see Michael with a great suggestion 1366 01:06:11,200 --> 01:06:14,590 in the chat, which is instead of printing i, 1367 01:06:14,590 --> 01:06:17,633 I'm going to print 10 minus i. 1368 01:06:17,633 --> 01:06:20,050 All right, that seems a little weird, but what's going on? 1369 01:06:20,050 --> 01:06:22,690 Well originally, we printed out the number 0. 1370 01:06:22,690 --> 01:06:23,920 What's 10 minus 0? 1371 01:06:23,920 --> 01:06:27,970 Well that's just [? 0. ?] Then our code originally printed out 1. 1372 01:06:27,970 --> 01:06:28,870 What's 10 minus 1? 1373 01:06:28,870 --> 01:06:29,710 Well, that's 9. 1374 01:06:29,710 --> 01:06:31,570 OK so we went 10, 9. 1375 01:06:31,570 --> 01:06:33,220 Next, we were printing out 2. 1376 01:06:33,220 --> 01:06:34,270 10 minus 2 is 8. 1377 01:06:34,270 --> 01:06:35,470 So we're printing out 8. 1378 01:06:35,470 --> 01:06:38,050 So on and so forth, all the way down to 9. 1379 01:06:38,050 --> 01:06:39,010 What's 10 minus 9? 1380 01:06:39,010 --> 01:06:44,170 Well that's 1, and so at that point we could say 10 through 1, Happy New Year. 1381 01:06:44,170 --> 01:06:46,070 Let's give that a try. 1382 01:06:46,070 --> 01:06:51,390 I'll run Python countdown.py, and now we get what we want 10, 9, 8, 7, 6, 5, 4, 1383 01:06:51,390 --> 01:06:53,670 3, 2, 1, Happy New Year. 1384 01:06:53,670 --> 01:06:55,630 So we've counted down from 10. 1385 01:06:55,630 --> 01:06:57,140 There are other ways of doing this. 1386 01:06:57,140 --> 01:06:59,015 I see someone solving this with a while loop. 1387 01:06:59,015 --> 01:07:02,040 I see someone that's adding some additional arguments to range. 1388 01:07:02,040 --> 01:07:04,152 There are definitely, as always, multiple ways 1389 01:07:04,152 --> 01:07:06,110 that you could have used to solve this problem. 1390 01:07:06,110 --> 01:07:09,720 This is just one way that you can do, using subtraction 1391 01:07:09,720 --> 01:07:17,120 to, instead of counting from 0 up to 9, counting from 10 all the way down to 1. 1392 01:07:17,120 --> 01:07:20,540 And so this is some pretty useful logic that we've managed to now figure out. 1393 01:07:20,540 --> 01:07:23,150 We've managed to figure out some code that can count 1394 01:07:23,150 --> 01:07:26,900 down from 10 all the way down to 1. 1395 01:07:26,900 --> 01:07:31,350 And in fact, this type of logic we might want to use multiple times. 1396 01:07:31,350 --> 01:07:36,627 Imagine that maybe we're writing a program that can count down to the New 1397 01:07:36,627 --> 01:07:38,210 Year in multiple different time zones. 1398 01:07:38,210 --> 01:07:40,340 So first on the East Coast of the United States, 1399 01:07:40,340 --> 01:07:42,680 we're counting down saying Happy New Year, then on the West Coast 1400 01:07:42,680 --> 01:07:44,722 we want to do a countdown and say Happy New Year. 1401 01:07:44,722 --> 01:07:48,740 This code that we've written, that counts down from 10, is pretty useful. 1402 01:07:48,740 --> 01:07:52,910 Maybe I would like to be able to use that code multiple times. 1403 01:07:52,910 --> 01:07:55,940 And so oftentimes, what you'll see in programming 1404 01:07:55,940 --> 01:07:59,000 is that when there's some code, when there's some logic that you might 1405 01:07:59,000 --> 01:08:03,495 want to reuse, we can package that code inside of something called a function. 1406 01:08:03,495 --> 01:08:05,370 And we've seen examples of functions already. 1407 01:08:05,370 --> 01:08:08,390 We've seen the print function that prints out text to the terminal. 1408 01:08:08,390 --> 01:08:11,520 We've seen the input function that asks for input from the user. 1409 01:08:11,520 --> 01:08:16,310 But it turns out that we can create our own functions that we can then 1410 01:08:16,310 --> 01:08:17,899 use again in a program. 1411 01:08:17,899 --> 01:08:20,000 We can use that code multiple times. 1412 01:08:20,000 --> 01:08:25,010 So I'm going to show you now how you could create a function. 1413 01:08:25,010 --> 01:08:26,790 And so here's what I'm going to do. 1414 01:08:26,790 --> 01:08:28,998 Let me get rid of the print Happy New Year right now. 1415 01:08:28,998 --> 01:08:32,189 I'm just focused on how do you write a function that counts down, 1416 01:08:32,189 --> 01:08:33,870 and here's how you do it. 1417 01:08:33,870 --> 01:08:37,790 Up at the very top, I'm going to say def countdown. 1418 01:08:37,790 --> 01:08:40,380 d-e-f is short for define. 1419 01:08:40,380 --> 01:08:45,252 And it's the keyword we use when we want to define or create a new function. 1420 01:08:45,252 --> 01:08:47,460 So we're going to define a function called countdown. 1421 01:08:47,460 --> 01:08:50,939 We're going to include parentheses to indicate that it is a function. 1422 01:08:50,939 --> 01:08:53,145 Inputs to that function can go in those parentheses. 1423 01:08:53,145 --> 01:08:54,270 We'll see that in a moment. 1424 01:08:54,270 --> 01:08:55,960 For now, we'll leave it empty. 1425 01:08:55,960 --> 01:08:59,819 And now anything inside the function needs to be indented. 1426 01:08:59,819 --> 01:09:03,330 So I'm going to indent both of these lines inside the function. 1427 01:09:03,330 --> 01:09:07,620 What you'll notice now is that this print is actually indented twice. 1428 01:09:07,620 --> 01:09:09,960 It's indented inside the function, and then it's 1429 01:09:09,960 --> 01:09:12,870 indented again inside of the for loop. 1430 01:09:12,870 --> 01:09:19,240 But I now have a function called countdown that can loop 10 times 1431 01:09:19,240 --> 01:09:24,620 and give me a countdown that's counting down from 10 all the way down to 1. 1432 01:09:24,620 --> 01:09:26,840 And now that I'm introducing functions, I'm 1433 01:09:26,840 --> 01:09:31,020 going to add one more function actually that's called my main function. 1434 01:09:31,020 --> 01:09:33,020 In some programming languages, this is required. 1435 01:09:33,020 --> 01:09:34,370 In Python, it's not. 1436 01:09:34,370 --> 01:09:37,370 But my main function is just going to be the function that I want 1437 01:09:37,370 --> 01:09:39,399 to run when I start running my program. 1438 01:09:39,399 --> 01:09:41,899 So what do I want to happen when I start running my program? 1439 01:09:41,899 --> 01:09:45,620 Well, I'm going to define my main function, again with that def keyword, 1440 01:09:45,620 --> 01:09:47,720 to define a new function. 1441 01:09:47,720 --> 01:09:50,740 And what I'm going to do in my main function is say go ahead and count 1442 01:09:50,740 --> 01:09:55,070 down, and then print("Happy New Year!"). 1443 01:09:55,070 --> 01:09:57,620 So my main function is running, otherwise known 1444 01:09:57,620 --> 01:10:00,210 as calling, two functions. 1445 01:10:00,210 --> 01:10:02,240 We're first calling the countdown function, 1446 01:10:02,240 --> 01:10:05,660 which is the function that I wrote that's going to count down from 10. 1447 01:10:05,660 --> 01:10:10,150 And then we're going to print Happy New Year. 1448 01:10:10,150 --> 01:10:13,910 And now at the very bottom, after I've defined my functions, 1449 01:10:13,910 --> 01:10:18,550 let's just run my main function by typing main and then parentheses 1450 01:10:18,550 --> 01:10:20,310 to actually run that function. 1451 01:10:20,310 --> 01:10:23,060 So this is now more sophisticated than the code we've seen so far. 1452 01:10:23,060 --> 01:10:24,340 So we'll go through it again. 1453 01:10:24,340 --> 01:10:26,290 I've defined two functions. 1454 01:10:26,290 --> 01:10:29,260 This countdown function is defined with this def 1455 01:10:29,260 --> 01:10:33,440 keyword to run that code that counts down, much as we've seen before. 1456 01:10:33,440 --> 01:10:37,570 And then the main function is just going to run the countdown function 1457 01:10:37,570 --> 01:10:39,850 and then print Happy New Year. 1458 01:10:39,850 --> 01:10:43,157 And so now, I can run this countdown program. 1459 01:10:43,157 --> 01:10:44,740 And right now, it does the same thing. 1460 01:10:44,740 --> 01:10:46,990 I've organized it a little better, I would argue. 1461 01:10:46,990 --> 01:10:48,530 But the effect is the same. 1462 01:10:48,530 --> 01:10:51,100 It's going to be 10 all the way down to 1. 1463 01:10:51,100 --> 01:10:55,810 And then at the very end it says Happy New Year. 1464 01:10:55,810 --> 01:10:58,960 But the nice thing about functions is that functions 1465 01:10:58,960 --> 01:11:00,380 have the ability to take input. 1466 01:11:00,380 --> 01:11:02,110 And that's one of the great advantages of functions. 1467 01:11:02,110 --> 01:11:04,210 They don't have to do the same thing every time. 1468 01:11:04,210 --> 01:11:08,000 They can take input and do different things based on that input. 1469 01:11:08,000 --> 01:11:10,785 For example, the print function takes input, which is this string. 1470 01:11:10,785 --> 01:11:13,660 And we've been able to make that print function say different things. 1471 01:11:13,660 --> 01:11:14,710 We made it say hello. 1472 01:11:14,710 --> 01:11:16,210 We made it say positive or negative. 1473 01:11:16,210 --> 01:11:18,440 We made it say the sum of a whole bunch of numbers. 1474 01:11:18,440 --> 01:11:22,450 Now, we've made it say Happy New Year, just based on what that input is. 1475 01:11:22,450 --> 01:11:26,353 And my countdown function, it can take some input as well. 1476 01:11:26,353 --> 01:11:28,020 So I'm going to make it take some input. 1477 01:11:28,020 --> 01:11:29,312 You can give that input a name. 1478 01:11:29,312 --> 01:11:31,650 I'll call the input n. 1479 01:11:31,650 --> 01:11:33,630 And now I'm going to want my countdown function 1480 01:11:33,630 --> 01:11:38,250 not to count down from 10 every time, but to count down from whatever n is. 1481 01:11:38,250 --> 01:11:42,278 And n might be 10 but it might be some other number instead. 1482 01:11:42,278 --> 01:11:45,570 And so what I'm going to do is, instead of saying 10 everywhere in my function, 1483 01:11:45,570 --> 01:11:50,820 I'm going to replace all of my 10's with just this number n. 1484 01:11:50,820 --> 01:11:54,060 So now, my countdown function is a little bit more flexible. 1485 01:11:54,060 --> 01:11:57,930 It's a little more adaptable to whatever I want to count down from. 1486 01:11:57,930 --> 01:12:03,455 I could say countdown(10), providing 10 as the input to the function. 1487 01:12:03,455 --> 01:12:06,330 And that would give me the same thing I've seen before, counting down 1488 01:12:06,330 --> 01:12:08,220 from 10, saying Happy New Year. 1489 01:12:08,220 --> 01:12:11,690 But all I need to do to change the way the function behaves 1490 01:12:11,690 --> 01:12:13,530 is change the input to the function. 1491 01:12:13,530 --> 01:12:16,080 I don't have to change the countdown function at all. 1492 01:12:16,080 --> 01:12:21,150 If I instead want to count down from 5, I just change the 10 to a 5. 1493 01:12:21,150 --> 01:12:24,410 And now, 5 is the input to my countdown function. 1494 01:12:24,410 --> 01:12:29,100 And now, we see a 5, 4, 3, 2, 1 and then Happy New Year. 1495 01:12:29,100 --> 01:12:32,210 1496 01:12:32,210 --> 01:12:34,180 And so this now shows some of the flexibility 1497 01:12:34,180 --> 01:12:37,633 we get with functions, functions that can take input and do something 1498 01:12:37,633 --> 01:12:38,300 with that input. 1499 01:12:38,300 --> 01:12:40,990 In this case, my countdown function is deciding 1500 01:12:40,990 --> 01:12:45,908 what numbers to print, based on whatever the input to that function is. 1501 01:12:45,908 --> 01:12:48,700 So a lot of new syntax, a little bit more complex than the programs 1502 01:12:48,700 --> 01:12:49,540 we've see. 1503 01:12:49,540 --> 01:12:52,220 I'll pause for questions. 1504 01:12:52,220 --> 01:12:55,240 [? Feddy ?] is asking why do I need main at the very end? 1505 01:12:55,240 --> 01:12:58,510 The reason why is because defining our functions 1506 01:12:58,510 --> 01:13:00,920 doesn't actually run those functions. 1507 01:13:00,920 --> 01:13:04,180 It's just saying this is what the main function is, 1508 01:13:04,180 --> 01:13:06,430 and this is what the countdown function is, 1509 01:13:06,430 --> 01:13:08,380 but neither function is going to run yet. 1510 01:13:08,380 --> 01:13:12,040 If I didn't have this main line at the very end, when I run my countdown 1511 01:13:12,040 --> 01:13:14,290 program, nothing will happen. 1512 01:13:14,290 --> 01:13:16,900 I've defined the functions, but I haven't actually 1513 01:13:16,900 --> 01:13:18,560 run those functions yet. 1514 01:13:18,560 --> 01:13:23,800 So I need to at the end say main, to say I would like to actually now run 1515 01:13:23,800 --> 01:13:25,930 this main function, and that is going to have 1516 01:13:25,930 --> 01:13:30,380 the effect of giving me the countdown. 1517 01:13:30,380 --> 01:13:32,810 [? Uliana ?] is asking, do I need to define main? 1518 01:13:32,810 --> 01:13:37,310 Yes, you do, and I did so on line 1 here, again with this def keyword. 1519 01:13:37,310 --> 01:13:41,630 Anytime I want to define a new function, I use the def keyword, 1520 01:13:41,630 --> 01:13:43,340 followed by the name of the function. 1521 01:13:43,340 --> 01:13:46,550 In the parentheses go any inputs to that function, if there are inputs, 1522 01:13:46,550 --> 01:13:48,030 but they're not necessary. 1523 01:13:48,030 --> 01:13:50,250 And then the colon, as we saw with conditions 1524 01:13:50,250 --> 01:13:53,660 and as we saw with loops introduces the block of code 1525 01:13:53,660 --> 01:13:55,970 that I would like inside of that function. 1526 01:13:55,970 --> 01:13:58,765 1527 01:13:58,765 --> 01:14:00,890 You could have named these functions anything else. 1528 01:14:00,890 --> 01:14:02,682 I see a couple of people asking about that. 1529 01:14:02,682 --> 01:14:06,223 They didn't need to be given these names, specifically. 1530 01:14:06,223 --> 01:14:11,910 1531 01:14:11,910 --> 01:14:15,150 Oh I see someone asking, how do you add comments to your program? 1532 01:14:15,150 --> 01:14:18,330 And so a comment is just some English text, 1533 01:14:18,330 --> 01:14:22,410 some description of what it is your program is doing. 1534 01:14:22,410 --> 01:14:25,650 The computer, the program that's running your Python program, 1535 01:14:25,650 --> 01:14:30,300 it doesn't really care about what those comments actually are. 1536 01:14:30,300 --> 01:14:33,270 It's just going to be a way for you, the person writing the code, 1537 01:14:33,270 --> 01:14:36,030 and someone reading the code to be able to better understand it. 1538 01:14:36,030 --> 01:14:40,080 To add a comment to your code, you use the hash mark, so the hash 1539 01:14:40,080 --> 01:14:41,400 or the pound symbol. 1540 01:14:41,400 --> 01:14:45,180 And then you can just write some English text, like count down to the new year. 1541 01:14:45,180 --> 01:14:46,890 Oftentimes, you use comments every couple 1542 01:14:46,890 --> 01:14:50,070 of lines just to describe what it is that your code is doing. 1543 01:14:50,070 --> 01:14:52,410 Again, the computer doesn't care about the comments. 1544 01:14:52,410 --> 01:14:56,550 It's just going to ignore them But it's good style, good practice for you. 1545 01:14:56,550 --> 01:15:00,480 And it makes it easier to read someone else's code, if you can read their code 1546 01:15:00,480 --> 01:15:04,050 and get a sense for what it is that their code is doing. 1547 01:15:04,050 --> 01:15:07,860 1548 01:15:07,860 --> 01:15:08,360 All right. 1549 01:15:08,360 --> 01:15:13,670 So right now, our program is doing the same thing every time. 1550 01:15:13,670 --> 01:15:17,390 Every time we run it, it's going to count down from 5 1551 01:15:17,390 --> 01:15:19,400 and then say Happy New Year, for example. 1552 01:15:19,400 --> 01:15:22,640 Sometimes, and we'll see an example of this in just a moment, 1553 01:15:22,640 --> 01:15:26,000 we might want our programs to have a little bit of randomness, 1554 01:15:26,000 --> 01:15:31,130 to not always do the same thing every time but to sometimes do one thing 1555 01:15:31,130 --> 01:15:34,290 and to sometimes do something else. 1556 01:15:34,290 --> 01:15:38,000 And so I would like maybe now, just for fun, to surprise myself. 1557 01:15:38,000 --> 01:15:41,300 I don't want always come down from 5, maybe sometimes count them from 5 maybe 1558 01:15:41,300 --> 01:15:43,910 sometimes come down from 10, maybe sometimes come down 1559 01:15:43,910 --> 01:15:45,080 from a different number. 1560 01:15:45,080 --> 01:15:49,970 Let's add, just for fun, a little bit of randomness into this program. 1561 01:15:49,970 --> 01:15:52,948 Now, how do I get a random number in Python? 1562 01:15:52,948 --> 01:15:54,740 Well, it turns out Python has a whole bunch 1563 01:15:54,740 --> 01:15:58,760 of built-in modules, or libraries, things that I can use inside 1564 01:15:58,760 --> 01:16:01,090 of my code that have already been written, 1565 01:16:01,090 --> 01:16:03,530 that just give me easy access to some functionality 1566 01:16:03,530 --> 01:16:05,930 that I might want as a Python programmer. 1567 01:16:05,930 --> 01:16:10,640 And it turns out that one of those modules is called the random module. 1568 01:16:10,640 --> 01:16:12,770 And so how do I use the random module? 1569 01:16:12,770 --> 01:16:16,220 Well, what I need to do at the top, and we haven't seen anything like this yet, 1570 01:16:16,220 --> 01:16:19,025 is import random. 1571 01:16:19,025 --> 01:16:21,620 Random is an already existing module in Python. 1572 01:16:21,620 --> 01:16:25,260 You could write your own module if you wanted to, but it already exists. 1573 01:16:25,260 --> 01:16:29,990 And I would just like to import it, meaning include it inside of my program 1574 01:16:29,990 --> 01:16:33,650 so that I have access to that random module. 1575 01:16:33,650 --> 01:16:36,590 And now, how do I use the random module? 1576 01:16:36,590 --> 01:16:39,170 And I would only know this by looking up the documentation, 1577 01:16:39,170 --> 01:16:42,320 in other words, the instructions for how to use the random module. 1578 01:16:42,320 --> 01:16:46,550 But there is a function called random.randint 1579 01:16:46,550 --> 01:16:48,290 that gets me a random integer. 1580 01:16:48,290 --> 01:16:54,380 And lets say I want a random integer between 5 and 15. 1581 01:16:54,380 --> 01:16:58,220 So it's going to give me a random number between 5 and 15 1582 01:16:58,220 --> 01:17:00,760 using this random.randint function. 1583 01:17:00,760 --> 01:17:03,540 And I'm going to save that random number inside of a variable. 1584 01:17:03,540 --> 01:17:06,330 I'll call it number. 1585 01:17:06,330 --> 01:17:11,970 And so now, I have a variable that is some random number between 5 and 15. 1586 01:17:11,970 --> 01:17:16,310 And now instead of counting down from 5, let me count down 1587 01:17:16,310 --> 01:17:18,515 from whatever that number is. 1588 01:17:18,515 --> 01:17:21,140 Whatever that number is, that's what I want to count down from. 1589 01:17:21,140 --> 01:17:24,320 And so I don't know yet before I run the program what it's going to be. 1590 01:17:24,320 --> 01:17:27,140 The program is going to surprise me by picking a random number 1591 01:17:27,140 --> 01:17:30,940 and then counting down to that random number. 1592 01:17:30,940 --> 01:17:36,160 So let's give it a try, python countdown.py. 1593 01:17:36,160 --> 01:17:37,600 This time, it counted down from 5. 1594 01:17:37,600 --> 01:17:40,267 So it just so happened to pick a random number between 5 and 15. 1595 01:17:40,267 --> 01:17:41,500 It picked 5. 1596 01:17:41,500 --> 01:17:44,935 But if I try running it again, this time it counted down from 8. 1597 01:17:44,935 --> 01:17:49,020 It counted down 8, 7, 6, 5, 4, 3, 2, 1, Happy New Year. 1598 01:17:49,020 --> 01:17:51,365 I'll try one more time, just for fun with 8 again. 1599 01:17:51,365 --> 01:17:52,740 Sometimes you get to same number. 1600 01:17:52,740 --> 01:17:54,520 I try it again, this time it's 5 again. 1601 01:17:54,520 --> 01:17:57,670 So every time I try it, I might get different values. 1602 01:17:57,670 --> 01:18:00,400 This time it counted down from 15, and so this 1603 01:18:00,400 --> 01:18:06,320 allows myself to have a little bit of randomness in the program as well. 1604 01:18:06,320 --> 01:18:08,320 So this is probably the most complex program 1605 01:18:08,320 --> 01:18:10,510 we've now written so far in today's seminar. 1606 01:18:10,510 --> 01:18:13,000 I'll pause here for questions about functions, 1607 01:18:13,000 --> 01:18:18,780 about imports, about variables, random numbers. 1608 01:18:18,780 --> 01:18:20,835 Multi-line comments? 1609 01:18:20,835 --> 01:18:22,710 We haven't seen any multi-line comments here. 1610 01:18:22,710 --> 01:18:25,437 But you can do so by using three quotation marks. 1611 01:18:25,437 --> 01:18:26,520 It's a little bit strange. 1612 01:18:26,520 --> 01:18:32,453 But if you have three quotation marks, you can have a multi-line comment here, 1613 01:18:32,453 --> 01:18:34,245 and that allows you to span multiple lines. 1614 01:18:34,245 --> 01:18:40,970 1615 01:18:40,970 --> 01:18:44,150 Can functions return values in Python? 1616 01:18:44,150 --> 01:18:45,560 Yes, they can. 1617 01:18:45,560 --> 01:18:48,940 We'll see some examples of that a little bit later today, in fact. 1618 01:18:48,940 --> 01:18:51,440 We haven't yet seen examples of that with our own functions. 1619 01:18:51,440 --> 01:18:55,400 But for example, the randint function is returning a value. 1620 01:18:55,400 --> 01:18:57,260 It's giving you back a number. 1621 01:18:57,260 --> 01:19:02,840 And we are then storing that value inside of this variable number. 1622 01:19:02,840 --> 01:19:05,810 Adam is asking how do I memorize all these libraries? 1623 01:19:05,810 --> 01:19:07,940 In short, you don't. 1624 01:19:07,940 --> 01:19:11,060 I definitely don't have all the libraries memorized. 1625 01:19:11,060 --> 01:19:12,080 I know a couple of them. 1626 01:19:12,080 --> 01:19:15,050 I know how to use random numbers because I use those kind of often, 1627 01:19:15,050 --> 01:19:17,360 and a couple of others that I use pretty frequently. 1628 01:19:17,360 --> 01:19:19,730 Ultimately, if you want to know how to use a library, 1629 01:19:19,730 --> 01:19:22,280 you can look to the documentation, the instructions 1630 01:19:22,280 --> 01:19:23,940 for how to use that library. 1631 01:19:23,940 --> 01:19:26,540 And Python has a pretty extensive documentation 1632 01:19:26,540 --> 01:19:30,920 that explains exactly how each of those libraries works. 1633 01:19:30,920 --> 01:19:33,810 So you can use that as a reference material for knowing how to do it. 1634 01:19:33,810 --> 01:19:38,190 1635 01:19:38,190 --> 01:19:39,750 All right. 1636 01:19:39,750 --> 01:19:42,750 So let's go ahead and try another exercise, an opportunity for you 1637 01:19:42,750 --> 01:19:46,400 to try something out for a little bit and get some practice 1638 01:19:46,400 --> 01:19:48,150 with writing some Python code of your own. 1639 01:19:48,150 --> 01:19:50,817 You'll get some practice with loops and a little bit of practice 1640 01:19:50,817 --> 01:19:53,280 with some random number generation, too. 1641 01:19:53,280 --> 01:19:56,280 And so it'll be a little more complex than the last exercise 1642 01:19:56,280 --> 01:19:57,612 I had you work on. 1643 01:19:57,612 --> 01:19:59,320 But we're going to have you build a game. 1644 01:19:59,320 --> 01:20:01,650 And this is going to be a guessing game. 1645 01:20:01,650 --> 01:20:04,920 What I want you to do is write a program that picks a pseudorandom number, 1646 01:20:04,920 --> 01:20:09,180 or a random number between 1 and 10, and then give the user three chances 1647 01:20:09,180 --> 01:20:12,420 to guess the number, each time printing some information, 1648 01:20:12,420 --> 01:20:15,850 letting the user know whether their guess was correct or not. 1649 01:20:15,850 --> 01:20:20,250 So your computer is going to pick a random number between 1 and 10, 1650 01:20:20,250 --> 01:20:23,170 and the user gets three chances to guess the number. 1651 01:20:23,170 --> 01:20:25,750 And so if the number is 2 for example, you might guess 8. 1652 01:20:25,750 --> 01:20:27,000 And it tells you you're wrong. 1653 01:20:27,000 --> 01:20:29,000 And maybe now you guess 4, and you get it wrong. 1654 01:20:29,000 --> 01:20:31,482 But then if you guess 2, well, then you get it right. 1655 01:20:31,482 --> 01:20:32,940 And the computer will let you know. 1656 01:20:32,940 --> 01:20:36,540 If you want a little bit of an extra challenge, maybe give the user a hint. 1657 01:20:36,540 --> 01:20:39,820 Let them know if their number is a little too big or a little too small, 1658 01:20:39,820 --> 01:20:40,950 for example. 1659 01:20:40,950 --> 01:20:43,140 But start by just maybe just letting the user 1660 01:20:43,140 --> 01:20:46,570 know whether their task was correct or not. 1661 01:20:46,570 --> 01:20:49,290 You'll probably find that you might want to use it 1662 01:20:49,290 --> 01:20:53,310 this random.randit function to generate a random number, which 1663 01:20:53,310 --> 01:20:57,270 you can access by importing random at the top of your program. 1664 01:20:57,270 --> 01:21:02,220 And maybe you want some kind of loop to let the user guess multiple times as 1665 01:21:02,220 --> 01:21:03,792 to what that number is. 1666 01:21:03,792 --> 01:21:06,000 So we'll give you a little more time to work on this. 1667 01:21:06,000 --> 01:21:08,880 We'll give you maybe 15 minutes to try and write 1668 01:21:08,880 --> 01:21:10,830 this guessing game for yourself. 1669 01:21:10,830 --> 01:21:13,800 If you have questions, again, about how to go about building this game 1670 01:21:13,800 --> 01:21:16,350 or if you get stuck at a particular point in your code, 1671 01:21:16,350 --> 01:21:18,827 ask a question on the ed discussion forum 1672 01:21:18,827 --> 01:21:20,910 where you can post the code you've written so far. 1673 01:21:20,910 --> 01:21:22,660 And again, we have a great team of mentors 1674 01:21:22,660 --> 01:21:25,170 who were here to help answer your questions and help you out 1675 01:21:25,170 --> 01:21:28,290 as you go about trying to write this program, too. 1676 01:21:28,290 --> 01:21:31,900 Valerie, again, has pasted the link to ed in the chat. 1677 01:21:31,900 --> 01:21:34,050 And in about 15 minutes, we'll reconvene and we'll 1678 01:21:34,050 --> 01:21:39,060 talk about how it is that you could have built this guessing game program. 1679 01:21:39,060 --> 01:21:41,190 All right welcome back, everyone. 1680 01:21:41,190 --> 01:21:43,680 Definitely OK if you didn't manage to finish that entirely. 1681 01:21:43,680 --> 01:21:47,090 But hopefully, you were able to make at least a little bit of progress. 1682 01:21:47,090 --> 01:21:49,350 What I thought we'd do now is see if anyone wants 1683 01:21:49,350 --> 01:21:51,580 to offer their approach to the problem. 1684 01:21:51,580 --> 01:21:53,890 How did you try to solve the problem? 1685 01:21:53,890 --> 01:21:57,960 What sorts of programming ideas or constructs were you using? 1686 01:21:57,960 --> 01:21:58,590 Let's see. 1687 01:21:58,590 --> 01:22:04,670 Let's go to [? Rami. ?] If you'd like to offer your idea. 1688 01:22:04,670 --> 01:22:06,250 AUDIENCE: Yeah, hello. 1689 01:22:06,250 --> 01:22:13,170 As first I used two f, two main functions, main and then as a game. 1690 01:22:13,170 --> 01:22:17,330 For main at first, I chose a random number between 1 and 100. 1691 01:22:17,330 --> 01:22:21,390 And I gave a variable named chance and number 3. 1692 01:22:21,390 --> 01:22:23,540 Then I do a while loop. 1693 01:22:23,540 --> 01:22:28,830 What are the chances bigger than 0, ask the player to give a input. 1694 01:22:28,830 --> 01:22:30,190 This is a number. 1695 01:22:30,190 --> 01:22:36,090 Then if this number is bigger than the number that I chose, 1696 01:22:36,090 --> 01:22:38,260 then it says if that number is bigger. 1697 01:22:38,260 --> 01:22:39,750 That's the hint. 1698 01:22:39,750 --> 01:22:43,140 If it's smaller, it says the number is smaller. 1699 01:22:43,140 --> 01:22:45,930 And if equal, it says the number is correct. 1700 01:22:45,930 --> 01:22:48,640 And the other function it goes another game. 1701 01:22:48,640 --> 01:22:54,100 Here I ask the player if you want to play another game, they put yes, y. 1702 01:22:54,100 --> 01:22:55,410 If not, they put no. 1703 01:22:55,410 --> 01:22:56,730 No, it breaks the loop. 1704 01:22:56,730 --> 01:23:00,050 Yes, it recalls the main function another time. 1705 01:23:00,050 --> 01:23:01,300 BRIAN YU: All right very nice. 1706 01:23:01,300 --> 01:23:03,842 So you even add an additional feature that we didn't require, 1707 01:23:03,842 --> 01:23:06,700 the ability to play again, which maybe is a good feature to add. 1708 01:23:06,700 --> 01:23:08,880 And this is going to be true of a lot of the programs you might write. 1709 01:23:08,880 --> 01:23:11,082 You might write a program and realize down the line 1710 01:23:11,082 --> 01:23:13,540 that maybe you want to add another feature to that program, 1711 01:23:13,540 --> 01:23:16,360 and you can keep building on top of what you've already built. 1712 01:23:16,360 --> 01:23:20,230 But I like these big ideas, using a random.randit to try and generate 1713 01:23:20,230 --> 01:23:23,560 a random number, and then using some kind of a loop 1714 01:23:23,560 --> 01:23:27,190 to repeatedly prompt the user to give them a certain number of chances 1715 01:23:27,190 --> 01:23:28,880 to try and get the right answer. 1716 01:23:28,880 --> 01:23:30,830 So I'll show you one way you could have approached the problem. 1717 01:23:30,830 --> 01:23:32,913 Again, there were many ways you could have done it 1718 01:23:32,913 --> 01:23:36,110 and I saw a lot of different ways in the ed discussion forum. 1719 01:23:36,110 --> 01:23:40,270 So definitely, OK if you didn't do it exactly the way that I did, but here's 1720 01:23:40,270 --> 01:23:42,070 one way you could have approached it. 1721 01:23:42,070 --> 01:23:45,910 I know I'm going to need the random module. 1722 01:23:45,910 --> 01:23:48,220 And I'll go ahead and define a main function 1723 01:23:48,220 --> 01:23:50,468 and run my main function at the bottom. 1724 01:23:50,468 --> 01:23:52,510 I'll start to do this more often in the programs, 1725 01:23:52,510 --> 01:23:54,760 just as the programs get more complex. 1726 01:23:54,760 --> 01:23:57,860 It can be nice to have a main function to keep things organized. 1727 01:23:57,860 --> 01:24:02,640 But what I'll do first is give myself a random number, 1728 01:24:02,640 --> 01:24:07,770 which I can get by via random.randit, and then 1 to 10. 1729 01:24:07,770 --> 01:24:11,560 So this is going to give me a random number between 1 and 10, 1730 01:24:11,560 --> 01:24:13,440 which is probably a good place to start. 1731 01:24:13,440 --> 01:24:16,230 And now that my program has picked a random number, 1732 01:24:16,230 --> 01:24:19,890 I would like to give my user three opportunities 1733 01:24:19,890 --> 01:24:23,280 to guess that number, to be able to make some sort of a guess. 1734 01:24:23,280 --> 01:24:26,918 And I see Hussein's using a for loop for i in range 3. 1735 01:24:26,918 --> 01:24:28,710 And I'm going to do exactly the same thing, 1736 01:24:28,710 --> 01:24:34,390 for i in range 3 to repeat some code three times. 1737 01:24:34,390 --> 01:24:37,290 And this is going to be 3 different guesses that the user is 1738 01:24:37,290 --> 01:24:42,250 going to have to make a guess. 1739 01:24:42,250 --> 01:24:45,980 And let's go ahead and get a guess from the user. 1740 01:24:45,980 --> 01:24:52,240 So I will say, guess is going to be an integer that comes in from the input. 1741 01:24:52,240 --> 01:24:55,250 We're going to ask the user to type in a guess. 1742 01:24:55,250 --> 01:24:57,200 The function does not have to be called main, 1743 01:24:57,200 --> 01:24:59,750 but it is a convention to call your primary function 1744 01:24:59,750 --> 01:25:02,610 that you're going to run when you run the program the main function. 1745 01:25:02,610 --> 01:25:06,562 So I'm following that convention here, but you could name it something else. 1746 01:25:06,562 --> 01:25:08,020 So I'm asking the user for a guess. 1747 01:25:08,020 --> 01:25:09,520 It's going to be an integer. 1748 01:25:09,520 --> 01:25:11,500 And now let's ask ourselves some conditions. 1749 01:25:11,500 --> 01:25:14,500 You might remember from conditions.py we wrote before, him 1750 01:25:14,500 --> 01:25:16,710 were checking is x greater than 0? 1751 01:25:16,710 --> 01:25:18,070 Is x less than 0? 1752 01:25:18,070 --> 01:25:19,420 Otherwise, what do we do. 1753 01:25:19,420 --> 01:25:21,410 We're going to do the same thing here. 1754 01:25:21,410 --> 01:25:24,430 But instead of comparing against 0, let's 1755 01:25:24,430 --> 01:25:28,630 compare it against the number, whatever the random number was 1756 01:25:28,630 --> 01:25:31,220 that was generated. 1757 01:25:31,220 --> 01:25:36,490 And so if the guess is greater than the number that was picked by the computer, 1758 01:25:36,490 --> 01:25:41,400 let's print out the guess is too large, or you could print some other message. 1759 01:25:41,400 --> 01:25:46,670 elif, meaning otherwise, if the guess is less than the number, 1760 01:25:46,670 --> 01:25:50,250 print guess is too small. 1761 01:25:50,250 --> 01:25:53,760 And then else, if the number is not too big or too small, it must be right. 1762 01:25:53,760 --> 01:25:57,750 I saw some people saying elif guess equals equals number. 1763 01:25:57,750 --> 01:26:00,990 We haven't seen this yet, but equals equals, with two equal signs, 1764 01:26:00,990 --> 01:26:03,710 is how you check if two things are equal to each other. 1765 01:26:03,710 --> 01:26:05,460 You could do that, but I'm also just going 1766 01:26:05,460 --> 01:26:08,790 to assume that if the number is not too big and the number is not too small, 1767 01:26:08,790 --> 01:26:10,350 we must have the right number. 1768 01:26:10,350 --> 01:26:14,270 So go ahead and print out correct. 1769 01:26:14,270 --> 01:26:16,160 And so now, I could try running the program. 1770 01:26:16,160 --> 01:26:17,993 I don't know what number it's going to pick, 1771 01:26:17,993 --> 01:26:21,618 but let's go ahead and try running Python guess.py. 1772 01:26:21,618 --> 01:26:22,410 Let's make a guess. 1773 01:26:22,410 --> 01:26:23,395 Let's guess 3. 1774 01:26:23,395 --> 01:26:26,730 1775 01:26:26,730 --> 01:26:27,610 Guess is too small. 1776 01:26:27,610 --> 01:26:28,818 Let's guess something bigger. 1777 01:26:28,818 --> 01:26:29,460 We'll guess 7. 1778 01:26:29,460 --> 01:26:30,180 Guess is too large. 1779 01:26:30,180 --> 01:26:31,138 We'll guess in between. 1780 01:26:31,138 --> 01:26:33,160 Let's try 5. 1781 01:26:33,160 --> 01:26:34,200 Guess it's too small. 1782 01:26:34,200 --> 01:26:34,930 So it was 6. 1783 01:26:34,930 --> 01:26:38,447 I didn't get it in three tries, so it stopped me. 1784 01:26:38,447 --> 01:26:39,280 But let's try again. 1785 01:26:39,280 --> 01:26:40,060 Maybe I'll get this done. 1786 01:26:40,060 --> 01:26:40,870 Let's guess 5. 1787 01:26:40,870 --> 01:26:41,680 Guess is too small. 1788 01:26:41,680 --> 01:26:43,230 Let's guess 8. 1789 01:26:43,230 --> 01:26:44,242 Guess is too large. 1790 01:26:44,242 --> 01:26:45,450 And so it's got to be 6 or 7. 1791 01:26:45,450 --> 01:26:47,130 I've got a 50/50 shot at this. 1792 01:26:47,130 --> 01:26:47,900 Let's guess 7. 1793 01:26:47,900 --> 01:26:49,320 OK, it was too large again. 1794 01:26:49,320 --> 01:26:51,090 So you could continue to play this game. 1795 01:26:51,090 --> 01:26:53,550 Maybe you'll have better luck than I do at trying 1796 01:26:53,550 --> 01:26:55,920 to guess what the right number is. 1797 01:26:55,920 --> 01:26:59,460 And maybe a slight improvement you could make, which I saw a couple people make, 1798 01:26:59,460 --> 01:27:02,220 is that once you get the answer right, you probably 1799 01:27:02,220 --> 01:27:04,680 don't want to let the user keep guessing. 1800 01:27:04,680 --> 01:27:06,790 And so here, we have a couple options. 1801 01:27:06,790 --> 01:27:08,610 We could break out of the loop. 1802 01:27:08,610 --> 01:27:12,160 We could even just say return, say the program's over. 1803 01:27:12,160 --> 01:27:14,160 We don't need to keep running the main function. 1804 01:27:14,160 --> 01:27:17,640 Once you're correct, let's just exit the program altogether. 1805 01:27:17,640 --> 01:27:19,792 Return is how you get out of a function. 1806 01:27:19,792 --> 01:27:21,750 So when we're done inside of the main function, 1807 01:27:21,750 --> 01:27:23,870 we can use return to get out of it. 1808 01:27:23,870 --> 01:27:27,130 1809 01:27:27,130 --> 01:27:29,350 Can we pick a number ourselves instead of 1810 01:27:29,350 --> 01:27:31,960 having the program pick a random one, I see someone asking. 1811 01:27:31,960 --> 01:27:32,780 Yes you could. 1812 01:27:32,780 --> 01:27:36,748 Right here on line 4 is where the random number is selected. 1813 01:27:36,748 --> 01:27:39,790 Right now, we're setting the number equal to some random number between 1 1814 01:27:39,790 --> 01:27:40,690 and 10. 1815 01:27:40,690 --> 01:27:43,615 I could cheat, of course, and just say, let's make the number 4. 1816 01:27:43,615 --> 01:27:45,490 And now I know exactly what it's going to be. 1817 01:27:45,490 --> 01:27:48,160 I'll just guess 4 and correct, I got it right. 1818 01:27:48,160 --> 01:27:50,170 But maybe that's not such a fun game, and it 1819 01:27:50,170 --> 01:27:53,870 will be a little more fun if the number is indeed a random number. 1820 01:27:53,870 --> 01:27:56,670 1821 01:27:56,670 --> 01:28:00,360 So again, multiple ways you could have approached that problem. 1822 01:28:00,360 --> 01:28:01,710 This was just one. 1823 01:28:01,710 --> 01:28:05,180 I've seen a couple of people ask now about return values of functions. 1824 01:28:05,180 --> 01:28:07,942 So I thought before we move on, I'd do an example of that. 1825 01:28:07,942 --> 01:28:10,650 So we've been doing a little bit of work with arithmetic earlier. 1826 01:28:10,650 --> 01:28:12,615 Let me create a new file called-- 1827 01:28:12,615 --> 01:28:15,720 1828 01:28:15,720 --> 01:28:20,280 well, actually, yeah, let me create a new file called maximum.py. 1829 01:28:20,280 --> 01:28:22,380 So let's imagine I want two numbers, and I 1830 01:28:22,380 --> 01:28:24,078 want to figure out which one is bigger. 1831 01:28:24,078 --> 01:28:26,370 So I want to be able to take two numbers and figure out 1832 01:28:26,370 --> 01:28:29,580 which one is bigger, probably something not too hard for a human to do. 1833 01:28:29,580 --> 01:28:33,150 But it turns out that quite often we want to, in a computer program, 1834 01:28:33,150 --> 01:28:36,660 look at two numbers and figure out which of those numbers is bigger. 1835 01:28:36,660 --> 01:28:39,540 It turns out Python actually has a built in function to do this. 1836 01:28:39,540 --> 01:28:41,873 We're not going to use that for now, just to demonstrate 1837 01:28:41,873 --> 01:28:44,370 how you could write this on your own. 1838 01:28:44,370 --> 01:28:48,940 But let me give myself a main function and get two integers as input. 1839 01:28:48,940 --> 01:28:52,320 So I'm going to get a value x, and I'm going to get a value y. 1840 01:28:52,320 --> 01:28:55,910 These are lines we've seen before several times now. 1841 01:28:55,910 --> 01:28:59,540 And now, what I'd like to do is define a new function 1842 01:28:59,540 --> 01:29:03,540 that is going to calculate the maximum of two numbers. 1843 01:29:03,540 --> 01:29:05,270 So let me give myself a new function. 1844 01:29:05,270 --> 01:29:06,730 I'm going to call it maximum. 1845 01:29:06,730 --> 01:29:09,350 And it's going to take as input two values. 1846 01:29:09,350 --> 01:29:11,150 And I can give those values names. 1847 01:29:11,150 --> 01:29:13,558 I could call them x and y, for consistency. 1848 01:29:13,558 --> 01:29:15,350 But you can also give them different names. 1849 01:29:15,350 --> 01:29:18,088 Every function gets its own set of variables. 1850 01:29:18,088 --> 01:29:19,880 So I could call these a and b, for example, 1851 01:29:19,880 --> 01:29:25,270 to make clear that this is a different set of names inside of this function. 1852 01:29:25,270 --> 01:29:29,220 And now, how do I find the maximum value between a and b? 1853 01:29:29,220 --> 01:29:34,230 Well, if a is greater than b, then the maximum value is just a. 1854 01:29:34,230 --> 01:29:39,270 And so I would like for this function to produce as output whatever a is. 1855 01:29:39,270 --> 01:29:41,490 And for a function to produce something as output, 1856 01:29:41,490 --> 01:29:44,610 this is what return means, in answer to Abdullah's question in the chat 1857 01:29:44,610 --> 01:29:45,450 right now. 1858 01:29:45,450 --> 01:29:48,120 Return means exit from a function. 1859 01:29:48,120 --> 01:29:51,210 And return can also optionally leave a function 1860 01:29:51,210 --> 01:29:54,540 and also have some output from that function, some data that 1861 01:29:54,540 --> 01:29:57,150 comes back from the function that can be used 1862 01:29:57,150 --> 01:30:00,520 by whoever is running that function. 1863 01:30:00,520 --> 01:30:03,150 So we'll go ahead and return a. 1864 01:30:03,150 --> 01:30:07,382 And else, otherwise, if a was not the maximum, I can just return b. 1865 01:30:07,382 --> 01:30:09,090 It's possible that the two were the same, 1866 01:30:09,090 --> 01:30:12,340 but if the two were the same it doesn't matter which one we return. 1867 01:30:12,340 --> 01:30:15,450 So we can just return the variable b. 1868 01:30:15,450 --> 01:30:19,230 And so I now have a function that takes two numbers and calculates 1869 01:30:19,230 --> 01:30:20,920 whichever the maximum is. 1870 01:30:20,920 --> 01:30:24,750 And so now, let me say that z, a new variable, 1871 01:30:24,750 --> 01:30:27,850 is going to be the maximum of whatever x and y are. 1872 01:30:27,850 --> 01:30:31,950 I'm passing in two inputs now to the maximum function, x and y. 1873 01:30:31,950 --> 01:30:36,360 And whatever the maximum's return value is, whatever output comes back 1874 01:30:36,360 --> 01:30:41,730 from the maximum function, that's going to be stored inside of this variable z. 1875 01:30:41,730 --> 01:30:48,350 And so now, I could print out the maximum is and then z. 1876 01:30:48,350 --> 01:30:51,735 I need to be sure to call my main function at the very end. 1877 01:30:51,735 --> 01:30:52,860 But let's now try this out. 1878 01:30:52,860 --> 01:30:57,390 I'm going to try python maximum, let me try typing 2 and 8. 1879 01:30:57,390 --> 01:31:00,570 And the maximum of those two numbers is 8. 1880 01:31:00,570 --> 01:31:03,630 And I did that by writing a function that can calculate maximums 1881 01:31:03,630 --> 01:31:04,600 that I've used once. 1882 01:31:04,600 --> 01:31:06,990 But in a more complex program, you can imagine that I 1883 01:31:06,990 --> 01:31:09,580 would use this function multiple times. 1884 01:31:09,580 --> 01:31:11,910 And I'm now using that ability to calculate 1885 01:31:11,910 --> 01:31:13,725 the maximum of two different values. 1886 01:31:13,725 --> 01:31:16,662 1887 01:31:16,662 --> 01:31:17,370 I see a question. 1888 01:31:17,370 --> 01:31:20,250 How does Python know that these are integers that we're dealing with? 1889 01:31:20,250 --> 01:31:22,650 Python knows that it's an integer in particular 1890 01:31:22,650 --> 01:31:26,200 because I converted the value to an integer here, on lines 2 and 3. 1891 01:31:26,200 --> 01:31:29,850 Whatever the user typed in, I converted it into an integer. 1892 01:31:29,850 --> 01:31:34,270 And we're saving it inside of these variables. 1893 01:31:34,270 --> 01:31:38,530 Dawn is asking how is x different from a here? 1894 01:31:38,530 --> 01:31:42,880 Every function is allowed to have its own set of variables with its own names 1895 01:31:42,880 --> 01:31:45,490 that are not accessible by other functions. 1896 01:31:45,490 --> 01:31:47,230 And so the names could overlap. 1897 01:31:47,230 --> 01:31:49,480 But I'm giving them different names to make clear 1898 01:31:49,480 --> 01:31:52,330 that they don't need to be the same. 1899 01:31:52,330 --> 01:31:55,300 That maximum is allowed to have inputs a and b, 1900 01:31:55,300 --> 01:31:58,720 and the connection here is that inside the main function, where 1901 01:31:58,720 --> 01:32:01,780 I have these x and y variables, I'm using 1902 01:32:01,780 --> 01:32:05,530 x and y as the inputs to the maximum function. 1903 01:32:05,530 --> 01:32:09,850 And maximum is just going to call those values a and b. 1904 01:32:09,850 --> 01:32:12,790 So every function gets its own set of variables that are not 1905 01:32:12,790 --> 01:32:15,610 accessible by the other variables. 1906 01:32:15,610 --> 01:32:19,300 1907 01:32:19,300 --> 01:32:21,430 Why did I need to write main at the bottom 1908 01:32:21,430 --> 01:32:24,730 but I didn't need to write maximum at the bottom, someone's asking. 1909 01:32:24,730 --> 01:32:28,750 The reason why is that the main function is where my program begins. 1910 01:32:28,750 --> 01:32:31,750 And while the main function is running, the main function 1911 01:32:31,750 --> 01:32:35,113 is going to call or run that maximum function. 1912 01:32:35,113 --> 01:32:37,030 And so I didn't need to say maximum down here. 1913 01:32:37,030 --> 01:32:40,455 It's possible to have one function call another function, 1914 01:32:40,455 --> 01:32:42,830 and then that function can call another function as well. 1915 01:32:42,830 --> 01:32:47,943 So you can have functions that are running other functions, too. 1916 01:32:47,943 --> 01:32:50,110 I know that functions can be a little bit confusing. 1917 01:32:50,110 --> 01:32:52,340 I'm going to do one more example of functions, 1918 01:32:52,340 --> 01:32:56,280 and maybe even two more examples of functions before your next exercise, 1919 01:32:56,280 --> 01:32:59,670 just so you can get a sense for how it is that these work. 1920 01:32:59,670 --> 01:33:01,860 One thing that you might want to do, imagine 1921 01:33:01,860 --> 01:33:05,640 you're playing a game with three players and every player scores 1922 01:33:05,640 --> 01:33:07,250 a certain number of points. 1923 01:33:07,250 --> 01:33:10,500 And what I'd like to do is, I'm doing a little bit of very simplistic computer 1924 01:33:10,500 --> 01:33:14,130 graphics, print out like a bar chart of how many points 1925 01:33:14,130 --> 01:33:20,660 each player got in the game. 1926 01:33:20,660 --> 01:33:23,280 So let me do that inside of a new file. 1927 01:33:23,280 --> 01:33:26,550 I'm going to call this file scores.py. 1928 01:33:26,550 --> 01:33:31,110 And what I'm going to do inside my main function is first ask for three scores. 1929 01:33:31,110 --> 01:33:34,740 Score 1 is going to get an integer input. 1930 01:33:34,740 --> 01:33:36,030 Type in Score 1. 1931 01:33:36,030 --> 01:33:38,490 score2 is going to be an integer input. 1932 01:33:38,490 --> 01:33:39,990 Type in another score. 1933 01:33:39,990 --> 01:33:44,130 And then score3 is going to be an integer input, Score 3. 1934 01:33:44,130 --> 01:33:47,070 So I'm asking the user to type in three scores. 1935 01:33:47,070 --> 01:33:50,820 And now, what I'd like to do is print out 1936 01:33:50,820 --> 01:33:54,240 a horizontal bar chart of those scores. 1937 01:33:54,240 --> 01:33:59,030 And what I'm going to use is a hash mark, a hash symbol, for each bar, 1938 01:33:59,030 --> 01:34:01,290 for each piece of that bar. 1939 01:34:01,290 --> 01:34:06,277 So if score 1 is like 5, then I want to print five hash marks, for example. 1940 01:34:06,277 --> 01:34:07,110 So how do I do that? 1941 01:34:07,110 --> 01:34:10,350 If score1 is 5, how do I print five hash marks? 1942 01:34:10,350 --> 01:34:13,030 Well, I probably want some kind of a loop. 1943 01:34:13,030 --> 01:34:18,080 So I could say for i in range score1, let's go ahead 1944 01:34:18,080 --> 01:34:21,680 and print out a hash mark. 1945 01:34:21,680 --> 01:34:24,410 And for now, I'm actually going to ignore score2 and score3. 1946 01:34:24,410 --> 01:34:28,038 I'm going to put a hash symbol in front of each to make them a comment. 1947 01:34:28,038 --> 01:34:30,830 Oftentimes, if I don't want to deal with certain lines of code yet, 1948 01:34:30,830 --> 01:34:34,190 I can put a comment in front of them to say, let's not run these lines of code 1949 01:34:34,190 --> 01:34:34,690 yet. 1950 01:34:34,690 --> 01:34:37,127 I'll add them into the program a little bit later. 1951 01:34:37,127 --> 01:34:38,960 So right now, all the main function is going 1952 01:34:38,960 --> 01:34:44,620 to do is it's going to ask for one score and then print a hash mark. 1953 01:34:44,620 --> 01:34:45,830 So let's try this. 1954 01:34:45,830 --> 01:34:50,060 I need to be sure to run my main function here at the bottom. 1955 01:34:50,060 --> 01:34:53,248 But let's run python scores.py. 1956 01:34:53,248 --> 01:34:54,040 I type in a number. 1957 01:34:54,040 --> 01:34:56,890 Let's type in 5, for example. 1958 01:34:56,890 --> 01:34:59,230 And all right, I got 5 hash marks. 1959 01:34:59,230 --> 01:35:00,970 But I wanted these to be like horizontal, 1960 01:35:00,970 --> 01:35:05,780 like hash marks in a row instead of hash marks stacked on top of each other. 1961 01:35:05,780 --> 01:35:08,790 So what did I do wrong? 1962 01:35:08,790 --> 01:35:09,790 What mistake did I make? 1963 01:35:09,790 --> 01:35:12,373 In fact it's a mistake I made a little bit earlier today, too. 1964 01:35:12,373 --> 01:35:15,307 1965 01:35:15,307 --> 01:35:16,140 Yeah, the new lines. 1966 01:35:16,140 --> 01:35:18,180 Python is automatically adding a new line 1967 01:35:18,180 --> 01:35:19,920 at the end of every print statement. 1968 01:35:19,920 --> 01:35:22,140 It's automatically creating a new line for me. 1969 01:35:22,140 --> 01:35:23,640 And that's not really what I want. 1970 01:35:23,640 --> 01:35:27,375 It turns out there's a way to override what comes at the end of the print, 1971 01:35:27,375 --> 01:35:28,500 and I'll show you that now. 1972 01:35:28,500 --> 01:35:29,000 It's new. 1973 01:35:29,000 --> 01:35:30,160 We haven't seen this yet. 1974 01:35:30,160 --> 01:35:32,910 But after the string, I'm going to add a comma. 1975 01:35:32,910 --> 01:35:37,310 And then I'm going to say end equals two quotation marks, 1976 01:35:37,310 --> 01:35:41,280 two quotation marks just being an empty string, no characters. 1977 01:35:41,280 --> 01:35:43,560 And what that's saying is that print the hash mark, 1978 01:35:43,560 --> 01:35:47,370 but after you print the hash mark, don't print a new line, just print nothing. 1979 01:35:47,370 --> 01:35:51,540 Nothing goes after that hash mark. 1980 01:35:51,540 --> 01:35:56,460 And so now if I run python scores.py, I type in a number, like the number 5, 1981 01:35:56,460 --> 01:36:00,452 and now I see five hash marks printed one after the other. 1982 01:36:00,452 --> 01:36:01,410 And that's what I want. 1983 01:36:01,410 --> 01:36:02,700 They're all in one row. 1984 01:36:02,700 --> 01:36:06,270 Now, one slightly annoying thing is that usually this prompt 1985 01:36:06,270 --> 01:36:08,640 appears on a brand new line for me. 1986 01:36:08,640 --> 01:36:11,440 This time, the prompt is like stuck on the same line. 1987 01:36:11,440 --> 01:36:14,760 And the reason is I didn't print a new line at the end of the program, 1988 01:36:14,760 --> 01:36:16,050 because I overrode that. 1989 01:36:16,050 --> 01:36:19,350 I said don't print a new line, just print nothing. 1990 01:36:19,350 --> 01:36:22,980 And so the kind of tricky way to fix this is, at the very end, 1991 01:36:22,980 --> 01:36:25,570 I can just say print with nothing in it. 1992 01:36:25,570 --> 01:36:27,605 And that won't print any new characters. 1993 01:36:27,605 --> 01:36:30,730 But because it is a print, it's going to print a new line at the end of it. 1994 01:36:30,730 --> 01:36:35,350 And so that will give me a clean new line at the very end. 1995 01:36:35,350 --> 01:36:36,990 So let me try this program again. 1996 01:36:36,990 --> 01:36:38,700 I type in the number 5. 1997 01:36:38,700 --> 01:36:44,390 And I do, in fact, see five bars, which is exactly what I wanted. 1998 01:36:44,390 --> 01:36:45,390 So that's now one score. 1999 01:36:45,390 --> 01:36:47,020 Let's do it for the other two. 2000 01:36:47,020 --> 01:36:52,230 So I'm going to uncomment these lines for score2 and score3. 2001 01:36:52,230 --> 01:36:54,090 And now, I really want to do the same thing, 2002 01:36:54,090 --> 01:36:55,675 just printing out that bar chart. 2003 01:36:55,675 --> 01:36:58,050 So I'm going to copy and paste it, and do the same thing. 2004 01:36:58,050 --> 01:37:01,890 But instead of score1, let's print it out for score3, 2005 01:37:01,890 --> 01:37:04,320 and then we'll do it one more time for score3. 2006 01:37:04,320 --> 01:37:06,870 2007 01:37:06,870 --> 01:37:09,150 And so I now have the same logic to print out a bar 2008 01:37:09,150 --> 01:37:12,525 for score1, for score2, and for score3. 2009 01:37:12,525 --> 01:37:14,400 And now, I have the ability to print a chart. 2010 01:37:14,400 --> 01:37:15,390 I can print out scores. 2011 01:37:15,390 --> 01:37:19,470 Maybe this person got 2 points and 3 points and 5 points. 2012 01:37:19,470 --> 01:37:25,660 And I get a bar chart with two hashes, eight hashes, and five hashes, 2013 01:37:25,660 --> 01:37:29,910 which is exactly what I wanted to see. 2014 01:37:29,910 --> 01:37:31,880 Now, there's something not great about the way 2015 01:37:31,880 --> 01:37:36,590 that I've written this code, something maybe not the best design. 2016 01:37:36,590 --> 01:37:39,530 Can anyone point out for me maybe what mistake I've made, 2017 01:37:39,530 --> 01:37:41,763 what I could improve here? 2018 01:37:41,763 --> 01:37:43,180 Yeah, there's a lot of repetition. 2019 01:37:43,180 --> 01:37:45,920 I see a lot of people suggesting that I've repeated myself a lot. 2020 01:37:45,920 --> 01:37:49,310 Especially here with these lines that are printing out all these hash marks, 2021 01:37:49,310 --> 01:37:50,890 there's a lot of repetition. 2022 01:37:50,890 --> 01:37:53,200 And repetition is another place where it can 2023 01:37:53,200 --> 01:37:56,770 be very helpful to have a function, a function that 2024 01:37:56,770 --> 01:37:59,260 organizes some of the common code, so that I 2025 01:37:59,260 --> 01:38:02,000 can use that code multiple times. 2026 01:38:02,000 --> 01:38:04,380 So let me give myself a function. 2027 01:38:04,380 --> 01:38:07,920 I'm going to call this function print_scores. 2028 01:38:07,920 --> 01:38:10,020 And it's going to accept as input the number 2029 01:38:10,020 --> 01:38:14,550 n, which is going to represent how many hash marks should I print. 2030 01:38:14,550 --> 01:38:17,200 Now, what is the print scores function going to do? 2031 01:38:17,200 --> 01:38:21,630 Well, we're going to loop n times, and I can do that for i in range n, 2032 01:38:21,630 --> 01:38:24,200 as we've seen before. 2033 01:38:24,200 --> 01:38:27,050 I'm going to print a hash mark with nothing at the end. 2034 01:38:27,050 --> 01:38:28,860 We've seen that before too. 2035 01:38:28,860 --> 01:38:31,610 And at the very end, we're going to print a new line. 2036 01:38:31,610 --> 01:38:37,590 So the exact same logic, I'm just using n instead of score1, score2, or score3. 2037 01:38:37,590 --> 01:38:46,690 And we're going to organize all of that into this function called print scores. 2038 01:38:46,690 --> 01:38:49,230 And so now, instead of these nine lines where 2039 01:38:49,230 --> 01:38:51,810 I'm repeating the same logic over and over again, 2040 01:38:51,810 --> 01:38:53,460 I can simplify a little bit. 2041 01:38:53,460 --> 01:39:00,180 I can just say print_scores(score1), and then do the same thing for score2, 2042 01:39:00,180 --> 01:39:02,340 and then do the same thing for score3. 2043 01:39:02,340 --> 01:39:04,050 I've simplified and organize my code. 2044 01:39:04,050 --> 01:39:08,670 I've cut down on the number of lines that I need inside of my program, 2045 01:39:08,670 --> 01:39:11,850 to take the common code, the code for printing out these bars, 2046 01:39:11,850 --> 01:39:14,370 and organizing them into a function. 2047 01:39:14,370 --> 01:39:17,128 2048 01:39:17,128 --> 01:39:18,170 And so now, I can run it. 2049 01:39:18,170 --> 01:39:21,240 It'll do the exact same thing, no different. 2050 01:39:21,240 --> 01:39:23,300 But I'd argue my code is a little more organized. 2051 01:39:23,300 --> 01:39:25,050 And you could maybe organize it even more, 2052 01:39:25,050 --> 01:39:27,860 reduce some repetition with some loops, someone is suggesting. 2053 01:39:27,860 --> 01:39:31,430 But I really wanted to just point out here the value of having a function, 2054 01:39:31,430 --> 01:39:34,910 not just for input and output, but also for organizing code 2055 01:39:34,910 --> 01:39:36,380 into a place where you can reuse. 2056 01:39:36,380 --> 01:39:39,110 And so functions are very good for code reuse 2057 01:39:39,110 --> 01:39:42,770 if I want to use some piece of code multiple times. 2058 01:39:42,770 --> 01:39:46,500 2059 01:39:46,500 --> 01:39:49,560 All right, one last thing that I want to show you 2060 01:39:49,560 --> 01:39:53,520 before I give you one final exercise to work on today 2061 01:39:53,520 --> 01:39:57,520 is how you might organize loops together in a sequence. 2062 01:39:57,520 --> 01:40:01,270 And so to introduce this, I'm going to create a new file. 2063 01:40:01,270 --> 01:40:04,320 I'm going to call it multiplication.py. 2064 01:40:04,320 --> 01:40:06,570 And I'm going to use this to be a program that's going 2065 01:40:06,570 --> 01:40:09,080 to print out some multiplication facts. 2066 01:40:09,080 --> 01:40:12,330 You might have learned your times tables in grade school, where you learn what 2067 01:40:12,330 --> 01:40:16,560 2 times 3, and 2 times 4, and 2 times 5, and you learn all your multiplication 2068 01:40:16,560 --> 01:40:17,580 facts. 2069 01:40:17,580 --> 01:40:20,280 Let's write a program that's going to print out 2070 01:40:20,280 --> 01:40:22,380 some of these multiplication facts. 2071 01:40:22,380 --> 01:40:25,090 And so I'm going to define a main function 2072 01:40:25,090 --> 01:40:27,210 that I'm going to call down below. 2073 01:40:27,210 --> 01:40:29,920 And we'll add some code here soon. 2074 01:40:29,920 --> 01:40:32,310 But now, before I even get to that, I'm going 2075 01:40:32,310 --> 01:40:35,310 to write a new function called multiply that's going 2076 01:40:35,310 --> 01:40:40,860 to print out multiplication facts for two numbers, for numbers x and y. 2077 01:40:40,860 --> 01:40:42,395 And how do I print out that? 2078 01:40:42,395 --> 01:40:44,520 Well, I first need to calculate, what do I get when 2079 01:40:44,520 --> 01:40:46,480 I multiply the two numbers together? 2080 01:40:46,480 --> 01:40:49,200 So I'm going to create a new variable called product, 2081 01:40:49,200 --> 01:40:52,080 which is just going to be x times y. 2082 01:40:52,080 --> 01:40:54,660 And now, I'm going to print out that multiplication fact. 2083 01:40:54,660 --> 01:41:02,843 I'm going to print out x multiplied by y is going to be equal to the product. 2084 01:41:02,843 --> 01:41:05,760 So this is a rather long print statement that's got a bunch of commas, 2085 01:41:05,760 --> 01:41:07,468 because I'm printing out multiple things. 2086 01:41:07,468 --> 01:41:11,430 But I'm printing the variable x comma, the string that 2087 01:41:11,430 --> 01:41:15,420 is just going to be the asterisk for multiplication, that's 2088 01:41:15,420 --> 01:41:17,010 in quotation marks, then a comma. 2089 01:41:17,010 --> 01:41:19,427 I want to print something else, I want to print the number 2090 01:41:19,427 --> 01:41:21,840 y, the variable y, then a comma. 2091 01:41:21,840 --> 01:41:25,260 Then in quotation marks equal sign to print out an equal sign, 2092 01:41:25,260 --> 01:41:29,310 and then after a comma, print out whatever the product is. 2093 01:41:29,310 --> 01:41:32,340 I'm not returning anything, I just want to print something out 2094 01:41:32,340 --> 01:41:34,300 to the terminal window. 2095 01:41:34,300 --> 01:41:37,840 And now I can use that multiply function inside of my main function. 2096 01:41:37,840 --> 01:41:43,020 I can say, go ahead and multiply 2 and 3, and then multiply 2 and 4, 2097 01:41:43,020 --> 01:41:45,050 for example. 2098 01:41:45,050 --> 01:41:48,270 And so now if I do python multiplication, 2099 01:41:48,270 --> 01:41:49,740 I see these multiplication facts. 2100 01:41:49,740 --> 01:41:51,150 2 times 3 is 6. 2101 01:41:51,150 --> 01:41:52,500 2 times 4 is 8. 2102 01:41:52,500 --> 01:41:55,770 Each time, this multiply function is calculating what 2103 01:41:55,770 --> 01:41:57,570 I get when I multiply those numbers. 2104 01:41:57,570 --> 01:42:01,080 And it's printing out that multiplication fact for me 2105 01:42:01,080 --> 01:42:04,980 so that I can see what it is. 2106 01:42:04,980 --> 01:42:08,780 And I'm running it two times on 2 and 3, and 2 and 4. 2107 01:42:08,780 --> 01:42:11,720 But now what I'd like to do is do all of the multiplication facts. 2108 01:42:11,720 --> 01:42:13,790 I want to print out all the multiplication facts 2109 01:42:13,790 --> 01:42:16,520 for like 0 up through 9, maybe. 2110 01:42:16,520 --> 01:42:18,180 And how would I do that? 2111 01:42:18,180 --> 01:42:20,000 Well, I could do this inside of a loop. 2112 01:42:20,000 --> 01:42:27,113 I could say let's do for i in range 10, and then let's do multiply, well 2113 01:42:27,113 --> 01:42:28,280 what am I going to multiply? 2114 01:42:28,280 --> 01:42:29,840 Well we can start with maybe 2. 2115 01:42:29,840 --> 01:42:34,710 Let's multiply 2 times whatever i is, for example. 2116 01:42:34,710 --> 01:42:38,330 And so it's going to be 2 times 0 and then 2 times 1 and then 2 times 2, 2117 01:42:38,330 --> 01:42:40,580 all the way up 10 times. 2118 01:42:40,580 --> 01:42:46,630 And so now it's inside of a loop, and I get 2 times 0 is 0, 2 times 1 is 2, 2119 01:42:46,630 --> 01:42:50,380 and I get very nicely printed, very quickly computed by the computer, 2120 01:42:50,380 --> 01:42:52,600 all of the multiplication facts for the number 2. 2121 01:42:52,600 --> 01:42:56,782 2 times everything I now have here. 2122 01:42:56,782 --> 01:42:58,490 But what if I want to not just do 2 times 2123 01:42:58,490 --> 01:43:01,262 everything, but 3 times everything and 4 times everything and 5 2124 01:43:01,262 --> 01:43:01,970 times everything? 2125 01:43:01,970 --> 01:43:06,280 How can I add some logic to do that as well? 2126 01:43:06,280 --> 01:43:08,820 2127 01:43:08,820 --> 01:43:10,400 Any ideas? 2128 01:43:10,400 --> 01:43:13,910 Yeah, Nathan is suggesting I use two loops instead of just one loop. 2129 01:43:13,910 --> 01:43:16,535 And so we're introducing the concept of what people in the chat 2130 01:43:16,535 --> 01:43:18,860 are correctly calling a nested loop, where 2131 01:43:18,860 --> 01:43:21,710 I have one loop inside of another loop. 2132 01:43:21,710 --> 01:43:25,880 And that's going to cause the loop to happen again and again for every time 2133 01:43:25,880 --> 01:43:28,370 the outer loop is called. 2134 01:43:28,370 --> 01:43:31,350 And so you can get loops upon loops happening within each other. 2135 01:43:31,350 --> 01:43:35,330 And so in here, I'm going to say for j in range 10. 2136 01:43:35,330 --> 01:43:40,775 And then I'm going to indent one more time, multiply i and j. 2137 01:43:40,775 --> 01:43:42,010 So what's going to happen? 2138 01:43:42,010 --> 01:43:44,260 What's the logic for these nested loops? 2139 01:43:44,260 --> 01:43:47,920 Well, we're going to start with this outer loop, i is going to be 0. 2140 01:43:47,920 --> 01:43:50,620 And then we're going to run this inner loop. j is going to be 0. 2141 01:43:50,620 --> 01:43:54,770 So we're going to get 0 time 0, 0 times 1, 0 times 2, so on and so forth, 2142 01:43:54,770 --> 01:43:59,110 all the way up to 0 times 9, because this loop is happening 10 times. 2143 01:43:59,110 --> 01:44:00,910 And then we go back to this loop again. 2144 01:44:00,910 --> 01:44:02,553 i is now going to be 1. 2145 01:44:02,553 --> 01:44:04,720 And so now we're going to run this inner loop again. 2146 01:44:04,720 --> 01:44:08,800 We're going to do 1 times 0, 1 times 1, 1 times 2, so on and so forth up 2147 01:44:08,800 --> 01:44:10,120 to 1 times 9. 2148 01:44:10,120 --> 01:44:13,360 Then that loop finishes, and we go back to the outer loop again. 2149 01:44:13,360 --> 01:44:18,030 Now, i is 2, and so we're going to do 2 times 0, 2 times 1, 2 times 2, 2150 01:44:18,030 --> 01:44:19,870 so on and so forth. 2151 01:44:19,870 --> 01:44:22,180 And we're going to be 3 and again and again. 2152 01:44:22,180 --> 01:44:23,710 Let's see what that looks like. 2153 01:44:23,710 --> 01:44:26,260 I run python multiplication.py. 2154 01:44:26,260 --> 01:44:28,060 And we'll see exactly what we wanted, which 2155 01:44:28,060 --> 01:44:32,560 is we got all of our 0 multiplication facts printed out first, 2156 01:44:32,560 --> 01:44:36,020 then all of our 1's, then 2's, and so on, and so forth, 2157 01:44:36,020 --> 01:44:39,457 all the way up to 9 times 9. 2158 01:44:39,457 --> 01:44:41,290 And we did that via nested loops, because we 2159 01:44:41,290 --> 01:44:44,260 wanted one loop to run multiple times. 2160 01:44:44,260 --> 01:44:46,600 If you want a loop to happen multiple times, 2161 01:44:46,600 --> 01:44:51,630 you can put that loop inside of another loop. 2162 01:44:51,630 --> 01:44:53,630 This is a little tricky at first but in a moment 2163 01:44:53,630 --> 01:44:55,297 you'll get some practice with just this. 2164 01:44:55,297 --> 01:44:59,730 2165 01:44:59,730 --> 01:45:02,400 [INAUDIBLE] is asking how do you make it start at 1? 2166 01:45:02,400 --> 01:45:04,410 It turns out range can take multiple arguments. 2167 01:45:04,410 --> 01:45:06,480 By default range starts at 0. 2168 01:45:06,480 --> 01:45:11,640 If I wanted it to start at 1, I would say 1 comma 10, and 1 comma 10. 2169 01:45:11,640 --> 01:45:14,070 And that will have it instead of starting at 0, 2170 01:45:14,070 --> 01:45:19,260 it's going to start with 1 times 1, 1 times 2, all the way up to 1 times 9, 2171 01:45:19,260 --> 01:45:19,830 and so forth. 2172 01:45:19,830 --> 01:45:22,630 2173 01:45:22,630 --> 01:45:26,530 [? Fang ?] is asking where do i and j get defined as integers? 2174 01:45:26,530 --> 01:45:28,660 Because I have a range in my for loop, it's 2175 01:45:28,660 --> 01:45:31,750 ranging over integer values 1 and 10 here, 2176 01:45:31,750 --> 01:45:33,610 i is automatically going to be an integer. 2177 01:45:33,610 --> 01:45:36,880 Python just knows that if I'm looping over a range of integers, then 2178 01:45:36,880 --> 01:45:40,690 i is going to be an integer each time. 2179 01:45:40,690 --> 01:45:43,660 If you're asking for these exact lines of code, all of this code 2180 01:45:43,660 --> 01:45:46,030 is available on the CS50x website under [? this ?] 2181 01:45:46,030 --> 01:45:50,120 seminar, so you can find all of the code that I've written here today 2182 01:45:50,120 --> 01:45:51,370 as you're following along too. 2183 01:45:51,370 --> 01:45:56,390 2184 01:45:56,390 --> 01:45:56,890 All right. 2185 01:45:56,890 --> 01:45:59,980 So now that we have the ability to have nested loops, 2186 01:45:59,980 --> 01:46:03,160 and we have the ability to be a little bit clever about how we're printing 2187 01:46:03,160 --> 01:46:09,100 things-- you'll recall in scores.py, we used this n equals empty string to be 2188 01:46:09,100 --> 01:46:11,890 able to print something without going onto a new line-- 2189 01:46:11,890 --> 01:46:15,760 I thought we'd try and put these ideas together in one last in-class exercise 2190 01:46:15,760 --> 01:46:16,300 for today. 2191 01:46:16,300 --> 01:46:19,480 And then we'll talk about it afterwards before we wrap up. 2192 01:46:19,480 --> 01:46:21,010 And here is your last exercise. 2193 01:46:21,010 --> 01:46:24,430 It's inspired by the game Mario, where if you've played the game Super Mario, 2194 01:46:24,430 --> 01:46:28,760 you might have seen this grid of bricks that's somewhere in one of the levels. 2195 01:46:28,760 --> 01:46:32,040 And so what you're going to do is you're going to create a grid. 2196 01:46:32,040 --> 01:46:33,790 You're going to write a program that asks 2197 01:46:33,790 --> 01:46:36,660 the user to first type in a number, a number n, 2198 01:46:36,660 --> 01:46:39,770 and then you're going to print an n by n grid of hash marks. 2199 01:46:39,770 --> 01:46:43,030 So if the user types in 5, then you're going to get this-- a 5 2200 01:46:43,030 --> 01:46:46,540 by 5 grid of hashes with five rows and five columns. 2201 01:46:46,540 --> 01:46:49,180 If the user typed in 4, it would be a 4 by 4 grid. 2202 01:46:49,180 --> 01:46:52,460 If the user typed in 10 you'd, print a 10 by 10 grid. 2203 01:46:52,460 --> 01:46:54,820 So this is what you're now going to try to do. 2204 01:46:54,820 --> 01:46:58,030 It's not too many lines of code, but it is a little bit tricky. 2205 01:46:58,030 --> 01:47:01,240 You're going to have to maybe use the end trick at the end of print 2206 01:47:01,240 --> 01:47:02,170 that we talked about. 2207 01:47:02,170 --> 01:47:05,290 You might have to use some nested loops that we've talked about. 2208 01:47:05,290 --> 01:47:07,690 So we'll give you about 10 minutes to try and write this. 2209 01:47:07,690 --> 01:47:09,640 If you have questions about any of it, ask 2210 01:47:09,640 --> 01:47:13,352 questions in the ed discussion forum as I see many of you have been already. 2211 01:47:13,352 --> 01:47:16,060 The mentors are here to help you answer your questions as you try 2212 01:47:16,060 --> 01:47:18,610 and figure out how to write this program. 2213 01:47:18,610 --> 01:47:20,770 But in about 10 minutes, we'll reconvene and we'll 2214 01:47:20,770 --> 01:47:25,050 talk about how it is that you might have created this grid. 2215 01:47:25,050 --> 01:47:27,370 All right, welcome back, everyone. 2216 01:47:27,370 --> 01:47:30,702 So would anyone like to share how they approached building this grid? 2217 01:47:30,702 --> 01:47:31,910 What did your code look like? 2218 01:47:31,910 --> 01:47:34,380 What kind of techniques did you use? 2219 01:47:34,380 --> 01:47:37,310 Let's go to Michael, go ahead. 2220 01:47:37,310 --> 01:47:43,110 AUDIENCE: So basically, what I did is I defined the main function. 2221 01:47:43,110 --> 01:47:47,460 And I declared height as an int input, and then height, 2222 01:47:47,460 --> 01:47:49,600 so that you could input height. 2223 01:47:49,600 --> 01:47:52,810 And then I called a function which is draw. 2224 01:47:52,810 --> 01:47:57,010 And I added height as the argument. 2225 01:47:57,010 --> 01:48:03,220 And then below that, I defined draw, h as the argument. 2226 01:48:03,220 --> 01:48:06,580 And I put for i in range h, and then inside 2227 01:48:06,580 --> 01:48:09,610 of that I nested for j in range h, and then 2228 01:48:09,610 --> 01:48:15,040 inside of that print and then one hash and then comma end, and then nothing. 2229 01:48:15,040 --> 01:48:20,390 And then I've got print with nothing, so that I just print in a new line. 2230 01:48:20,390 --> 01:48:22,368 And then I just called main. 2231 01:48:22,368 --> 01:48:23,660 BRIAN YU: All right, very nice. 2232 01:48:23,660 --> 01:48:27,732 So you're using for i in range, for j in range, using some kind of nested loop, 2233 01:48:27,732 --> 01:48:30,440 using some printing, you even added an additional function, which 2234 01:48:30,440 --> 01:48:32,480 you didn't have to do but it was an option 2235 01:48:32,480 --> 01:48:35,033 to create an additional function if you wanted to. 2236 01:48:35,033 --> 01:48:36,950 [? Nashihad, ?] did you have another approach? 2237 01:48:36,950 --> 01:48:38,140 What did you do? 2238 01:48:38,140 --> 01:48:39,802 AUDIENCE: I kind of did the same thing. 2239 01:48:39,802 --> 01:48:42,260 BRIAN YU: All right, so let's take a look at that approach. 2240 01:48:42,260 --> 01:48:44,930 Using the approach of using nested loops and how we 2241 01:48:44,930 --> 01:48:47,200 might have gone about creating this grid. 2242 01:48:47,200 --> 01:48:48,950 And so the first thing we might want to do 2243 01:48:48,950 --> 01:48:51,440 is ask the user for how tall the pyramid should be 2244 01:48:51,440 --> 01:48:54,260 or how tall the grid should be, getting ahead of myself. 2245 01:48:54,260 --> 01:48:59,410 We'll ask for input, for maybe the height of the grid. 2246 01:48:59,410 --> 01:49:01,240 And now we want some kind of nested loop, 2247 01:49:01,240 --> 01:49:03,280 because we want to repeat row after row. 2248 01:49:03,280 --> 01:49:06,520 And each row is going to be repeated hash after hash. 2249 01:49:06,520 --> 01:49:12,330 So for i in range height, and for j in range height, 2250 01:49:12,330 --> 01:49:14,400 to create this nested loop. 2251 01:49:14,400 --> 01:49:15,878 I'm going to print a hash mark. 2252 01:49:15,878 --> 01:49:17,670 And I don't want anything at the end of it. 2253 01:49:17,670 --> 01:49:20,350 So end equals empty string. 2254 01:49:20,350 --> 01:49:23,370 So let's give this a try, python grid.py. 2255 01:49:23,370 --> 01:49:27,130 Let's type in 3 as a height, for example. 2256 01:49:27,130 --> 01:49:28,910 And let's press Return. 2257 01:49:28,910 --> 01:49:32,280 OK, that doesn't look much like a grid. 2258 01:49:32,280 --> 01:49:34,870 So it seems like I'm forgetting something. 2259 01:49:34,870 --> 01:49:37,510 Can anyone help me out with what I might be forgetting 2260 01:49:37,510 --> 01:49:41,150 or what I might be missing in the program? 2261 01:49:41,150 --> 01:49:43,400 Yeah, it's the new line, very good to those people who 2262 01:49:43,400 --> 01:49:44,780 are suggesting it in the chat. 2263 01:49:44,780 --> 01:49:47,270 I don't want new lines after every hash mark. 2264 01:49:47,270 --> 01:49:49,610 But I do want it at the end of the row, before I 2265 01:49:49,610 --> 01:49:52,740 go to the start of the next row inside of this loop. 2266 01:49:52,740 --> 01:49:55,640 And so at the very end, I can just say print to say go ahead 2267 01:49:55,640 --> 01:49:56,990 and give me a new line. 2268 01:49:56,990 --> 01:50:00,000 That way I move on to the next line before I start the next row. 2269 01:50:00,000 --> 01:50:04,070 So now if I say 5, I do in fact get a 5 by 5 grid. 2270 01:50:04,070 --> 01:50:06,740 If I instead say 7, I now get a 7 by 7 grid. 2271 01:50:06,740 --> 01:50:08,720 So it gives me a little bit of an ability 2272 01:50:08,720 --> 01:50:13,670 to customize what I want that grid to look like. 2273 01:50:13,670 --> 01:50:14,750 All right. 2274 01:50:14,750 --> 01:50:16,805 So as we wrap up today's session in Python, 2275 01:50:16,805 --> 01:50:18,680 we do have some homework for you, if you want 2276 01:50:18,680 --> 01:50:21,620 to try your hand at writing some more Python programs yourself. 2277 01:50:21,620 --> 01:50:24,290 They're taken from CS50's problem sets. 2278 01:50:24,290 --> 01:50:28,010 And so your first homework assignment, similar to what we just did, 2279 01:50:28,010 --> 01:50:29,510 also inspired by the game Mario. 2280 01:50:29,510 --> 01:50:32,625 At the end of one of the levels, you might recall this pyramid. 2281 01:50:32,625 --> 01:50:35,870 So your first homework assignment is to write a program in Python 2282 01:50:35,870 --> 01:50:37,070 to build this pyramid. 2283 01:50:37,070 --> 01:50:39,980 You're going to ask the user for a number, 2284 01:50:39,980 --> 01:50:41,555 for how tall the pyramid should be. 2285 01:50:41,555 --> 01:50:43,680 And you're going to print a pyramid of that height. 2286 01:50:43,680 --> 01:50:46,670 Notice that this is not a 5 by 5 grid, but a 5 2287 01:50:46,670 --> 01:50:48,950 by 5 pyramid, right aligned with everything 2288 01:50:48,950 --> 01:50:50,510 lined up on the right hand side. 2289 01:50:50,510 --> 01:50:53,900 But the first row has just one hash, the second row has two, 2290 01:50:53,900 --> 01:50:56,970 the third one has three, so on, and so forth. 2291 01:50:56,970 --> 01:50:59,690 And so that's your first homework problem. 2292 01:50:59,690 --> 01:51:03,693 And your second homework problem is a problem from CS50 called Cash. 2293 01:51:03,693 --> 01:51:06,110 You're going to write a program that asks the user to type 2294 01:51:06,110 --> 01:51:07,550 in some amount of money. 2295 01:51:07,550 --> 01:51:11,087 And you're going to print out how many US coins would be required to make 2296 01:51:11,087 --> 01:51:12,420 change for that amount of money. 2297 01:51:12,420 --> 01:51:18,770 So in the United States, the coins have values of $0.25 $0.10 $0.05, and $0.01. 2298 01:51:18,770 --> 01:51:23,460 So if I told you to make change for let's say $0.31, for example, 2299 01:51:23,460 --> 01:51:26,450 you would use one quarter for $0.25, you would 2300 01:51:26,450 --> 01:51:30,250 use one nickel for five additional cents, and then one penny. 2301 01:51:30,250 --> 01:51:33,710 And you would figure out what is the fewest number of coins required 2302 01:51:33,710 --> 01:51:37,430 to make that amount of change, and write a Python program to figure that out. 2303 01:51:37,430 --> 01:51:40,623 And as a hint, as you're trying to figure out which coin to use next, 2304 01:51:40,623 --> 01:51:42,290 you usually want to use the biggest one. 2305 01:51:42,290 --> 01:51:46,820 If I want to make change for $0.31, I want to start with a $0.25 coin 2306 01:51:46,820 --> 01:51:50,210 rather than use like 31 one cent coins, which 2307 01:51:50,210 --> 01:51:54,430 might take up quite a number of coins. 2308 01:51:54,430 --> 01:51:56,388 And so you're welcome to work on that. 2309 01:51:56,388 --> 01:51:58,930 You're welcome to continue to post on the ed discussion forum 2310 01:51:58,930 --> 01:52:01,630 if you have questions about any of these problems 2311 01:52:01,630 --> 01:52:03,780 as you go about trying to work on it. 2312 01:52:03,780 --> 01:52:06,280 And we've only just scratched the surface of Python, really. 2313 01:52:06,280 --> 01:52:09,870 If you're interested in learning more about Python and Python programming, 2314 01:52:09,870 --> 01:52:12,640 I'd encourage you to take CS50 on edX, if you haven't already, 2315 01:52:12,640 --> 01:52:14,557 where you'll learn even more about programming 2316 01:52:14,557 --> 01:52:16,770 in Python and other programming languages. 2317 01:52:16,770 --> 01:52:18,520 And if you choose to do that, that's where 2318 01:52:18,520 --> 01:52:22,605 you'll submit these problems, Mario and Cash, that you'll be working on here. 2319 01:52:22,605 --> 01:52:24,730 And I'd also note there are other seminars that are 2320 01:52:24,730 --> 01:52:26,740 coming up later this month and next. 2321 01:52:26,740 --> 01:52:28,660 There's one on SQL that I will be leading, 2322 01:52:28,660 --> 01:52:31,660 one on Linux that Kareem will be leading, and one on game development 2323 01:52:31,660 --> 01:52:32,830 that Colton will be leading. 2324 01:52:32,830 --> 01:52:36,190 So if any of these topics are topics that you're interested in exploring, 2325 01:52:36,190 --> 01:52:38,050 we hope to see you at some of those, too. 2326 01:52:38,050 --> 01:52:41,423 But before we wrap up, we would love to get your feedback on today's session. 2327 01:52:41,423 --> 01:52:43,840 And so I'm going to go ahead and put a link into the chat. 2328 01:52:43,840 --> 01:52:46,070 And I'll share my screen as well with it. 2329 01:52:46,070 --> 01:52:50,660 But if you could go to this URL, cs50.ly/feedback, 2330 01:52:50,660 --> 01:52:53,202 this will just help us to improve future sessions like these. 2331 01:52:53,202 --> 01:52:56,327 This, again, is the very first of these types of sessions that we're doing. 2332 01:52:56,327 --> 01:52:58,400 And we would love to hear what you think of that. 2333 01:52:58,400 --> 01:53:00,150 So it shouldn't take too long to fill out. 2334 01:53:00,150 --> 01:53:04,030 But if you could, take a moment to go ahead and fill out that feedback form 2335 01:53:04,030 --> 01:53:09,108 at this URL here, cs50.ly/feedback. 2336 01:53:09,108 --> 01:53:10,900 And once you filled out that feedback form, 2337 01:53:10,900 --> 01:53:13,928 that wraps up today's session on introducing Python. 2338 01:53:13,928 --> 01:53:15,470 Thank you all so much for being here. 2339 01:53:15,470 --> 01:53:18,820 Thank you to the mentors for all of your help with students today. 2340 01:53:18,820 --> 01:53:21,350 And we hope to see you at some seminars in the future. 2341 01:53:21,350 --> 01:53:23,310 Thanks, everyone. 2342 01:53:23,310 --> 01:53:24,000