Search code examples
pythonstringformatconsole-output

stuck to printing pyramid code although my code is correct but my jupyter notebook doesn't print me the exact answer


Screenshot of code and output

Code

num = int(input("Enter the number of rows:"))
for i in range(0,num):
  for j in range(0,num-i-1):
    print(end="")
  for j in range(0,i+1):
    print("*",end="")
  print()

Current output

*
**
***
****
*****
******
    

answer is not exactly the same as PYRAMID, show it in the picture


Solution

  • I think you want your pyramid to be centered and not leaning to the left side. For that, you have to adjust the spacing before and after the stars in your loops accordingly:

    Changed Code

    num = int(input("Enter the number of rows:"))
    for i in range(0,num):
      for j in range(0,num-i-1):
        print(end=" ")
      for j in range(0,i+1):
        print("*",end=" ")
      print()
    

    Output

         * 
        * * 
       * * * 
      * * * * 
     * * * * * 
    * * * * * *