1 00:00:00,000 --> 00:00:00,720 2 00:00:00,720 --> 00:00:03,030 ZAMYLA CHAN: It's me, Zamyla. 3 00:00:03,030 --> 00:00:05,940 Today we're going to recreate the Mario problem. 4 00:00:05,940 --> 00:00:10,950 One of our very first problems in C, now we're going to make it in Python. 5 00:00:10,950 --> 00:00:13,560 So if you recall from your Mario problem, 6 00:00:13,560 --> 00:00:17,130 then you'll remember that what we had to do was to prompt the user 7 00:00:17,130 --> 00:00:21,600 and validate their input, and then draw pyramid. 8 00:00:21,600 --> 00:00:25,800 To prompt the user in C, CS50 provided our own library 9 00:00:25,800 --> 00:00:28,560 in which we had the function get_int. 10 00:00:28,560 --> 00:00:30,930 So in order to use this get_int function you 11 00:00:30,930 --> 00:00:35,070 had to hashtag include the CS50 library. 12 00:00:35,070 --> 00:00:38,460 In Python we also provide you with the CS50 module 13 00:00:38,460 --> 00:00:41,730 with the exact same function get_int. 14 00:00:41,730 --> 00:00:45,090 But instead of hashtag including it, we're going to import it. 15 00:00:45,090 --> 00:00:47,790 That's the equivalent in Python. 16 00:00:47,790 --> 00:00:50,820 In order to ensure that we got proper user input, 17 00:00:50,820 --> 00:00:55,080 we typically used a do-while loop in which the do-while loop would ensure 18 00:00:55,080 --> 00:00:59,250 that we prompted the user at least once, and then we would only repeat that loop 19 00:00:59,250 --> 00:01:01,950 if the user gave us invalid input. 20 00:01:01,950 --> 00:01:05,220 Now Python actually doesn't have a do-while loop structure, 21 00:01:05,220 --> 00:01:08,890 but Python does have an equivalent while loop. 22 00:01:08,890 --> 00:01:12,090 So let's see how we can take a while loop in Python 23 00:01:12,090 --> 00:01:15,150 and turn it into the correct functionality, 24 00:01:15,150 --> 00:01:17,560 mirroring the do-while loop. 25 00:01:17,560 --> 00:01:20,500 Here, as my condition for the while loop, I have true. 26 00:01:20,500 --> 00:01:24,690 So that means that the loop will execute at least once. 27 00:01:24,690 --> 00:01:29,340 So I'm going to prompt the user there and then, if a certain condition 28 00:01:29,340 --> 00:01:32,230 is met, I'll break out of the loop. 29 00:01:32,230 --> 00:01:37,200 But if that condition isn't met, then that loop will repeat itself. 30 00:01:37,200 --> 00:01:39,090 Another difference between C and Python is 31 00:01:39,090 --> 00:01:42,750 how we express the compound conditional expressions. 32 00:01:42,750 --> 00:01:46,350 For ors and ands, we used two characters, but in Python 33 00:01:46,350 --> 00:01:50,640 we simply just need to type those words out, "Or" and "And." 34 00:01:50,640 --> 00:01:54,180 Notice also in red I've indicated some of the syntactical differences 35 00:01:54,180 --> 00:01:55,650 between C and Python. 36 00:01:55,650 --> 00:02:00,330 So here I show how you need to include a colon after your condition 37 00:02:00,330 --> 00:02:03,390 in order to enter that if condition. 38 00:02:03,390 --> 00:02:07,200 Remember also that style is quite literally Python syntax 39 00:02:07,200 --> 00:02:08,970 and how it interprets the code. 40 00:02:08,970 --> 00:02:12,900 So, if before you used to do your style after you wrote all of your code, 41 00:02:12,900 --> 00:02:14,160 that won't do in Python. 42 00:02:14,160 --> 00:02:19,080 So make sure to indent correctly in order for all of your code to execute. 43 00:02:19,080 --> 00:02:21,630 Now that we've prompted and validated the user input, 44 00:02:21,630 --> 00:02:24,820 the next thing that we have to do is to draw the pyramid. 45 00:02:24,820 --> 00:02:28,530 Now the logic hasn't changed so the pseudocode also hasn't changed. 46 00:02:28,530 --> 00:02:32,520 For every row we'll want to print the left pyramid, spaces first, then 47 00:02:32,520 --> 00:02:37,410 hashes, print the gap, two spaces, and then print the right pyramid. 48 00:02:37,410 --> 00:02:41,220 Printing just the hashes without any trailing spaces, those aren't needed. 49 00:02:41,220 --> 00:02:43,830 You'll notice that the pseudocode is exactly the same. 50 00:02:43,830 --> 00:02:46,750 No logic has changed for this problem. 51 00:02:46,750 --> 00:02:48,960 So the only rule that changes is the syntax. 52 00:02:48,960 --> 00:02:51,120 So how do we print in Python? 53 00:02:51,120 --> 00:02:55,440 Well printing in Python comes by default with a new line. 54 00:02:55,440 --> 00:02:58,560 So if I simply included "Hello, world" in the string, 55 00:02:58,560 --> 00:03:00,900 then it would print a new line after that. 56 00:03:00,900 --> 00:03:04,950 So if I want to override this, then I'd include a comma after the string, 57 00:03:04,950 --> 00:03:09,610 and then indicate end equals with two empty quotation marks. 58 00:03:09,610 --> 00:03:12,510 Now if I wanted to print something, say 50 times, 59 00:03:12,510 --> 00:03:16,350 then I would include a star 50 right after the string, 60 00:03:16,350 --> 00:03:19,650 and there I would have 50 hashes printed out. 61 00:03:19,650 --> 00:03:22,890 Finally to just print a new line, I would call print, 62 00:03:22,890 --> 00:03:26,370 and I just wouldn't pass anything in, and I'd have any new line. 63 00:03:26,370 --> 00:03:28,800 So there we have our Mario pyramid. 64 00:03:28,800 --> 00:03:32,900 My name is Zamyla, and this was Mario. 65 00:03:32,900 --> 00:03:35,921