from math import from tkinter import root Tk int input canv Canvas roo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from math import *
from tkinter import *
root = Tk()
n = int(input())
canv = Canvas(root, width = 729, height = 729, bg = 'blue')
def rec(x1, y1, x2, y2, k):
if k == n: return
canv.create_rectangle(x1+(x2-x1)/3,y1+(y2-y1)/3,x1+2*(x2-x1)/3,y1+2*(y2-y1)/3, fill = 'white')
rec(x1,y1,x1+(x2-x1)/3,y1+(y2-y1)/3,k+1)
rec(x1+(x2-x1)/3,y1,x1+2*(x2-x1)/3,y1+(y2-y1)/3,k+1)
rec(x1+2*(x2-x1)/3,y1,x2,y1+(y2-y1)/3,k+1)
rec(x1,y1+(y2-y1)/3,x1+(x2-x1)/3,y1+2*(y2-y1)/3,k+1)
rec(x1+2*(x2-x1)/3,y1+(y2-y1)/3,x2,y1+2*(y2-y1)/3,k+1)
rec(x1,y1+2*(y2-y1)/3,x1+(x2-x1)/3,y2,k+1)
rec(x1+(x2-x1)/3,y1+2*(y2-y1)/3,x1+2*(x2-x1)/3,y2,k+1)
rec(x1+2*(x2-x1)/3,y1+2*(y2-y1)/3,x2,y2,k+1)
rec(0,0,729,729,0)
canv.pack()
root.mainloop()