![]() ![]() # Loop while the row number is less than or equal to the height of the tree ![]() # Set the initial values for the while loop Here’s what it looks like in the code: # Function to draw a Christmas tree with a given height To draw a Christmas tree in Python, let’s use a while loop to print the asterisks in the shape of a tree. More importantly, if you’re new to loops, you should definitely try reproducing the tree in the previous example using a while loop. The for-loop approach is the simplest one to generate a Christmas tree in Python.īut it doesn’t mean you couldn’t do it with a while loop. When you call the draw_tree() function and pass it a height, it will print a Christmas tree with the given height.įor instance, if you call draw_tree(5), it prints a Christmas tree like follows: 2. The number of spaces before the asterisks and the number of asterisks on each row is calculated based on the height of the tree.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |