Last active
September 17, 2018 01:00
-
-
Save Ad115/2c7d8262560e32313c63de32ab1be58b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from turtle import Turtle | |
gamera = Turtle() | |
# Podríamos hacer muchas cosas padres con sólo repetir: | |
gamera.forward(50) | |
gamera.left(30) | |
gamera.forward(50) | |
gamera.left(30) | |
gamera.forward(50) | |
gamera.left(30) | |
# ... | |
# Pero Python nos provee de un mecanismo para hacer esta repetición: | |
for i in range(100): | |
gamera.forward(100) | |
gamera.left(60) | |
# Incluso podemos modificar cada iteración: | |
for i in range(100): | |
gamera.forward(100-i) | |
gamera.left(60) | |
# Y podemos no sólo abstraer el bloque que queremos repetir, | |
# sino que también podemos abstraer el procedimiento completo: | |
def espiral(tortuga): | |
for i in range(100): | |
tortuga.forward(100-i) | |
tortuga.left(60) | |
# Ahora el procedimiento se llama espiral, | |
# y podemos hacer que nuestra tortuga lo siga | |
espiral(gamera) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment