Funciones - johncroth/pythonEd2024 GitHub Wiki
Save and execute the following program in fun_a.py
. It draws 3 randomly placed triangles,
which is nothing new, but it is pretty different than what we have seen up to this point.
from turtle import *
from random import *
def saltar():
penup()
goto( randint(-200,200), randint(-200,200) )
pendown()
def dibujar_triangulo():
for i in range(0,3):
forward(80)
right(120)
saltar()
dibujar_triangulo()
saltar()
dibujar_triangulo()
saltar()
dibujar_triangulo()
input( "Empujar <Enter> para continuar")
In particular, it is better organized.
After importing turtle
and random
in the usual way, we have two function definitions, which define new operations in terms
of other previously defined operations. Theses definitions start with the word def
and then give the name of the function,
followed by a :
. Our first function is saltar()
, and the second is dibujar_triangulo()
. The code block after the def
line is not executed immediately, it is only remembered for later use. As you can read, the block inside saltar()
lifts
the pen with penup()
, jumps to a random spot, and places the pen down again with pendown().
The block inside dibujar_triangulo()
draws a triangle in the familiar way.
The second part of the program calls these functions in a certain order, executing the code in the def
blocks above. First
we call saltar()
and the turtle jumps to a random place, then dibjar_triangulo()
to draw the triangulo, and then jump
and draw twice more, with saltar()
and dibujar_triangulo()
.
Once we have defined a function, we can use these just like any of the built-in Python function we have learned. For instance, add
the following lines just about the line with input
to draw some triangles with a shared point:
for i in range(0,4):
saltar()
dibujar_triangulo()
right(180)
dibujar_triangulo()
Let's say we want to draw pentagons as well as triangles. We can just write a function to do that. Carefully add the following lines
to fun_a.py
. First, just below the block that defines dibujar_triangulo()
, we will define a function to draw a pentagon:
def dibujar_pentagono():
for i in range(0,5):
forward(60)
right(72)
(It is common to group all the definitions of functions togther at the top, creating a "language" for the work below.) Then just about
the input
at the bottom add the following.
goto(0,0)
dibujar_pentagono()
right(144)
dibujar_pentagono()
saltar()
dibujar_triangulo()
You'll see two pentagons which share a side, and and then a randomly placed pentagon. The point is, now we only need a single line to draw a triangle, a pentagon, or jump to a random location.
Functions are a very fundamental topic, and we have more to learn about them, but first let's practice a little.
-
Create a file
fun_1.py
and copy the code forfun_a.py
into it.A. Change the program so that all the triangles are yellow with
color(0,1,1)
and all the pentagons are green.B. Add a function that draws a red filled rectangle,
-
The lyrics to the first part of the song
Carañito
are as follows.Lloro por quererte, por amarte, por desearte Lloro por quererte, por amarte, por desearte Ay cariño, ay mi vida Nunca, pero nunca me abandones, cariñito Nunca, pero nunca me abandones, cariñito Lloro por quererte, por amarte, por desearte Lloro por quererte, por amarte, por desearte Ay cariño, ay mi vida Nunca, pero nunca me abandones, cariñito Nunca, pero nunca me abandones, cariñito
Copy the code below into the file
func_2.py
, and complete the program below to make it print out these lyrics to the console. Use all the function provided at least once.def linea_lloro(): print( "Lloro por quererte, por amarte, por desearte" ) def linea_ay(): print( "Ay cariño, ay mi vida" ) def linea_nunca(): print( "Nunca, pero nunca me abandones, cariñito" ) def imprimir_uno_corus(): # Escribe esta funcion imprimir_corus() print() imprimir_corus()
Function with errors, including indentation. Maybe mention that above.
Functions with no parameters. square isoceles triangle with 30degree angle. draw house, using the two. write a function to draw a house, and draw 3 houses
Function to print out a verse of a song.
Using variables within functions. (But not changing them.)
return values as exercise? return a random number between -100 and 100.
randomly set color