如何在 python3 中对角线打印一行?我想打印字母A.我是python的新用户



我想打印字母A。我是python的新用户。我正在尝试使用以下代码段。我怎样才能改善这一点?

letter_rep=['******']

对于范围内的 i(len(letter_rep((:

print('*'*i,letter_rep)
def print_A(w: int, h: int, char: str) -> None:
"""Print the letter 'A' as an ASCII art using char.
Play around with w and h to find the ideal dimenstion. A good starting
point can be w = 20, h = 10
:param w: Width of the canvas
:param h: Height of the canvas
:param char: Character used for the ASCII art.
"""
# Let the north west point of the canvas be the origin, we can generate
# the equations to compute the x coordinate based on the current y for the
# left and right strokes of the letter 'A'
left_stroke = lambda y: int((y + h) * w / (2 * h))
right_stroke = lambda y: int((h - y) * w / (2 * h))
mid_stroke_y: int = h // 2  # Set position of the horizontal stroke
for y in range(h):
# must use negative y due to the canvas being in the fourth quadrant
left_stroke_x = left_stroke(-y)
right_stroke_x = right_stroke(-y)
# The following print statements can be condensed to one line. 
# It is expanded here for better explanation.
print(' ' * left_stroke_x, end='')  # Empty spaces to the left of the left stroke
print(char, end='')  # Left stroke
print(  # Space between the left and right stroke
(right_stroke_x - left_stroke_x) * (char if y == mid_stroke_y else ' '), 
end='',
)
print(char, end='')  # Right stroke
print(' ' * (w - right_stroke_x))  # Empty spaces to the right of the right stroke

用法示例:

print_A(20, 10, '*')
'''
Output:
**          
*  *         
*    *        
*      *       
*        *      
************     
*            *    
*              *   
*                *  
*                  * 
'''
print_A(40, 10, '@')
'''
Output:
@@                    
@    @                  
@        @                
@            @              
@                @            
@@@@@@@@@@@@@@@@@@@@@@          
@                        @        
@                            @      
@                                @    
@                                    @  
'''

最新更新