Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python What's an alternate solution to building a pyramid?

choppingbl0ck

New Coder
This is what I have so far. I'm told it's not acceptable. Any ideas on a different way to do this? I have to use loops

Python:
def print_pyramid(str,h):
    if str=="right":
        for i in range(h):
            print('#' * (i+1))
 
    elif str=="left":
        for i in range(h):
            print(' '*(h-i) + '#'*(i+1))
    elif str=="both":
        for i in range(h):
            print(' '*(h-i-1) + '#'*(2*i))




Here is the question


NSBW7.jpg
 
This is what I have so far. I'm told it's not acceptable. Any ideas on a different way to do this? I have to use loops

Python:
def print_pyramid(str,h):
    if str=="right":
        for i in range(h):
            print('#' * (i+1))
 
    elif str=="left":
        for i in range(h):
            print(' '*(h-i) + '#'*(i+1))
    elif str=="both":
        for i in range(h):
            print(' '*(h-i-1) + '#'*(2*i))




Here is the question


NSBW7.jpg
I do not think there is necessarily anything wrong with the way your code functions. More, it is probably the way it is written. First, you use `str` as a variable. NEVER USE the same names for variables that Python uses. Python uses `str` for the type string, and for the function converting to a string. I would recommend using `alignment`. Second, you use undescriptive variable names in the for loops. You should always use descriptive variable names to make it easier for someone else to understand what your program is doing, like `index`. Thirdly (and this is more of a picky point), but I would recommend using type hinting so someone looking at your code knows what type the arguments are.



Python:
def print_pyramid(alignment: str, height: int) -> None:
    if alignment == "right":
        for index in range(height):
          print(“#” * (index + 1))

   elif alignment == "left":
      for index in range(height):
         print(“” * (height - index) + “#” * (index + 1))

   elif alignment == "both":
      for index in range(height):
         print(“ “ * (height – index - 1) + “#” * (2 * index))
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom