Advertisement
acclivity

pyBuildPyramidOfBlocks

Apr 13th, 2021 (edited)
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. # Build a Pyramid of Blocks
  2. while True:
  3.     num = int(input("Enter number of blocks (1, 3, 6, 10, 15  etc.): "))
  4.     # Work out the number of rows required for the requested number of blocks
  5.     rowmax, blks = 0, 0
  6.     while blks < num:
  7.         blks += rowmax
  8.         rowmax += 1
  9.     if blks == num:
  10.         break
  11.     print("Not a valid number of blocks\n")
  12.  
  13. for row in range(rowmax -1):
  14.     sp = (rowmax - row) * 2
  15.     print(" " * sp + "[ ] " * (row+1))
  16.  
  17. # Sample output:
  18. #
  19. # Enter number of blocks (1, 3, 6, 10, 15  etc.): 15
  20. #             [ ]
  21. #           [ ] [ ]
  22. #         [ ] [ ] [ ]
  23. #       [ ] [ ] [ ] [ ]
  24. #     [ ] [ ] [ ] [ ] [ ]
  25. #
  26. # Process finished with exit code 0
  27. #
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement