I currently working on CS50. There is a problem where we need to recreate half-pyramids using hashes (#). The hashes are to substitute blocks like in Mario.
This is also the first time I'm using Python. I don't really know Python. So I just try to calculate myself based on my experience from C language.
from sys import exit
from cs50 import get_int
def main():
while True:
num = get_int("Height: ")
if num >= 1 and num <= 8:
break
print_pyramid(num)
exit(0)
def print_pyramid(n):
for i in range(n):
print_spaces(i, n) # calling two function that print spaces and hash
print_hash(i) # after print every spaces and hash in each row,
print() # print new line for new row
def print_spaces(i, n):
for j in range(n-(i+1)): # those calculation means that we print space one less than the height ......# (seven spaces)
print(" ", end="") # if height = 8 8-(i+1) 'i' is increment from above function .....## (six spaces)
def print_hash(i):
for l in range(i+1): # print the hash by increasing it (i+1)
print("#", end="") # i = 0 # / i = 1 ##
l = 1 # there's two spaces between two pyramid. at the print_spaces function range(n - i+1)
n = 4 # so initialize l = 1 and n = 4, would always give (4-(1+1)) = 2
print_spaces(l, n)
for l in range(i+1): # print rigth hash 'pyramid'
print("#", end="")
main()
I always love to work with complicated idea using a lot of function for their different purpose.
I also got a 98% score from CS50.
What do you think of my code? Since this is my first time using Python, are there any Pythonic ways to simplify my program? (especially using function like I did).
Height: 4
# #
## ##
### ###
#### ####