Advertisement
asweigart

diamond2.py

Aug 2nd, 2022 (edited)
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. """
  2. diamond2.py
  3. This program prints a centered row of Os of varying sizes:
  4. O
  5. O
  6. OOO
  7. OOOOO
  8. OOOOOOO
  9. OOOOOOOOO
  10. OOOOOOO
  11. OOOOOOOOO
  12. OOOOOOO
  13. OOOOO
  14. OOOOOOO
  15. OOOOOOOOO
  16. OOOOOOOOO
  17. OOOOOOO
  18. OOOOO
  19. OOOOOOO
  20. OOOOO
  21. OOOOOOO
  22. OOOOO
  23. OOOOOOO
  24. OOOOO
  25. """
  26.  
  27. import random, time
  28. # Experiment with changing these CONSTANT variable values:
  29. MAX_SIZE = 10
  30. DIAMOND_CHAR = 'O'
  31.  
  32. oCount = 1
  33.  
  34. while True:
  35. if random.randint(0, 1) == 0:
  36. # Increase the size of the diamond
  37. if oCount <= MAX_SIZE - 2: # Only increase if it won't increase beyond MAX_SIZE.
  38. oCount = oCount + 2
  39. else:
  40. # Decrease the size of the diamond
  41. if oCount >= 3: # Only decrease if it won't decrease to less than 1.
  42. oCount = oCount - 2
  43.  
  44. spaceCount = (MAX_SIZE - oCount) // 2
  45. print(' ' * spaceCount + DIAMOND_CHAR * oCount)
  46. time.sleep(0.1)
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement