+: turtle 060-068

This commit is contained in:
devfzn 2023-11-11 00:09:22 -03:00
parent c87350d5ae
commit f9145a4b56
Signed by: devfzn
GPG Key ID: E070ECF4A754FDB1
19 changed files with 432 additions and 58 deletions

View File

@ -10,6 +10,21 @@ de *Nichola Lacey*
- [012-019](./basic/basic02.py) - [012-019](./basic/basic02.py)
- [020-026](./basic/basic03.py) - [020-026](./basic/basic03.py)
- [027-034](./basic/basic04.py) - [027-034](./basic/basic04.py)
- [035-044](./basic/basic05.py)
- [045-051](./basic/basic06.py)
- [052-059](./basic/basic07.py)
## Retos Turtle
- [060](./trtl/turtle01.py)
- [061](./trtl/turtle02.py)
- [062](./trtl/turtle03.py)
- [063](./trtl/turtle04.py)
- [064](./trtl/turtle05.py)
- [065](./trtl/turtle06.py)
- [066](./trtl/turtle07.py)
- [067](./trtl/turtle08.py)
- [068](./trtl/turtle09.py)
## Uso ## Uso

View File

@ -11,43 +11,6 @@ from common.common import (
run_func, run_func,
clear clear
) )
def challenges_sublist():
choices = 4
select_ok = False
while not select_ok:
clear()
selection = user_input(choices)
match selection:
case 1:
challenges01()
case 2:
challenges02()
case 3:
challenges03()
case 4:
challenges04()
case 5:
challenges05()
case 6:
challenges06()
pass
case 7:
pass
case 8:
pass
case 9:
pass
case 10:
pass
case 11:
pass
case 'v':
return
case 's':
exit(0)
case _:
continue
def challenges01(): def challenges01():
select_ok = False select_ok = False

View File

@ -1,7 +1,7 @@
class basics_001: class basics_001:
def hola(self): def hola(self):
"Ask for the user's first name and display the output message" "Ask for the user's first name and display the output message."
nombre = input("Ingresa tu primer nombre: ") nombre = input("Ingresa tu primer nombre: ")
print(f"Hola {nombre}") print(f"Hola {nombre}")

View File

@ -13,19 +13,39 @@ def excp_handler(function):
sleep(3) sleep(3)
return wrapper return wrapper
def opcs_default(): def opcs_default(spaces):
print(""" v) Volver sep = ' ' * spaces
s) Salir""") print(f""" v){sep}Volver
s){sep}Salir""")
def enter_continue(): def enter_continue():
input("\n\tPresiona Enter para continuar\n") input("\n\tPresiona Enter para continuar\n")
def enter_run():
input("\n\tPresiona Enter para correr el programa\n")
@excp_handler @excp_handler
def run_func(func): def run_func(func):
print(tab, func.__doc__, '\n') print(tab, func.__doc__, '\n')
func() func()
enter_continue() enter_continue()
def print_run_func(func):
print(tab, func.__doc__, '\n')
cont = 0
retry = True
enter_continue()
while retry:
try:
func()
retry = False
except Exception:
cont += 1
if cont > 3:
print("Ocurrio un error al intentar correr el programa")
print("¿Está instalado 'tk' en el sistema?")
break
def user_input(i): def user_input(i):
try: try:
selection = input("\n Ingresa una opción: ") selection = input("\n Ingresa una opción: ")
@ -50,5 +70,5 @@ def print_challenges(exercises):
print(f"{i+1}) ".rjust(8), end='') print(f"{i+1}) ".rjust(8), end='')
print(vars(exercises).get(funcs[i]).__doc__) print(vars(exercises).get(funcs[i]).__doc__)
i+=1 i+=1
opcs_default() opcs_default(2)
return i, ex return i, ex

BIN
imgs/pattern_067.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

33
main.py
View File

@ -1,14 +1,16 @@
#!/usr/bin/env python #!/usr/bin/env python
from basic import basic from basic import basic
from trtl import trtl
from common.common import clear, user_input, opcs_default from common.common import clear, user_input, opcs_default
def toc(): def toc():
content="""\tContenido\n content="""\tContenido\n
1) Basicos 001-059 1) Basicos 001-059
2) Intermedio 060-123 2) Turtle 060-068
3) Tkinter 124-138 3) Intermedio 069-123
4) SQLite 139-145 4) Tkinter 124-138
5) SQLite 139-145
... ...
s) Salir s) Salir
""" """
@ -28,7 +30,7 @@ def basic_challenges():
while not select_ok: while not select_ok:
clear() clear()
print(content) print(content)
opcs_default() opcs_default(1)
selection = user_input(7) selection = user_input(7)
match selection: match selection:
case 1: case 1:
@ -54,14 +56,13 @@ def basic_challenges():
def interm_challenges(): def interm_challenges():
content = """ content = """
1) Interm 060-068 1) Interm 069-079
2) Interm 069-079 2) Interm 080-087
3) Interm 080-087 3) Interm 088-095
4) Interm 088-095 4) Interm 096-103
5) Interm 096-103 5) Interm 105-110
6) Interm 105-110 6) Interm 111-117
7) Interm 111-117 7) Interm 118-123
8) Interm 118-123
""" """
print(content) print(content)
@ -86,16 +87,16 @@ def main():
clear() clear()
print(header) print(header)
toc() toc()
selection = user_input(3) selection = user_input(10)
match selection: match selection:
case 1: case 1:
basic_challenges() basic_challenges()
case 2: case 2:
interm_challenges() trtl.challenges()
case 3: case 3:
tkinter_challenges() interm_challenges()
case 4: case 4:
pass tkinter_challenges()
case 5: case 5:
pass pass
case 6: case 6:

15
trtl/example01.py Normal file
View File

@ -0,0 +1,15 @@
import turtle as tt
def example_01():
"""Pentagono invertido"""
tt.bgcolor("black")
tt.color("white")
tt.speed(1)
tt.setundobuffer(None)
for _ in range(5):
tt.forward(100)
tt.right(72)
tt.exitonclick()
if __name__ == "__main__":
example_01()

18
trtl/example02.py Normal file
View File

@ -0,0 +1,18 @@
import turtle as tt
def example_02():
"""10 pentagonos que forman un decaedro"""
tt.bgcolor("black")
tt.color("white")
tt.speed(10)
tt.setundobuffer(None)
for _ in range(10):
tt.right(36)
for _ in range(5):
tt.forward(100)
tt.right(72.0)
tt.exitonclick()
if __name__ == "__main__":
example_02()

28
trtl/example03.py Normal file
View File

@ -0,0 +1,28 @@
from turtle import penup, goto, pendown # required for turtle(logo) graphics.
from math import sin, cos, pi
import turtle # required to calculate sin, cos and pi
r = 200 # radius = 400px
inc = pi/50 # angular increment = 100th of the circle = 2*pi/100 = pi/50
t = 0 # plotting point
n = 1.5 # offset; lies in range 0 and 2pi
for i in range(100): # there will be 100 chords(straight lines).
# (x1, y1) is the starting point of the chord.
x1 = r*sin(t); y1 = r*cos(t)
# (x2, y2) is the ending point of the chord.
x2 = r*sin(t+n); y2 = r*cos(t+n)
# display the calculated values to the user along with the line number.
print("{}: ({}, {}), ({}, {})".format(i+1, x1, y1, x2, y2))
turtle.speed(100)
turtle.bgcolor('black')
turtle.color('magenta')
penup() # lift pen
goto(x1, y1) # go to (x1, y1) point
pendown() # drop pen
goto(x2, y2) # go to (x2, y2) point
t += inc # increment angle
input("Press any enter to continue...")

59
trtl/trtl.py Normal file
View File

@ -0,0 +1,59 @@
from . import turtle01 as ex01
from . import turtle02 as ex02
from . import turtle03 as ex03
from . import turtle04 as ex04
from . import turtle05 as ex05
from . import turtle06 as ex06
from . import turtle07 as ex07
from . import turtle08 as ex08
from . import turtle09 as ex09
from common.common import (
user_input,
print_run_func,
opcs_default,
clear
)
tab = ' '
def challenges():
select_ok = False
while not select_ok:
clear()
print(tab, '1)', ex01.turtle_01.__doc__)
print(tab, '2)', ex02.turtle_02.__doc__)
print(tab, '3)', ex03.turtle_03.__doc__)
print(tab, '4)', ex04.turtle_04.__doc__)
print(tab, '5)', ex05.turtle_05.__doc__)
print(tab, '6)', ex06.turtle_06.__doc__)
print(tab, '7)', ex07.turtle_07.__doc__)
print(tab, '8)', ex08.turtle_08.__doc__)
print(tab, '9)', ex09.turtle_09.__doc__)
opcs_default(1)
selection = user_input(9)
match selection:
case 1:
print_run_func(ex01.turtle_01)
case 2:
print_run_func(ex02.turtle_02)
case 3:
print_run_func(ex03.turtle_03)
case 4:
print_run_func(ex04.turtle_04)
case 5:
print_run_func(ex05.turtle_05)
case 6:
print_run_func(ex06.turtle_06)
case 7:
print_run_func(ex07.turtle_07)
case 8:
print_run_func(ex08.turtle_08)
case 9:
print_run_func(ex09.turtle_09)
case 'v':
return
case 's':
select_ok = True
exit(0)
case _:
continue

19
trtl/turtle01.py Normal file
View File

@ -0,0 +1,19 @@
import turtle as tt
def turtle_01():
"""Draw a square"""
# Settings
tt.bgcolor("black")
tt.color("magenta")
tt.speed(2)
# Square
tt.forward(100)
tt.right(90)
tt.forward(100)
tt.right(90)
tt.forward(100)
tt.right(90)
tt.forward(100)
tt.exitonclick()

20
trtl/turtle02.py Normal file
View File

@ -0,0 +1,20 @@
import turtle as tt
def turtle_02():
"""Draw a triangle"""
# Settings
tt.bgcolor("black")
tt.color("magenta")
tt.pensize(3)
tt.speed(1)
# Triangle
tt.backward(140)
tt.left(45)
tt.forward(200)
tt.right(90)
tt.forward(200)
tt.right(135)
tt.forward(140)
tt.exitonclick()

17
trtl/turtle03.py Normal file
View File

@ -0,0 +1,17 @@
import turtle as tt
def turtle_03():
"""Draw a circle"""
# Settings
tt.bgcolor("black")
tt.color("magenta")
tt.fillcolor("yellow")
tt.pensize(3)
tt.speed(3)
# Circle
tt.begin_fill()
tt.circle(100)
tt.end_fill()
tt.exitonclick()

56
trtl/turtle04.py Normal file
View File

@ -0,0 +1,56 @@
import turtle as tt
def turtle_04():
"""Draw three squares in a row with a gap between each. Fill them using
three different colours."""
# Settings
tt.bgcolor("black")
tt.color("magenta")
tt.pensize(3)
tt.speed(1)
def sqr_draw1(color):
tt.down()
tt.fillcolor(color)
tt.begin_fill()
tt.back(100)
tt.left(90)
tt.forward(100)
tt.right(90)
tt.down()
tt.forward(100)
tt.right(90)
tt.forward(100)
tt.end_fill()
def sqr_draw2_3(color):
tt.down()
tt.fillcolor(color)
tt.begin_fill()
tt.forward(100)
tt.left(90)
tt.forward(100)
tt.left(90)
tt.forward(100)
tt.left(90)
tt.forward(100)
tt.end_fill()
# Square 1
tt.up()
tt.back(75)
sqr_draw1('yellow')
# Square 2
tt.up()
tt.left(90)
tt.forward(25)
sqr_draw2_3('orange')
# Square 3
tt.up()
tt.left(90)
tt.forward(125)
sqr_draw2_3('green')
tt.exitonclick()

23
trtl/turtle05.py Normal file
View File

@ -0,0 +1,23 @@
import turtle as tt
def turtle_05():
"""Draw a five-pointed star."""
# Settings
tt.bgcolor("black")
tt.color("magenta")
tt.pensize(2)
tt.speed(3)
# Star
tt.up()
tt.back(150)
tt.left(90)
tt.forward(75)
tt.right(90)
tt.down()
for _ in range(4):
tt.fd(300)
tt.right(144)
tt.fd(300)
tt.exitonclick()

48
trtl/turtle06.py Normal file
View File

@ -0,0 +1,48 @@
import turtle as tt
def turtle_06():
"""Write 1 2 3, starting at the bottom of the number one."""
# Settings
tt.bgcolor("black")
tt.color("magenta")
tt.pensize(10)
tt.speed(3)
# Number 1
tt.up()
tt.back(150)
tt.left(90)
tt.down()
tt.forward(200)
tt.up()
# Number 2
tt.right(90)
tt.forward(100)
tt.down()
tt.forward(100)
tt.right(90)
tt.forward(100)
tt.right(90)
tt.forward(100)
tt.left(90)
tt.forward(100)
tt.left(90)
tt.forward(100)
tt.up()
# Number 3
tt.forward(75)
tt.down()
tt.forward(100)
tt.left(90)
tt.forward(100)
tt.left(90)
tt.forward(100)
tt.back(100)
tt.right(90)
tt.forward(100)
tt.left(90)
tt.forward(100)
tt.exitonclick()

27
trtl/turtle07.py Normal file
View File

@ -0,0 +1,27 @@
import turtle as tt
from random import choice
def turtle_07():
"""Draw an octagon that uses a different colour (randomly selected from a
list of six possible colours) for each line."""
colors = ['yellow', 'orange', 'red', 'green', 'magenta']
def rand_color():
return choice(colors)
tt.bgcolor("black")
tt.pensize(10)
tt.speed(2)
tt.up()
tt.back(100)
tt.left(90)
tt.forward(250)
tt.right(90)
tt.down()
for _ in range(8):
tt.color(rand_color())
tt.forward(200)
tt.right(45)
tt.exitonclick()

16
trtl/turtle08.py Normal file
View File

@ -0,0 +1,16 @@
import turtle as tt
def turtle_08():
"""Create the pattern on image \"./imgs/pattern_067.png\""""
tt.bgcolor("black")
tt.color("magenta")
tt.pensize(2)
tt.speed(4)
for _ in range(10):
tt.right(36)
for _ in range(8):
tt.forward(100)
tt.right(45)
tt.exitonclick()

29
trtl/turtle09.py Normal file
View File

@ -0,0 +1,29 @@
import turtle as tt
from random import randint, random
def turtle_09():
"""Draw a pattern that will change each time the program is run. Use the
random function to pick the number of lines, the length of each line and
the angle of each turn."""
def rand_lines():
return randint(50, 150)
def rand_length():
return randint(150, 300)
def rand_angl():
return round(random()*360, 2)
lines = rand_lines()
length = rand_length()
angle = rand_angl()
tt.speed(100)
tt.bgcolor("black")
tt.color("magenta")
for _ in range(lines):
tt.left(angle)
tt.forward(length)
tt.exitonclick()