Home > front end >  Are you printing an additional character at the beginning of each line? - main error left aligned py
Are you printing an additional character at the beginning of each line? - main error left aligned py

Time:03-08

I'm currently at pset6 from cs50, mario-less. My code compiles and prints the left aligned pyramid as the problem asks, but when I do a check50, most of them fail. What is the problem?

from cs50 import get_int

# Ask user for input
n = get_int("Height: ")

# While loop to check condition
while n < 1 or n > 8:
    print("Invalid number ")
    n = get_int("Enter another number: ")

# One for loop to prin left sided piramid
for j in range(1, n   1):
    spaces = n - j   1
    print(" " * spaces   "#" * j)

CodePudding user response:

As it currently stands, for n == 5 your code will print:

     #
    ##
   ###
  ####
 #####

However, we want to avoid that extra space at the start of every line and get:

    #
   ##
  ###
 ####
#####

So just reduce the number of spaces you are adding by 1:

# One for loop to print left sided pyramid
for j in range(1, n   1):
    spaces = n - j
    print(" " * spaces   "#" * j)

CodePudding user response:

Best answered by Andrew McClement

  • Related