3

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)
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
stefanp
  • 37
  • 3
  • What is get_int? Is this what you need to implement? Do you have a start? – daemon Mar 07 '22 at 22:59
  • yes, it specifies in description to import get_int function from cs50 library – stefanp Mar 07 '22 at 23:06
  • Is this for a class? What does it need to do? We dont have access to cs50? – daemon Mar 07 '22 at 23:11
  • i`m currently doing the cs50x course from Harvard for free and i`m on week 6, where I have to print a left sided piramid out of hashes. My code seems to work in their web visual studio, but it doesnt pass the test ccheck. i`m going to sleep for now, i`m beat. thanks for the interest – stefanp Mar 07 '22 at 23:16
  • @stefanp You're using the backtick character `\`` for your apostrophe instead of `'` which is used for code in markdown. That's why your text is showing up weird. – Ryan Haining Mar 07 '22 at 23:26
  • 1
    @daemon CS50 is a freely-available harvard course. This specific problem is [here](https://cs50.harvard.edu/x/2022/psets/1/mario/less/) – Ryan Haining Mar 07 '22 at 23:28

2 Answers2

1

I did cs50 a while ago and my answer was something like yours (we probably based it on the C code we did prior). But thinking about it now, another way to do this with the help of fstrings would be like this:

n = int(input('height: ')
c = 1
for i in range(n):
    print(f'{" "*(n-1)}{"#"*c}')
    n -= 1
    c += 1
FelixOakz
  • 19
  • 4
0

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)
Andrew McClement
  • 1,171
  • 5
  • 14
  • hah, thanks dude. I was super tired and I missed that. you saved me a lot of time with an awesome simple explanation. Thanks - sorry, I don`t have reputation 15 yet for vote – stefanp Mar 07 '22 at 23:21