Created
December 15, 2021 21:13
-
-
Save les-peters/759615cc33714c14fff51d13998e110c to your computer and use it in GitHub Desktop.
Christmas Tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| question = """ | |
| Given a file named merry-christmas.txt that has a single integer x in it, | |
| write a script to generate a Christmas tree with asterisks (*) and output | |
| it as happy-holidays.txt. The tree should have a height of x, and if | |
| merry-christmas.txt is empty, the height should be 25 asterisks tall. | |
| Example (you can be flexible with your output, but this might be helpful | |
| for you as a starting point): | |
| // merry-christmas.txt | |
| 3 | |
| // happy-holidays.txt | |
| * | |
| *** | |
| ***** | |
| """ | |
| def create_christmas_tree(height): | |
| for level in range(0, height + 1): | |
| blanks = ' ' * (height - level) | |
| stars = '*' * level | |
| print(blanks + stars + stars[1::]) | |
| return None | |
| try: | |
| with open('merry-christmas.txt') as f: | |
| lines = f.readlines() | |
| height = int(lines[0]) | |
| except: | |
| height = 25 | |
| create_christmas_tree(height) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment