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

Click For Summary

Discussion Overview

The discussion revolves around the Python code for generating a Christmas tree and specifically addresses the reasoning behind initializing the variable "spaces" to "tree_height - 1". Participants explore the implications of this choice in relation to the tree's visual structure.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant questions why "spaces" is set to "tree_height - 1" and suggests it might be better represented as half of that value.
  • Another participant proposes that if "spaces" were equal to the height, the tree would start from the left edge rather than being centered.
  • Some participants note that the width of the tree at the next-to-bottom row is twice the height, leading to a specific pattern of symbols in each row.
  • A participant confirms that the number of symbols in each row follows the formula $2i-1$, where $i$ is the row number.
  • One participant explains that "spaces" is initialized to "tree_height - 1" to position the cursor correctly for printing, indicating that this value is related to the horizontal distance on the second-to-last row.

Areas of Agreement / Disagreement

Participants express differing views on the appropriate value for "spaces", with no consensus reached on whether it should be "tree_height - 1" or another value. The discussion remains unresolved regarding the optimal representation of spaces in the tree structure.

Contextual Notes

Some participants express uncertainty about the formula for spaces and the implications of the tree's width in relation to its height, indicating a need for further clarification on these points.

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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
5K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
55
Views
7K
  • · Replies 17 ·
Replies
17
Views
3K