A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://inventwithpython.com/bigbookpython/project16.html below:

Diamonds

#16
Diamonds

This program features a small algorithm for drawing ASCII-art diamonds of various sizes. It contains functions for drawing either an outline or filled-in-style diamond of the size you dictate. These functions are good practice for a beginner; try to understand the pattern behind the diamond drawings as they increase in size.

The Program in Action

When you run diamonds.py, the output will look like this:

Diamonds, by Al Sweigart [email protected]

/\
\/

/\
\/

 /\
/  \
\  /
 \/

 /\
//\\
\\//
 \/

  /\
 /  \
/    \
\    /
 \  /
  \/

  /\
 //\\
///\\\
\\\///
 \\//
  \/
--snip--
How It Works

A helpful approach to creating this program yourself is to “draw” diamonds of several sizes in your editor first and then figure out the pattern they follow as the diamond gets bigger. This technique will help you realize that each row of an outline diamond has four parts: the number of leading spaces, the exterior forward slash, the number of interior spaces, and the exterior backslash. Filled diamonds have several interior forward slashes and backslashes rather than interior spaces. Cracking this pattern is how I wrote diamonds.py.

 1. r"""Diamonds, by Al Sweigart [email protected]
 2. Draws diamonds of various sizes.
 3. View this code at https://nostarch.com/big-book-small-python-projects
 4.                            /\       /\
 5.                           /  \     //\\
 6.             /\     /\    /    \   ///\\\
 7.            /  \   //\\  /      \ ////\\\\
 8.  /\   /\  /    \ ///\\\ \      / \\\\////
 9. /  \ //\\ \    / \\\///  \    /   \\\///
10. \  / \\//  \  /   \\//    \  /     \\//
11.  \/   \/    \/     \/      \/       \/
12. Tags: tiny, beginner, artistic"""
13.
14. def main():
15.     print('Diamonds, by Al Sweigart [email protected]')
16.
17.     # Display diamonds of sizes 0 through 6:
18.     for diamondSize in range(0, 6):
19.         displayOutlineDiamond(diamondSize)
20.         print()  # Print a newline.
21.         displayFilledDiamond(diamondSize)
22.         print()  # Print a newline.
23.
24.
25. def displayOutlineDiamond(size):
26.     # Display the top half of the diamond:
27.     for i in range(size):
28.         print(' ' * (size - i - 1), end='')  # Left side space.
29.         print('/', end='')  # Left side of diamond.
30.         print(' ' * (i * 2), end='')  # Interior of diamond.
31.         print('\\')  # Right side of diamond.
32.
33.     # Display the bottom half of the diamond:
34.     for i in range(size):
35.         print(' ' * i, end='')  # Left side space.
36.         print('\\', end='')  # Left side of diamond.
37.         print(' ' * ((size - i - 1) * 2), end='')  # Interior of diamond.
38.         print('/')  # Right side of diamond.
39.
40.
41. def displayFilledDiamond(size):
42.     # Display the top half of the diamond:
43.     for i in range(size):
44.         print(' ' * (size - i - 1), end='')  # Left side space.
45.         print('/' * (i + 1), end='')  # Left half of diamond.
46.         print('\\' * (i + 1))  # Right half of diamond.
47.
48.     # Display the bottom half of the diamond:
49.     for i in range(size):
50.         print(' ' * i, end='')  # Left side space.
51.         print('\\' * (size - i), end='')  # Left side of diamond.
52.         print('/' * (size - i))  # Right side of diamond.
53.
54.
55. # If this program was run (instead of imported), run the game:
56. if __name__ == '__main__':
57.     main()

After entering the source code and running it a few times, try making experimental changes to it. On your own, you can also try to figure out how to do the following:

Exploring the Program

Try to find the answers to the following questions. Experiment with some modifications to the code and rerun the program to see what effect the changes have.

  1. What happens when you change print('\\') on line 31 to print('@')?
  2. What happens when you change print(' ' * (i * 2), end='') on line 30 to print('@' * (i * 2), end='')?
  3. What happens when you change range(0, 6) on line 18 to range(0, 30)?
  4. What happens when you delete or comment out for i in range(size): on line 34 or on line 49?

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4