ZAMYLA CHAN: It's 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 had to do was to prompt the user
and validate their input, and then draw pyramid.
To prompt 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, as 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, 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 two 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.
Now that we've prompted and validated the user input,
the next thing that we have to do is to draw the pyramid.
Now the logic hasn't changed so the pseudocode also hasn't changed.
For every row we'll want to print the left pyramid, spaces first, then
hashes, print the gap, two spaces, and then print the right pyramid.
Printing just the hashes without any trailing spaces, those aren't needed.
You'll notice that the pseudocode is exactly the same.
No logic has changed for this problem.
So the only rule that changes is the syntax.
So how do we print in Python?
Well printing in Python comes by default with a new line.
So if I simply included "Hello, world" in the string,
then it would print a new line after that.
So if I want to override this, then I'd include a comma after the string,
and then indicate end equals with two empty quotation marks.
Now if I wanted to print something, say 50 times,
then I would include a star 50 right after the string,
and there I would have 50 hashes printed out.
Finally to just print a new line, I would call print,
and I just wouldn't pass anything in, and I'd have any new line.
So there we have our Mario pyramid.
My name is Zamyla, and this was Mario.