[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:00.15,0:00:01.62,Default,,0000,0000,0000,,- [Instructor] We can\Nuse Boolean expressions Dialogue: 0,0:00:01.62,0:00:03.39,Default,,0000,0000,0000,,to ask questions in our programs, Dialogue: 0,0:00:03.39,0:00:07.02,Default,,0000,0000,0000,,but how can we branch control\Nflow based on the answer? Dialogue: 0,0:00:07.02,0:00:09.75,Default,,0000,0000,0000,,To do that, we need conditionals. Dialogue: 0,0:00:09.75,0:00:12.48,Default,,0000,0000,0000,,Conditionals form the basis of selection. Dialogue: 0,0:00:12.48,0:00:14.01,Default,,0000,0000,0000,,They allow the computer to decide Dialogue: 0,0:00:14.01,0:00:16.65,Default,,0000,0000,0000,,which code to run depending\Non whether the answer Dialogue: 0,0:00:16.65,0:00:20.94,Default,,0000,0000,0000,,to a specific question or\Ncondition is true or false. Dialogue: 0,0:00:20.94,0:00:24.00,Default,,0000,0000,0000,,The first piece of a\Nconditional is the IF statement. Dialogue: 0,0:00:24.00,0:00:26.64,Default,,0000,0000,0000,,We start an IF statement\Nwith the keyword If. Dialogue: 0,0:00:26.64,0:00:28.60,Default,,0000,0000,0000,,Then we add our condition, Dialogue: 0,0:00:28.60,0:00:29.88,Default,,0000,0000,0000,,which can be any boolean expression. Dialogue: 0,0:00:29.88,0:00:32.74,Default,,0000,0000,0000,,We always end this line with a colon Dialogue: 0,0:00:32.74,0:00:34.32,Default,,0000,0000,0000,,because an IF statement\Ncomes in two parts. Dialogue: 0,0:00:34.32,0:00:36.36,Default,,0000,0000,0000,,This first part, which is the condition Dialogue: 0,0:00:36.36,0:00:39.06,Default,,0000,0000,0000,,and the second part, which\Nis the instructions to run Dialogue: 0,0:00:39.06,0:00:41.58,Default,,0000,0000,0000,,if that condition evaluates to true. Dialogue: 0,0:00:41.58,0:00:45.21,Default,,0000,0000,0000,,In Python, we indicate those\Ninstructions using indentation. Dialogue: 0,0:00:45.21,0:00:47.67,Default,,0000,0000,0000,,Any lines of code immediately\Nfollowing that condition Dialogue: 0,0:00:47.67,0:00:49.53,Default,,0000,0000,0000,,that are indented one level in Dialogue: 0,0:00:49.53,0:00:52.20,Default,,0000,0000,0000,,are considered inside that IF statement, Dialogue: 0,0:00:52.20,0:00:54.09,Default,,0000,0000,0000,,if the condition evaluates to true, Dialogue: 0,0:00:54.09,0:00:56.85,Default,,0000,0000,0000,,the computer will go on to\Nexecute all of the instructions Dialogue: 0,0:00:56.85,0:00:59.26,Default,,0000,0000,0000,,indented inside the IF statement in order. Dialogue: 0,0:00:59.26,0:01:01.23,Default,,0000,0000,0000,,When it's done, it'll go on to execute Dialogue: 0,0:01:01.23,0:01:03.18,Default,,0000,0000,0000,,the rest of the program as normal. Dialogue: 0,0:01:03.18,0:01:04.89,Default,,0000,0000,0000,,If the condition evaluates to false, Dialogue: 0,0:01:04.89,0:01:07.85,Default,,0000,0000,0000,,the computer will skip\Nall the lines of code Dialogue: 0,0:01:07.85,0:01:09.18,Default,,0000,0000,0000,,that are indented inside\Nof that IF statement. Dialogue: 0,0:01:09.18,0:01:12.21,Default,,0000,0000,0000,,Instead, it'll jump directly\Nto the next line of code Dialogue: 0,0:01:12.21,0:01:13.14,Default,,0000,0000,0000,,that's outside of the IF statement. Dialogue: 0,0:01:13.14,0:01:14.52,Default,,0000,0000,0000,,That means the first line of code Dialogue: 0,0:01:14.52,0:01:16.35,Default,,0000,0000,0000,,that is indented at the same level Dialogue: 0,0:01:16.35,0:01:18.21,Default,,0000,0000,0000,,as that initial if. Dialogue: 0,0:01:18.21,0:01:19.62,Default,,0000,0000,0000,,Let's try an example. Dialogue: 0,0:01:19.62,0:01:22.18,Default,,0000,0000,0000,,Here we have the Boolean expression, Dialogue: 0,0:01:22.18,0:01:24.48,Default,,0000,0000,0000,,num_orders equals equals\N10, which asks the question, Dialogue: 0,0:01:24.48,0:01:26.31,Default,,0000,0000,0000,,is this the customer's 10th order? Dialogue: 0,0:01:26.31,0:01:29.43,Default,,0000,0000,0000,,We can then use this Boolean\Nvalue in our IF statement. Dialogue: 0,0:01:29.43,0:01:31.23,Default,,0000,0000,0000,,We'll indent one print function call Dialogue: 0,0:01:31.23,0:01:33.96,Default,,0000,0000,0000,,inside of the IF\Nstatement and one outside. Dialogue: 0,0:01:33.96,0:01:37.59,Default,,0000,0000,0000,,The one inside will only\Nexecute if it is the 10th order, Dialogue: 0,0:01:37.59,0:01:39.87,Default,,0000,0000,0000,,that is if the variable is 10th order Dialogue: 0,0:01:39.87,0:01:42.09,Default,,0000,0000,0000,,contains the Boolean value True. Dialogue: 0,0:01:42.09,0:01:44.92,Default,,0000,0000,0000,,We can also write this\Nwithout the variable Dialogue: 0,0:01:44.92,0:01:47.01,Default,,0000,0000,0000,,where we just inline that\Nwhole Boolean expression Dialogue: 0,0:01:47.01,0:01:48.46,Default,,0000,0000,0000,,in the IF statement. Dialogue: 0,0:01:48.46,0:01:50.19,Default,,0000,0000,0000,,Let's trace how the\Ncomputer executes this. Dialogue: 0,0:01:50.19,0:01:51.36,Default,,0000,0000,0000,,It starts with the first line Dialogue: 0,0:01:51.36,0:01:54.36,Default,,0000,0000,0000,,and assigns a value 10 to\Nthe variable num_orders. Dialogue: 0,0:01:54.36,0:01:55.92,Default,,0000,0000,0000,,Then it hits this IF statement, Dialogue: 0,0:01:55.92,0:01:58.89,Default,,0000,0000,0000,,so first it evaluates\Nthe Boolean expression. Dialogue: 0,0:01:58.89,0:02:02.88,Default,,0000,0000,0000,,10 is equal to 10, so\Nthis evaluates to true. Dialogue: 0,0:02:02.88,0:02:04.02,Default,,0000,0000,0000,,Because it's true, Dialogue: 0,0:02:04.02,0:02:06.75,Default,,0000,0000,0000,,the computer will\Nexecute the lines of code Dialogue: 0,0:02:06.75,0:02:08.28,Default,,0000,0000,0000,,indented inside of the IF statement. Dialogue: 0,0:02:08.28,0:02:11.42,Default,,0000,0000,0000,,First, it'll print, "Your order is free!" Dialogue: 0,0:02:11.42,0:02:12.66,Default,,0000,0000,0000,,And then it'll assign the value zero Dialogue: 0,0:02:12.66,0:02:14.43,Default,,0000,0000,0000,,to the variable num_orders. Dialogue: 0,0:02:14.43,0:02:16.55,Default,,0000,0000,0000,,Then it just keeps moving through Dialogue: 0,0:02:16.55,0:02:17.85,Default,,0000,0000,0000,,the rest of the program in order, Dialogue: 0,0:02:17.85,0:02:20.94,Default,,0000,0000,0000,,so it'll print the value of\Nnum_orders, which is now zero. Dialogue: 0,0:02:20.94,0:02:22.97,Default,,0000,0000,0000,,Now let's say instead num_orders Dialogue: 0,0:02:22.97,0:02:24.22,Default,,0000,0000,0000,,was originally set to three. Dialogue: 0,0:02:24.22,0:02:25.29,Default,,0000,0000,0000,,When the computer executes this program, Dialogue: 0,0:02:25.29,0:02:27.42,Default,,0000,0000,0000,,it'll assign the value\Nthree to num_orders, Dialogue: 0,0:02:27.42,0:02:29.07,Default,,0000,0000,0000,,evaluate the Boolean expression. Dialogue: 0,0:02:29.07,0:02:31.50,Default,,0000,0000,0000,,Three is not equal to\N10, so this is false, Dialogue: 0,0:02:31.50,0:02:33.00,Default,,0000,0000,0000,,and then because it's false, Dialogue: 0,0:02:33.00,0:02:34.74,Default,,0000,0000,0000,,we'll skip the lines of code indented Dialogue: 0,0:02:34.74,0:02:36.89,Default,,0000,0000,0000,,inside the IF statement. Dialogue: 0,0:02:36.89,0:02:38.80,Default,,0000,0000,0000,,It'll jump to the next\Nline of code outside, Dialogue: 0,0:02:38.80,0:02:40.100,Default,,0000,0000,0000,,which is print num_orders,\Nso it'll print three. Dialogue: 0,0:02:40.100,0:02:43.65,Default,,0000,0000,0000,,Note that if we didn't\Nindent the line of code Dialogue: 0,0:02:43.65,0:02:46.29,Default,,0000,0000,0000,,num_orders equal zero,\Nit would be considered Dialogue: 0,0:02:46.29,0:02:49.41,Default,,0000,0000,0000,,outside of the IF statement,\Nso in the false case, Dialogue: 0,0:02:49.41,0:02:51.73,Default,,0000,0000,0000,,num_orders would be set to zero, Dialogue: 0,0:02:51.73,0:02:53.32,Default,,0000,0000,0000,,and then it would print zero. Dialogue: 0,0:02:53.32,0:02:54.99,Default,,0000,0000,0000,,In the true case, we would\Nprint, "Your order is free." Dialogue: 0,0:02:54.99,0:02:56.55,Default,,0000,0000,0000,,Then we would set num_orders to zero, Dialogue: 0,0:02:56.55,0:02:58.68,Default,,0000,0000,0000,,and then we would print zero. Dialogue: 0,0:02:58.68,0:03:01.08,Default,,0000,0000,0000,,You can write IF statements\Nwith any Boolean expressions Dialogue: 0,0:03:01.08,0:03:02.85,Default,,0000,0000,0000,,as conditions, and you can put any Dialogue: 0,0:03:02.85,0:03:05.82,Default,,0000,0000,0000,,and as many instructions\Nas you want inside of them. Dialogue: 0,0:03:05.82,0:03:08.20,Default,,0000,0000,0000,,With this conditional, I could make sure Dialogue: 0,0:03:08.20,0:03:09.73,Default,,0000,0000,0000,,to only ask the follow-up question, Dialogue: 0,0:03:09.73,0:03:11.61,Default,,0000,0000,0000,,chicken or tofu, if the\Nuser ordered pad Thai, Dialogue: 0,0:03:11.61,0:03:13.44,Default,,0000,0000,0000,,because if they order the papaya salad, Dialogue: 0,0:03:13.44,0:03:15.12,Default,,0000,0000,0000,,that question doesn't make sense. Dialogue: 0,0:03:15.12,0:03:18.57,Default,,0000,0000,0000,,Note that in most IDEs we\Nindent using the tab key. Dialogue: 0,0:03:18.57,0:03:19.87,Default,,0000,0000,0000,,Our standard lines of code Dialogue: 0,0:03:20.72,0:03:21.69,Default,,0000,0000,0000,,should line up directly\Nwith the left margin. Dialogue: 0,0:03:21.69,0:03:24.54,Default,,0000,0000,0000,,That is, there should be no\Nspaces or indents before them. Dialogue: 0,0:03:24.54,0:03:26.79,Default,,0000,0000,0000,,Lines of code indented\Ninside of an IF statement Dialogue: 0,0:03:26.79,0:03:29.04,Default,,0000,0000,0000,,should be one tab key over. Dialogue: 0,0:03:29.04,0:03:31.81,Default,,0000,0000,0000,,Getting the indentation right is crucial Dialogue: 0,0:03:31.81,0:03:32.88,Default,,0000,0000,0000,,because this is the only\Nway we can tell the computer Dialogue: 0,0:03:32.88,0:03:34.74,Default,,0000,0000,0000,,which lines of code are\Ninside the IF statement Dialogue: 0,0:03:34.74,0:03:36.63,Default,,0000,0000,0000,,and which are outside. Dialogue: 0,0:03:36.63,0:03:37.95,Default,,0000,0000,0000,,We need to be careful here though Dialogue: 0,0:03:37.95,0:03:39.66,Default,,0000,0000,0000,,because now our control flow branches. Dialogue: 0,0:03:39.66,0:03:43.52,Default,,0000,0000,0000,,If the user orders pad Thai,\Nthis program works fine, Dialogue: 0,0:03:43.52,0:03:46.08,Default,,0000,0000,0000,,but if the user orders something\Nelse, we get a name error. Dialogue: 0,0:03:46.08,0:03:48.18,Default,,0000,0000,0000,,The variable protein is only defined Dialogue: 0,0:03:48.18,0:03:49.71,Default,,0000,0000,0000,,inside the IF statement. Dialogue: 0,0:03:49.71,0:03:51.00,Default,,0000,0000,0000,,If the condition is false, Dialogue: 0,0:03:51.00,0:03:53.01,Default,,0000,0000,0000,,this assignment statement doesn't execute, Dialogue: 0,0:03:53.01,0:03:56.01,Default,,0000,0000,0000,,so the computer doesn't\Nknow a variable protein. Dialogue: 0,0:03:56.01,0:03:57.96,Default,,0000,0000,0000,,To fix this, we either need to make sure Dialogue: 0,0:03:57.96,0:03:59.85,Default,,0000,0000,0000,,we're only accessing the variable protein Dialogue: 0,0:03:59.85,0:04:03.00,Default,,0000,0000,0000,,inside the IF statement where\Nwe're guaranteed it exists, Dialogue: 0,0:04:03.00,0:04:05.40,Default,,0000,0000,0000,,or we need to initialize\Nthe variable protein Dialogue: 0,0:04:05.40,0:04:07.50,Default,,0000,0000,0000,,before the IF statement, Dialogue: 0,0:04:07.50,0:04:08.85,Default,,0000,0000,0000,,which guarantees that it's always defined. Dialogue: 0,0:04:08.85,0:04:10.68,Default,,0000,0000,0000,,It's common in situations like this Dialogue: 0,0:04:10.68,0:04:11.79,Default,,0000,0000,0000,,to initialize the variable Dialogue: 0,0:04:11.79,0:04:14.30,Default,,0000,0000,0000,,to an empty or placeholder value Dialogue: 0,0:04:14.30,0:04:16.38,Default,,0000,0000,0000,,like the empty string or no protein. Dialogue: 0,0:04:16.38,0:04:19.02,Default,,0000,0000,0000,,Now there's no error on\Neither possible path. Dialogue: 0,0:04:19.02,0:04:22.08,Default,,0000,0000,0000,,If the order is equal to\Npad Thai or if it's not. Dialogue: 0,0:04:22.08,0:04:25.42,Default,,0000,0000,0000,,Now that our control flow\Nbranches, when we run a program, Dialogue: 0,0:04:25.42,0:04:26.54,Default,,0000,0000,0000,,we're only testing one Dialogue: 0,0:04:26.54,0:04:29.01,Default,,0000,0000,0000,,of perhaps many possible passive\Nexecution through the code. Dialogue: 0,0:04:29.01,0:04:30.45,Default,,0000,0000,0000,,Just because it works for one case Dialogue: 0,0:04:30.45,0:04:31.86,Default,,0000,0000,0000,,doesn't mean it works for the other, Dialogue: 0,0:04:31.86,0:04:33.54,Default,,0000,0000,0000,,and it's up to us to test for that. Dialogue: 0,0:04:33.54,0:04:35.61,Default,,0000,0000,0000,,Otherwise, it'll be our users who suffer Dialogue: 0,0:04:35.61,0:04:37.81,Default,,0000,0000,0000,,when they're the first\Nones to find the bug.