#๐Ÿ”’ Help with understanding the code

46 messages ยท Page 1 of 1 (latest)

tranquil zodiac
#

Hello, I don't really understand this code (especially the second and third for loop and how the print function does here)

inland deltaBOT
#

@tranquil zodiac

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.

tranquil zodiac
#
steps = int(input("Enter the number of steps for the Christmas tree: "))

for i in range(1, steps + 1):
    for j in range(steps - i):
        print(" ", end="")
    

    for s in range(2 * i - 1):
        print("-", end="")
    
    print()
#

this is the code

pale nova
tranquil zodiac
#

I understand that the input and then the first for loop where it starts at 1 (so it doesnt start at 0) and then ends at the number + 1

#

so that if you go from 1-4

#

you dont want it to stop counting at 3

#

idk if im making sense

pale nova
#

ok cool

neon jay
#

!e

steps = 5

for i in range(1, steps + 1):
    for j in range(steps - i):
        print(".", end="")
    

    for s in range(2 * i - 1):
        print("-", end="")
    
    print()

this might give some perspective

pale nova
#

so what do you think of the 2nd loop?

inland deltaBOT
tranquil zodiac
#

I don't get the steps-i

pale nova
tranquil zodiac
#

yeah but each time its reducing the value of j right?

#

so like after 3-1

#

its going to be 3-2

#

and so on

#

but isnt that not whats supposed to happen?

#

as in it's supposed to increase the number instead of decrease it?

pale nova
#

!e

steps = 5

for i in range(1, steps + 1):
    print(f"{i=} {steps=} {steps-i=} {2 * i - 1=}")
    for j in range(steps - i):
        print(".", end="")
    

    for s in range(2 * i - 1):
        print("-", end="")
    
    print()
inland deltaBOT
tranquil zodiac
#

wait so

pale nova
tranquil zodiac
#

ok

#

but then it multiplies it

pale nova
#

So in the first iteration, steps-i = 4.

So you can see it as

for j in range(4):
pale nova
#

!e

print("rat" * 3)
inland deltaBOT
tranquil zodiac
#

Ok

#

I get whatโ€™s happening so far

tranquil zodiac
#

But how does it maintain the tree shape then ?

pale nova
#

The 2nd loop adds spaces

#

The 3rd loop makes the tree (dashes)

#

In the first iteration, it would make 4 spaces and 1 dash

#

In the 2nd iteration, it would make 3 spaces and 2 dashes

#

etc

lunar patrol
#

i think u know what end="" does?? it doesnt make \n

pale nova
#

\n means new line

inland deltaBOT
#
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.