Construcccion - johncroth/pythonEd2024 GitHub Wiki

Up to this point, every program has been contained in one file. But this is not required. Create a file "formas.py" and with the following code

from turtle import *

def dibujar_polygono( longitud, lados):
    for i in range( lados ):
        forward( longitud )
        right( 360 / lados )

def dibujar_triangulo( longitud ):
    dibujar_polygono( longitud, 3 )

def dibujar_cuadrado( longitud ):
    dibujar_polygono( longitud, 4 )

def saltar( x, y ):
    penup()
    goto( x, y )
    pendown()

And now, create and execute constru.py with the following code:

from formas import *
from random import *

dibujar_cuadrado( 40 )
dibujar_triangulo( 60 )
for i in range(0,10):
    saltar( randint(-200, 200 ), randint(-200, 200))
    dibujar_polygono( randint(10,100), randint(3,8))

Finally, create and execute constru_b.py as follows:

from formas import *

dibujar_cuadrado(40)
forward(40)
right(180)
dibujar_triangulo( 40 )

The file "formas.py" is not really a python program, because if you try to run it, nothing will happen. Instead we call it a "module" or a "library." These modules effectively extend python

The modules we have used before, like turtle and random are not fundamentally different than formas, except they do different things. And are much longer and more complicated.

Ejercicios

  1. Our goal here is to write a module that "renames" the functions of the turtle package to be more like Spanish than English.

A. To begin, create the file esp_turtle.py and copy the following code to start:

from turtle import *

def adelante( dist ):
    forward( dist )

def derecha( grados ):
    right( grados )

Then, in reutil_1.py copy and execute the following.

from esp_turtle import *

for i in range(0,5):
    adelante( 80 )
    derecha( 72 )

B. Extend esp_turtle, add functions izquierda( grados ), levanta_boligrafo(), and baja_boligrafo() that wrap the respective function is turtle. Then add, code to reutil_1.py that calls these functions to see they work as expected.

C. Adding "backward" "begin_fill" and "end_fill." Maybe those with and without parameters should be separate.