猿问

画字母python

我有一个作业,我必须这样做:


该程序的目的是使用字符绘制一组矩形(边平行于轴)。


您将收到一系列矩形,每行一个,由左上角和右下角的坐标(X 和 Y)定义。


您应该使用 # 字符“绘制”所有矩形。X 和 Y 坐标分别向右和向下增长。


输入示例:


0 0 2 8


0 7 8 8


10 0 12 8


10 0 18 1


10 7 18 8


20 0 22 8


20 0 28 1


20 7 28 8

输出示例:


    ###       ######### #########

    ###       ######### #########

    ###       ###       ###      

    ###       ###       ###      

    ###       ###       ###      

    ###       ###       ###      

    ###       ###       ###      

    ######### ######### #########

    ######### ######### #########

我已经做了这个:


import sys


def make_rectangules(first_coordenates, second_coordenates):


    for y in range(second_coordenates[1]-first_coordenates[1]+1):

        for x in range(second_coordenates[0]-first_coordenates[0]+1):    

            print('#')



def main():

    aux_list = []

    for line in sys.stdin:

        line = line.strip('\n').split()

        line = list(map(int,line))

        first_coordenates = (line[0],line[1])

        second_coordenates = (line[2],line[3])

        make_rectangules(first_coordenates, second_coordenates)


main()

但我没有得到相同的结果。


摇曳的蔷薇
浏览 275回答 2
2回答

ITMISS

我做的:import sysdef main():    aux_list = [[0, 0, 2, 8],                [0, 7, 8, 8],                 [10, 0, 12, 8],                [10, 0, 18, 1],                [10, 7, 18, 8],                [20, 0, 22, 8],                [20, 0, 28, 1],                [20, 7, 28, 8]               ]    max_x = 0    max_y = 0    for elem in aux_list:        if elem[2] > max_x:            max_x = elem[2]        if elem[3] > max_y:            max_y = elem[3]    array = [[" " for x in range(max_y+1)] for y in range(max_x+1)]    for elem in aux_list:        for i in range(elem[0], elem[2]+1):            for j in range(elem[1], elem[3]+1):                array[i][j] = "#"    for x in range(max_y+1):        for y in range(max_x+1):            print(array[y][x], end='')        print("")main()结果 :    >>python draw.py###       ######### ############       ######### ############       ###       ######       ###       ######       ###       ######       ###       ######       ###       ############ ######### ################## ######### #########这不是最干净的方法,但它可以工作,并且此代码示例可以帮助您。请花点时间理解这段代码,如果您需要一些说明,请向我提问。

翻翻过去那场雪

代码:input_data = '''0 0 2 80 7 8 810 0 12 810 0 18 110 7 18 820 0 22 820 0 28 120 7 28 8'''rectangles = [line.split(' ') for line in input_data.splitlines()]output_list = []for rect in rectangles:&nbsp; &nbsp; x1, y1, x2, y2 = [int(coord) for coord in rect]&nbsp; &nbsp; for i in range(y1, y2 + 1):&nbsp; &nbsp; &nbsp; &nbsp; prev = output_list[i] if i < len(output_list) else ''&nbsp; &nbsp; &nbsp; &nbsp; if len(prev) < x2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prev += ' ' * (x2 - len(prev) + 1)&nbsp; &nbsp; &nbsp; &nbsp; prev = prev[:x1] + '#' * (x2 - x1 + 1) + prev[x2 + 1:]&nbsp; &nbsp; &nbsp; &nbsp; if i < len(output_list):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output_list[i] = prev&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output_list.append(prev)output = '\n'.join(output_list)print(output)输出:###&nbsp; &nbsp; &nbsp; &nbsp;######### ############&nbsp; &nbsp; &nbsp; &nbsp;######### ############&nbsp; &nbsp; &nbsp; &nbsp;###&nbsp; &nbsp; &nbsp; &nbsp;######&nbsp; &nbsp; &nbsp; &nbsp;###&nbsp; &nbsp; &nbsp; &nbsp;######&nbsp; &nbsp; &nbsp; &nbsp;###&nbsp; &nbsp; &nbsp; &nbsp;######&nbsp; &nbsp; &nbsp; &nbsp;###&nbsp; &nbsp; &nbsp; &nbsp;######&nbsp; &nbsp; &nbsp; &nbsp;###&nbsp; &nbsp; &nbsp; &nbsp;############ ######### ################## ######### #########随意询问有关代码的任何内容。
随时随地看视频慕课网APP

相关分类

Python
我要回答