TRY: -
from PIL import Image
def draw_nested_rectangles():
global pic
height = int(input('Please enter the overall height: '))
width = int(input('Please enter the overall width: '))
size = (width,height)
pic = Image.new('RGB', size, 'white')
firstRec(width, height)
secondRec(width, height)
thirdRec(width, height)
fourthRec(width, height)
pic.show()
def firstRec(width,height):
global pic
size = (width,height)
blueProgression = 0
for x in range(width,width):
color = (0,0,blueProgression)
for y in range(height,height):
pic.putpixel((x,y),color)
blueProgression += 5
def secondRec(width,height):
global pic
size = (width,height)
greenProgression = 255
for x in range(int(width*0.15),int(width*0.85)):
color = (0,greenProgression,0)
for y in range(int(height*0.15),int(height*0.85)):
pic.putpixel((x,y), color)
greenProgression -= 5
def thirdRec(width,height):
global pic
size = (width,height)
greenProgression = 255
for x in range(int(width*0.30),int(width*0.70)):
color = (255,0,0)
for y in range(int(height*0.30),int(height*0.70)):
pic.putpixel((x,y),color)
def fourthRec(width,height):
global pic
size = (width,height)
greenProgression = 255
for x in range(int(width*0.45),int(width*0.55)):
color = (255,255,255)
for y in range(int(height*0.45),int(height*0.55)):
pic.putpixel((x,y),color)
pic = 0
draw_nested_rectangles()
ПРИЧИНА ДЛЯ НЕДОПУСТИМОГО ВЫХОДА: -
Причина, по которой вы получили чистое изображение, а не то, которое вы ожидали, заключается в том, что вы создавали отдельный pic
объект изображения внутри каждой функции, из-за чего все функции работали над отдельными объектами изображения, а не один.
FIX: -
Чтобы получить желаемый результат, вы должны определить переменную pic
как глобальную, внутри каждой функции, которая хочет ее использовать, используя синтаксис global pic
ВХОД ОБРАЗЦА: -
Please enter the overall height: 500
Please enter the overall width: 500
ВЫБОР ВЫБОРА: -
![](https://i.stack.imgur.com/0J9L5.png)