#๐ Help with understanding the code
46 messages ยท Page 1 of 1 (latest)
@tranquil zodiac
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.
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
ok what do you understand about this code so far?
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
ok cool
!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
so what do you think of the 2nd loop?
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | ....-
002 | ...---
003 | ..-----
004 | .-------
005 | ---------
I don't really understand it
I don't get the steps-i
so if steps is 3 and i is the running number like the starting number 1, then steps-i is 3 - 1
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?
!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()
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | i=1 steps=5 steps-i=4 2 * i - 1=1
002 | ....-
003 | i=2 steps=5 steps-i=3 2 * i - 1=3
004 | ...---
005 | i=3 steps=5 steps-i=2 2 * i - 1=5
006 | ..-----
007 | i=4 steps=5 steps-i=1 2 * i - 1=7
008 | .-------
009 | i=5 steps=5 steps-i=0 2 * i - 1=9
010 | ---------
wait so
no that's supposed to happen. So you can do range(n) and it will only loop in that range
So in the first iteration, steps-i = 4.
So you can see it as
for j in range(4):
Do you know you can multiply a string?
!e
print("rat" * 3)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
ratratrat
Yeah
But how does it maintain the tree shape then ?
โ๏ธ
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
i think u know what end="" does?? it doesnt make \n
\n means new line
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.