1 00:00:00,000 --> 00:00:00,710 2 00:00:00,710 --> 00:00:03,050 ZAMYLA CHAN: It's a-me, Zamyla. 3 00:00:03,050 --> 00:00:07,310 Today, we're going to recreate the Mario problem, one of our very first problems 4 00:00:07,310 --> 00:00:10,970 in C. Now we're going to make it in Python. 5 00:00:10,970 --> 00:00:13,550 So if you recall from your Mario problem, 6 00:00:13,550 --> 00:00:17,120 then you'll remember that what we'd had to do was to prompt the user 7 00:00:17,120 --> 00:00:21,860 and validate their input and then draw the half pyramid. 8 00:00:21,860 --> 00:00:26,120 To prompts the user in C, CS50 provided our own library 9 00:00:26,120 --> 00:00:28,880 in which we had the function get_int. 10 00:00:28,880 --> 00:00:31,130 So in order to use this get_int function, 11 00:00:31,130 --> 00:00:35,420 you had to hashtag include the CS50 Library. 12 00:00:35,420 --> 00:00:38,780 In Python, we also provide you with the CS50 Module 13 00:00:38,780 --> 00:00:42,080 with the exact same function, get_int. 14 00:00:42,080 --> 00:00:45,350 But instead of hashtag including it, we're going to import it. 15 00:00:45,350 --> 00:00:48,110 That's the equivalent in Python. 16 00:00:48,110 --> 00:00:51,170 In order to ensure that we got proper user input, 17 00:00:51,170 --> 00:00:54,200 we typically used a do while loop in which the 18 00:00:54,200 --> 00:00:57,350 do while loop would ensure that we prompted the user at least once 19 00:00:57,350 --> 00:01:02,300 and then we would only repeat that loop if the user gave us invalid input. 20 00:01:02,300 --> 00:01:05,540 Now, Python actually doesn't have a do while loop structure. 21 00:01:05,540 --> 00:01:09,240 But Python does have an equivalent while loop. 22 00:01:09,240 --> 00:01:12,410 So let's see how we can take a while loop in Python 23 00:01:12,410 --> 00:01:15,480 and turn it into the correct functionality 24 00:01:15,480 --> 00:01:18,020 mirroring the do while loop. 25 00:01:18,020 --> 00:01:20,850 Here is my condition for the while loop I have true. 26 00:01:20,850 --> 00:01:25,010 So that means that the loop will execute at least once. 27 00:01:25,010 --> 00:01:30,360 So I'm going to prompt the user there and then if a certain condition is met, 28 00:01:30,360 --> 00:01:32,580 then I'll break out of the loop. 29 00:01:32,580 --> 00:01:37,520 But if that condition isn't met, then that loop will repeat itself. 30 00:01:37,520 --> 00:01:39,410 Another difference between C and Python is 31 00:01:39,410 --> 00:01:43,070 how we express the compound conditional expressions. 32 00:01:43,070 --> 00:01:45,560 For ors and ands, we used characters. 33 00:01:45,560 --> 00:01:50,960 But in Python, we simply just need to type those words out, or and and. 34 00:01:50,960 --> 00:01:54,500 Notice also in red, I've indicated some of the syntactical differences 35 00:01:54,500 --> 00:01:55,970 between C and Python. 36 00:01:55,970 --> 00:02:00,680 So here, I show how you need to include a colon after your condition 37 00:02:00,680 --> 00:02:03,740 in order to enter that if condition. 38 00:02:03,740 --> 00:02:07,550 Remember also that style is quite literally Python syntax 39 00:02:07,550 --> 00:02:09,289 and how it interprets the code. 40 00:02:09,289 --> 00:02:13,220 So if before you used to do your style after you wrote all of your code, 41 00:02:13,220 --> 00:02:14,510 that won't do in Python. 42 00:02:14,510 --> 00:02:19,720 So make sure to indent correctly in order for all of your code to execute. 43 00:02:19,720 --> 00:02:20,450 OK. 44 00:02:20,450 --> 00:02:23,280 So now that we've prompted and validated the user input, 45 00:02:23,280 --> 00:02:25,970 it's time to draw the half pyramid. 46 00:02:25,970 --> 00:02:30,230 The pseudocode is going to stay the same because the logic isn't changing. 47 00:02:30,230 --> 00:02:34,770 For every row, we'll want to print the requisite number of spaces, hashes, 48 00:02:34,770 --> 00:02:37,590 and then a new line after we finished each row. 49 00:02:37,590 --> 00:02:41,270 In C, we did this by executing a for loop for int i 50 00:02:41,270 --> 00:02:43,220 equal 0 all the way till height. 51 00:02:43,220 --> 00:02:47,150 And within that for loop, we printed the spaces, printed the hashes, and then 52 00:02:47,150 --> 00:02:48,480 a new line. 53 00:02:48,480 --> 00:02:51,410 So let's just translate that to Python. 54 00:02:51,410 --> 00:02:55,940 We would have a for loop for every i in range height. 55 00:02:55,940 --> 00:02:59,900 We would print the spaces, print the hashes, and then print a new line. 56 00:02:59,900 --> 00:03:04,070 From printf in C, we now use print in Python. 57 00:03:04,070 --> 00:03:08,310 By default, print will include a new line after every string. 58 00:03:08,310 --> 00:03:10,790 So if we just simply called "hello, world" 59 00:03:10,790 --> 00:03:14,449 then it would print "hello, world" with a new line included. 60 00:03:14,449 --> 00:03:16,490 Now, if we want to omit that new line, then we'll 61 00:03:16,490 --> 00:03:20,690 have to override the default by adding a comma after our string 62 00:03:20,690 --> 00:03:24,840 and then end equals with two empty quotation marks. 63 00:03:24,840 --> 00:03:29,570 Then something nifty is if I want to print something, say, 50 times, 64 00:03:29,570 --> 00:03:32,810 then all I have to do to print 50 hashes is 65 00:03:32,810 --> 00:03:37,010 to add a star 50 right after that string. 66 00:03:37,010 --> 00:03:39,470 And there we have 50 hashes. 67 00:03:39,470 --> 00:03:42,020 Much easier than the nested for loop structure 68 00:03:42,020 --> 00:03:45,170 that we had to do in Mario in C. Finally, 69 00:03:45,170 --> 00:03:48,230 if I just want to print a new line, then I simply call print 70 00:03:48,230 --> 00:03:51,290 without passing anything in it. 71 00:03:51,290 --> 00:03:53,090 And there you have your pyramid. 72 00:03:53,090 --> 00:03:57,190 My name is Zamyla, and this was Mario. 73 00:03:57,190 --> 00:03:59,768