Python Why is spaces equal to tree_height-1 in the Christmas tree code in Python?

AI Thread Summary
The discussion revolves around a Python code snippet for generating a text-based Christmas tree. The main point of confusion is the initial value of the "spaces" variable, which is set to "tree_height - 1." Participants clarify that this value is necessary to center the tree properly, as it accounts for the horizontal distance from the left edge to the first character of the second-to-last row. The tree's width increases with each row, following the formula of "2i - 1" for the number of symbols in row i. The conversation emphasizes the importance of understanding how the spaces and hashes work together to create a visually centered tree structure.
mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hey! :giggle:

I found online the following code for a christmas tree in Python :
Code:
tree_height = input("Enter the tree height : ")
tree_height = int(tree_height)
spaces = tree_height-1
hashes = 1
stump_spaces = tree_height-1

while tree_height != 0 :
    for i in range(spaces):
        print(' ', end="")
    for i in range(hashes):
        print('#', end="")
    print()
    tree_height -= 1
    spaces -= 1
    hashes += 2
for i in range(stump_spaces):
    print(' ', end="")
print('#',end="")

Could you explain to me why the "spaces" is at the beginning equal to "tree_height-1" ? Shouldn't it be the half of that? :unsure:
 
Technology news on Phys.org
What do you think the spaces value should be? $\displaystyle \frac{h-1}{2}$?
 
Jameson said:
What do you think the spaces value should be? $\displaystyle \frac{h-1}{2}$?

When the spaces is equal to the height doesn't the tree start from the left end instead in the middle? :unsure:
 
The tree width at the next-to-bottom row is twice the height. So, if you entered '20' at the input line, the first row would have one symbol, the second row would have 3, the third row would have 5, and so on. The second-to-last row will have 39 symbols, not 20 or 19 or 21.
 
Ackbach said:
The tree width at the next-to-bottom row is twice the height. So, if you entered '20' at the input line, the first row would have one symbol, the second row would have 3, the third row would have 5, and so on. The second-to-last row will have 39 symbols, not 20 or 19 or 21.

So the number of symbols is $2i-1$ for the line $i$, right? I haven't really understood the formula for the spaces. Could you explain that further to me ? :unsure:
 
mathmari said:
So the number of symbols is $2i-1$ for the line $i$, right? I haven't really understood the formula for the spaces. Could you explain that further to me ? :unsure:
Yes, that's right!

spaces is initially equal to tree_height - 1, because tree_height is roughly half the horizontal distance from the far left to the far right on the second-to-last row. You want to "put your cursor" ready-to-print right before the exact character you need to print, hence the -1.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top