#๐Ÿ”’ Pascal triangle but

27 messages ยท Page 1 of 1 (latest)

green isle
civic carbonBOT
#

@green isle

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

crisp moat
#

what's the error?

green isle
#

grabB = prevRow[grabA + 1]
IndexError: list index out of range

#
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2024.1.4\plugins\python\helpers\pydev\pydevd.py", line 1551, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2024.1.4\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:\Users\best\PycharmProjects\coding\Python\Assignments\Pascal\pascal.py", line 40, in <module>
    createTriangle(7)
  File "C:\Users\best\PycharmProjects\coding\Python\Assignments\Pascal\pascal.py", line 26, in createTriangle
    grabB = prevRow[grabA + 1]
IndexError: list index out of range
toxic hinge
#

whats the err ๐Ÿ˜†

#

ah

green isle
#

Yep :/

#

The pastebin has the full code

#

And using grabB = prevRow[j + 1] has the same effect

crisp moat
#

Your code was awfully complex

#

I rewrote it

#

wanna see?

green isle
#

Sure

crisp moat
#

!e

def createTriangle(height):
    """Create a Pascal's triangle with given height."""
    triangle = []

    while len(triangle) < height:
        if len(triangle) == 0:
            triangle.append([1])
        elif len(triangle) == 1:
            triangle.append([1, 1])
        else:
            prevRow = triangle[-1]
            print(f"{prevRow=}")

            currRow = []
            for j in range(1, len(prevRow)):
                currNumber = prevRow[j - 1] + prevRow[j]
                currRow.append(currNumber)

            triangle.append([1] + currRow + [1])

    return triangle


for row in createTriangle(7):
    print(row)
civic carbonBOT
crisp moat
#

variable names like i and j make my brain hurt

green isle
#

iteration and jiteration :P

crisp moat
#

now I know

green isle
crisp moat
#

yep

#

your way would probably work fine, but because I'm lazy, I made it simpler so that I could get it working

green isle
#

Fair enough

#

Thanks

civic carbonBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

๐Ÿ”’ Pascal triangle but