Customisable Arrow Pattern - Change a Math Symbol to Make a New Shape
by tanish satpal in Teachers > 6
70 Views, 3 Favorites, 0 Comments
Customisable Arrow Pattern - Change a Math Symbol to Make a New Shape
This Instructable explains the Python program designed to print an arrow pattern based on user input. The pattern combines stars (*), spaces, hyphens (-), plus signs (+), and slashes (/) to form two symmetric sections (upper and lower). By analysing each line of the code, we will uncover its functionality and explore how altering mathematical symbols impacts the output in the end.
Without further ado, lets go!
Supplies
All you need is a python processing application. I am using Google Colab, as it is the easiest to work with.
Function Definition
• A function named print_arrow is defined, taking rows as a parameter. This parameter determines the size and complexity of the pattern.
Upper Section (Forward Arrow Pattern)
Inside the function,
• Loop (1 to rows): A loop iterates i from 1 to rows (inclusive).
• * Pattern: "*" is repeated i times to form an increasing pattern of stars.
• Spaces ( " " * (2 * (rows - i))): Spaces are added between the stars and hyphens, calculated as 2 * (rows - i) to center-align the arrow shape.
• - Pattern: Hyphens (-) are repeated i times, forming the trailing section of the arrow.
Example Output (for Rows = 3):
To give a basic explanation of this step, lets run this code when rows = 3. You will get something like this:
As you can see, 2 vividly visible triangles are generated, but thats not it! We need to go further.
Lower Section (Backward Arrow Pattern)
Continuing on, in the function we add a second loop, which reverses the same function. It might not be clear, but it is present.
• Loop (rows to 1): A loop iterates i from rows down to 1.
• + Pattern: "+" is repeated i times to form a decreasing pattern of plus signs.
• Spaces ( " " * (2 * (rows - i))): Spaces are inserted, maintaining alignment with the upper section.
• / Pattern: Slashes (/) are repeated i times, forming the trailing section of the arrow.
Example Output (for Rows = 3):
Considering only the bottom portion, we can get an output like this:
However, by making both codes run together, we get this:
It may not look significant, but it is the base of the code. By altering the row number and changing the operations, things change drastically!
User Input
Till now, we have been using 3 as the number of rows while testing. But this code is meant to be adaptive, so we give the user access to inputting their own number of rows.
Input Validation
However, there are people who would input negative numbers or imaginary numbers. To ensure that this doesn't lead to an error, we make a failsafe, to ensure that positive integers are inputted only.
• If the input is positive, the print_arrow function is called. Otherwise, an error message prompts the user to provide a valid positive integer.
Error Handling
Then, we have people who put decimals or text into the number of rows. This must not happen, so we have this failsafe, which uses the valueError function that is built into Python.
• If the input is non-numeric, the program catches the error and displays a helpful message.
Making Changes
Lets make changes to the input. For this, I will use 16 as the number of rows,
Upper Section Change
• rows // i: Replaces rows - i with integer division. This creates uneven spacing because the number of spaces depends on the ratio of rows to i, which decreases as i increases.
• Impact: The alignment of stars and hyphens becomes non-linear, introducing a unique asymmetry.
Lower Section Change
• Effect: Similarly, the spaces in the lower section now vary dynamically with the decreasing value of i, leading to an uneven alignment of plus signs and slashes.
This gives a swirly effect which looks great when the number of rows are increased
Conclusion
This Python program creatively uses loops, string multiplication, and input validation to produce a symmetric arrow pattern. Adjusting mathematical symbols or ranges alters the design, providing a versatile framework for pattern generation. By understanding its logic, users can customize and expand this program for various designs.
This truly was amazing for me. Create replicas of this program and feel free to adjust even more operations and create unique patterns, or even increase the number of patterns altogether! Post it on the "I Made It" section, where I will personally comment on your expertise.
And the Google Colab link for you to refer too: https://colab.research.google.com/drive/1JRapgvN5cfsyea6Zu8M3TeRaNvxIVjO5?usp=sharing