Christmas Tree Generator


#1

My code generates a Christmas tree with customisable size:
Excuse my disgusting variable names and no comments.
Makes it funner to understand tho :slight_smile:

Code:

import random
print ""
print "Christmas Tree Generator:"
print โ€œโ€

gap_list = []
size_input = int(raw_input(โ€œEnter a number:โ€))
print ""
range_of_input = range(1,size_input * 2,2)
max_characters = (size_input * 2) - 1
tree_character = ""
enclosing_line = ""
tree_list = []
bauble1_list = []
bauble2_list = []
lap = ""
pot_gap = ""
pot = ""
for i in range(0,max_characters):
enclosing_line += "_"
print enclosing_line
print ""
for i in range_of_input:
if i == 1:
tree_character = "*"
else:
tree_character = ""
for i in range(0,i):
tree_character += "="
number_spaces = (max_characters - len(tree_character)) / 2
actual_gap = โ€œโ€

for i in range(0,number_spaces):
actual_gap += " "
gap_list.append(actual_gap)
tree_list.append(actual_gap + tree_character)
print tree_list[0]
for i in range(0, len(tree_list)):
gap_count = len(gap_list[i])
tree_upper_bound = len(tree_list[i])
new_tree_line = ""
bauble_index = random.randrange(gap_count, tree_upper_bound, 2)
temp_line_copy = tree_list[i]
new_tree_line = temp_line_copy[0:bauble_index] + โ€œoโ€ + temp_line_copy[bauble_index + 1: len(temp_line_copy)]
bauble1_list.append(new_tree_line)
for i in range(0, len(bauble1_list)):
if i > (len(tree_list) / 4):
gap_count = len(gap_list[i])
tree_upper_bound = len(bauble1_list[i])
new_tree_line = ""
bauble_index = random.randrange(gap_count, tree_upper_bound, 2)
temp_line_copy = bauble1_list[i]
new_tree_line = temp_line_copy[0:bauble_index] + โ€œoโ€ + temp_line_copy[bauble_index + 1: len(temp_line_copy)]
bauble2_list.append(new_tree_line)
else:
bauble2_list.append(bauble1_list[i])
for i in range(1, len(tree_list) - 1, 2):
print bauble2_list[i + 1]
print bauble2_list[i]
ham = ((max_characters) / 2)
sap = ""
bud = int((size_input / 10))
sud = int((size_input / 10))
pot_gap = ""
if bud % 2 == 0:
bud = bud + 1
for i in range(0,(max_characters - bud) / 2):
sap += " "
brap = ""
for i in range(0,bud):
brap += "|"
for i in range(0,sud):
print sap + brap
for i in range(0,((max_characters - bud) / 2) - 2):
lap += " "
for i in range(0,bud + 4):
pot += "-"
pot_lid = lap + pot
print pot_lid
for i in range(0, bud + 2):
pot_gap += " "
pot_lids = lap + โ€œ|โ€ + pot_gap + "|"
for i in range(0, sud):
print pot_lids
print pot_lid

print ""
print enclosing_line
print ""
enclosing_line = sap + sap
slap = ""
for i in range (0, bud):
slap += " "
manage = enclosing_line + slap
pork = ""
for i in range(0,len(manage) - 14):
pork += manage[i]
variabe = pork + "Samuel Everson"
print variabe

<do not remove the three backticks above>

#2

It looks great, but you should put indentation on your code.


#3

There you go buddy :smiley: Merry Christmas

#Christmas Tree Generator

import random

def intro(): #Prints Introduction

print ""
print "Christmas Tree Generator:"
print ""

def treeSize(): #Determines Size of Tree

return int(raw_input("The tree will be as tall as you wish (over 10). \n \nPlease choose a height: "))

def userChar(): #Determines Level Char

char =  raw_input("\nThe tree will be made of letters. \n \nPlease choose a single letter: ")

if len(char) != 1:
    
   print "Please Restart"
   input()

else:

    return char

def userName(): #Returns Userโ€™s Name

return raw_input("\nWhat is your name: ")

def maxChars(size): #Returns Maximum Number of Characters in Tree (Block of Text to be Filled)

maxChars = (size * 2) - 1  

return maxChars

def levelChars(size): #Returns List of Each Levels Number of Characters

levelChars = range(1,(size * 2), 2) 

return levelChars

def enclosingLine(maxChars): #Returns a Line of Underscores the Length of the Bottom Section of the Tree

endLine = ""

for i in range(0,maxChars):
    
    endLine += "_"

return endLine

def spaceList(levelChars): #Produces a List of Spaces as a Precursor for the Letters

spaces = []

for i in range(len(levelChars)):
        
        temp = " " * ((levelChars[-1] - levelChars[i]) / 2)
        
        spaces.append(temp)
        
return spaces

def letterList(levelChars, char): #Produces a List of the Letters with a Star on Top

letters = []

for i in range(len(levelChars)):

    if i == 0 :

        letters.append("*")
        
    else:

        temp = char * (levelChars[i])

        letters.append(temp)

return letters

def splice(spaces, letters): #Produces a List of Spaces and Letters (Tree Bulk)

treeBulk = []

for i in range(len(spaces)):

    temp = spaces[i] + letters[i]

    treeBulk.append(temp)
    
return treeBulk

def printBulk(treeBulk): #Splits the Tree into 2 Lists, an Even and an Odd, These are then printed

evenBulk = []                           #in opposite order so that the tree appears to have branches.

oddBulk = []

for i in range(len(treeBulk)):

    if i != 0:

        if i % 2 == 0:

            temp1 = treeBulk[i]

            evenBulk.append(temp1)


        elif i % 2 == 1:

            temp2 = treeBulk[i]
        
            oddBulk.append(temp2)
            
    else:
        
        print treeBulk[0]


for i in range(len(evenBulk)):

    print evenBulk[i]
    
    print oddBulk[i]

def printTrunk(height): #Produces a Trunk with Size Relative to the Treeโ€™s Height

thickness = 2 * int(height / 9 + 1) - 1

space = ((((height * 2) - 1) - thickness)) / 2

trunkBase = (" " * space) + ("|" * thickness)

depth = int(height / 10 + 1)

for i in range(depth):

    print trunkBase

def printPot(height): #Produces a Plant Pot with Size Relative to Treeโ€™s Height

width = 2 * int(height / 5) - 1

space = ((((height * 2) - 1) - width)) / 2

potHorizontal = (" " * space) + ("_" * width)

depth = int(height / 8 + 1)

print potHorizontal

internalSpacing = width - 2

potVertical = (" " * space) +  ("|") + (" " * internalSpacing) + ("|")

for i in range(depth):

    print potVertical

print potHorizontal

def printName(maxChars, name): #Produces a Name on the Far Right

space = maxChars - len(name)

print " " * space + name

Main Code

intro() #Prints Introduction

height = treeSize() #Stores User-Inputted Height

char = userChar() #Stores Letter to be Used in Levels

name = userName() #Stores UserName to Print Later

maxChars = maxChars(height) #Returns Max Number of Chars in Tree

levelChars = levelChars(height) #Returns List with Number of Chars for Each Level

spaces = spaceList(levelChars) #Returns List with Correct Number of Spaces in Each Level

letters = letterList(levelChars, char) #Returns List with Correct Number of Chars in Each Level

treeBulk = splice(spaces, letters) #Returns List with Correct Number of Spaces and Chars in Each Level

print โ€œโ€ #Prints Blank Line

print enclosingLine(maxChars) #Prints Line of Underscores

printBulk(treeBulk) #Sends treeBulk List to be Split and Printed into a Jagged Tree

printTrunk(height) #Prints the Tree Trunk

printPot(height) #Prints the Plant Pot

print enclosingLine(maxChars) #Prints Line of Underscores

print โ€œโ€ #Prints Blank Line

printName(maxChars, name) #Prints Signature