Skip to content

Printing Pyramid Patterns in Python#78

Open
wajeehacom wants to merge 1 commit intoGrow-with-Open-Source:mainfrom
wajeehacom:feature01
Open

Printing Pyramid Patterns in Python#78
wajeehacom wants to merge 1 commit intoGrow-with-Open-Source:mainfrom
wajeehacom:feature01

Conversation

@wajeehacom
Copy link
Copy Markdown

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

  • Added pyramid_pattern.py in the branch feature/pyramid-pattern.
  • Prints a symmetric pyramid pattern using asterisks (*).

@github-actions
Copy link
Copy Markdown

👋 @wajeehacom
Thank you for raising your pull request.
Please make sure you have followed our contributing guidelines. We will review it as soon as possible.

Copy link
Copy Markdown
Contributor

@iamwatchdogs iamwatchdogs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @wajeehacom, please review the change requests and make the necessary changes to proceed.

Comment on lines +2 to +11
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()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make use of Pythonic features to write more effective & clean code.

Suggested change
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make a habit of using name-main idiom, even though it feels like it won't make much difference in scripts like this one.

Suggested change
full_pyramid(5)
if __name__ == "__main__":
full_pyramid(5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants