00:00:00,710 --> 00:00:03,050 ZAMYLA CHAN: It's a-me, Zamyla. Today, we're going to recreate the Mario problem, one of our very first problems in C. Now we're going to make it in Python. So if you recall from your Mario problem, then you'll remember that what we'd had to do was to prompt the user and validate their input and then draw the half pyramid. To prompts the user in C, CS50 provided our own library in which we had the function get_int. So in order to use this get_int function, you had to hashtag include the CS50 Library. In Python, we also provide you with the CS50 Module with the exact same function, get_int. But instead of hashtag including it, we're going to import it. That's the equivalent in Python. In order to ensure that we got proper user input, we typically used a do while loop in which the do while loop would ensure that we prompted the user at least once and then we would only repeat that loop if the user gave us invalid input. Now, Python actually doesn't have a do while loop structure. But Python does have an equivalent while loop. So let's see how we can take a while loop in Python and turn it into the correct functionality mirroring the do while loop. Here is my condition for the while loop I have true. So that means that the loop will execute at least once. So I'm going to prompt the user there and then if a certain condition is met, then I'll break out of the loop. But if that condition isn't met, then that loop will repeat itself. Another difference between C and Python is how we express the compound conditional expressions. For ors and ands, we used characters. But in Python, we simply just need to type those words out, or and and. Notice also in red, I've indicated some of the syntactical differences between C and Python. So here, I show how you need to include a colon after your condition in order to enter that if condition. Remember also that style is quite literally Python syntax and how it interprets the code. So if before you used to do your style after you wrote all of your code, that won't do in Python. So make sure to indent correctly in order for all of your code to execute. OK. So now that we've prompted and validated the user input, it's time to draw the half pyramid. The pseudocode is going to stay the same because the logic isn't changing. For every row, we'll want to print the requisite number of spaces, hashes, and then a new line after we finished each row. In C, we did this by executing a for loop for int i equal 0 all the way till height. And within that for loop, we printed the spaces, printed the hashes, and then a new line. So let's just translate that to Python. We would have a for loop for every i in range height. We would print the spaces, print the hashes, and then print a new line. From printf in C, we now use print in Python. By default, print will include a new line after every string. So if we just simply called "hello, world" then it would print "hello, world" with a new line included. Now, if we want to omit that new line, then we'll have to override the default by adding a comma after our string and then end equals with two empty quotation marks. Then something nifty is if I want to print something, say, 50 times, then all I have to do to print 50 hashes is to add a star 50 right after that string. And there we have 50 hashes. Much easier than the nested for loop structure that we had to do in Mario in C. Finally, if I just want to print a new line, then I simply call print without passing anything in it. And there you have your pyramid. My name is Zamyla, and this was Mario.