I've been wanting to start writing how I solve problems for a while, and the best time to start was yesterday, the second best time is now. So here we go.
If you're familiar with CS50 and the Mario problem then you probably understand what I was trying to do, but here's a brief overview.
The Problem
The task was to print a pyramid of height specified by the user. The correct amount of whitespace, then the correct number of # blocks on each line, followed by a space, then the same amount of # blocks after the space creating something like this:
# #
## ##
### ###
The Solution
To tackle this problem, I first used the get_int function from the cs50 library. Save me having to typecast things. I like convenience!
from cs50 import get_int
Then we needed to make sure that the height is valid, and if it isn't we need to prompt the user again until a correct value is entered.
I decided to define the variable height first. Then I could check if the value was correct, and if not re-prompt the user.
height = 0
while height > 8 or height < 1:
height = get_int("Height: ")
All good so far but the next part was the toughest. We now have the height of the pyramid that will reject non-numeric heights and re-prompt if the height is invalid. Now to print the correct pyramid.
I tried a few solutions but I landed on this one because it made the most sense to me.
First, we need to enter a loop for each row of the pyramid
for i in range(1, height + 1):
I started at 1 and ended at height + 1 so that the value of i is equal to the row number.
Then, enter another loop for each column.
for i in range(1, height + 1):
for j in range(1, height + 1):
Now we need to print the correct amount of spaces before printing any #'s. I decided to do this by checking to see if j (the current column) is greater than the height - i (the current row number) and if it is, then Python knows it's time to start printing #'s instead of whitespace!
This logic works because on row i, we want i amount of hashtags and height - i amount of spaces.
# Loop through rows and then columns
for i in range(1, height + 1):
for j in range(1, height + 1):
# Start printing # after correct number of spaces
if j > (height - i):
print("#", end="")
else:
print(" ", end="")
end="" makes sure a new line isn't printed after each iteration.
Finally, we just need to print a space and the same amount of #'s after the space which is easily done.
# Print middle space and remaining #
if j == height:
print(" ", "#" * i, end="")
There's one key part I haven't mentioned. We need to print a new line after the column loops have finished so that a new row starts. Easily fixed, just print()
after the j loop and we're done!
Full code:
from cs50 import get_int
height = 0
while height > 8 or height < 1:
height = get_int("Height: ")
# Loop through rows and then columns
for i in range(1, height + 1):
for j in range(1, height + 1):
# Start printing # after correct number of spaces
if j > (height - i):
print("#", end="")
else:
print(" ", end="")
# Print middle space and remaining #
if j == height:
print(" ", "#" * i, end="")
print()
There you have it. On the way to Bowser's castle.
Top comments (0)