How to generate a Christmas tree with python code
This article mainly explains "how to generate a Christmas tree with python code". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to generate a Christmas tree with python code".
A small tree print ('* '.rjust (3),' * * '.rjust (4),' * *,'| '.rjust (3), sep='\ n')
Results:
*
* *
* * *
| |
This line of code uses the rjust function to align strings to the right.
Its first parameter specifies the length of the string, that is,'* '.rjust (3) is obviously not long enough because there is only one character *, so what to do? by default, fill in 2 spaces from the left with spaces, so the length is 3 characters!
It is also important to note that the keyword parameter sep of print indicates the division symbols of the first few parameters.
This tree has no color, so is there any way to make a colorful little tree?
A print with color tree ("\ 033 [5 * 35 * 40m" +'* '.rjust (3) + "\ 033 [0m",' * * .rjust (4),'* *,'| '.rjust (3), sep='\ n')
Results:
How do you do that? Compared with the first section, there are only a few more characters: "\ 033 [5 / 35 / 40m". How do you interpret it? \ 033 [can be understood as a prefix for formatted printing, and other characters mean: 5 indicates that the display mode is flashing, 35 indicates that the foreground color is magenta, and 40m indicates that the background color is black
1. Display method:
0 (default), 1 (highlight), 22 (not bold), 4 (underline), 24 (non-underline), 5 (flicker), 25 (non-flicker), 7 (reflection), 27 (non-reflection)
2. Foreground color:
30 (black), 31 (red), 32 (green), 33 (× × ×), 34 (blue), 35 (magenta), 36 (cyan), 37 (white)
3. Background color:
40 (black), 41 (red), 42 (green), 43 (× × ×), 44 (blue), 45 (magenta), 46 (cyan), 47 (white)
How to make it colorful? The second and third lines choose other foreground colors respectively:
Print ("\ 033 [5Trachet40m" +'* '.rjust (3) + "\ 033 [0m", "\ 033 [536umbent 40m" +' * * '.rjust (4) + "\ 033 [0m", "\ 033 [5Tring 46th 40m * *\ 033 [0m", "\ 033 [535trans40m" +' | '.rjust (3) + "\ 033 [0m", sep='\ n')
Thank you for your reading, the above is the content of "how to use python code to generate a Christmas tree". After the study of this article, I believe you have a deeper understanding of how to use python code to generate a Christmas tree. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!