Last active
January 31, 2023 22:18
-
-
Save Tomi-3-0/987ca1686cc5055dbdb096826d419f31 to your computer and use it in GitHub Desktop.
Mario hash blocks using python (cs50 assignment)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def main(): | |
height = get_height() | |
print_hashes(height) | |
def print_hashes(height): | |
# loop through the rows | |
for i in range(height): | |
# number of hashes per row | |
hashes = i + 1; | |
# number of spaces from left | |
spaces = height - i - 1 | |
# for each column, first print the spaces without | |
for i in range(spaces, 0, -1): | |
print(" ", end="") | |
# for each column print the hashes | |
for k in range(hashes): | |
print("#", end="") | |
# print 2 spaces between left and right pyramids | |
print(" ", end="") | |
# print hashes for right pyramid | |
for i in range(hashes): | |
print("#", end="") | |
# print empty spaces | |
print("\n", end="") | |
def get_height(): | |
# make sure height is between 0 and 8 | |
while True: | |
try: | |
x = int(input("Height: ")) | |
# make sure height is between 0 and 8 | |
if x > 0 and x < 9: | |
return x | |
except ValueError: | |
print("Height is not an integer") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment