Printing Pyramid Patterns in Python#78
Conversation
|
👋 @wajeehacom |
iamwatchdogs
left a comment
There was a problem hiding this comment.
Hi @wajeehacom, please review the change requests and make the necessary changes to proceed.
| def full_pyramid(n): | ||
| for i in range(1, n + 1): | ||
| # Print leading spaces | ||
| for j in range(n - i): | ||
| print(" ", end="") | ||
|
|
||
| # Print asterisks for the current row | ||
| for k in range(1, 2*i): | ||
| print("*", end="") | ||
| print() |
There was a problem hiding this comment.
Make use of Pythonic features to write more effective & clean code.
| def full_pyramid(n): | |
| for i in range(1, n + 1): | |
| # Print leading spaces | |
| for j in range(n - i): | |
| print(" ", end="") | |
| # Print asterisks for the current row | |
| for k in range(1, 2*i): | |
| print("*", end="") | |
| print() | |
| def full_pyramid(n): | |
| for i in range(1, n + 1): | |
| # Print leading spaces | |
| print(" " * (n - i), end="") | |
| # Print asterisks for the current row | |
| print("*" * (2*i-1)) |
Tip
Just for educational purposes, here's an oneliner version:
(lambda n : print('\n'.join([ f'{" " * (n-i)}{"*" * (2*i-1)}' for i in range(1, n+1) ])))(int(input('Size of triangle: ')))| print("*", end="") | ||
| print() | ||
|
|
||
| full_pyramid(5) No newline at end of file |
There was a problem hiding this comment.
Make a habit of using name-main idiom, even though it feels like it won't make much difference in scripts like this one.
| full_pyramid(5) | |
| if __name__ == "__main__": | |
| full_pyramid(5) |
|
This PR is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 10 days. |
|
This PR was closed because it has been stalled for 10 days with no activity. |
Description
This pull request adds a Python program that prints a pyramid pattern based on user input. The program demonstrates nested loops and formatting techniques.
Changes made
pyramid_pattern.pyin the branchfeature/pyramid-pattern.