diff --git a/README.md b/README.md index faf7ef9..a4f9197 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,21 @@ de *Nichola Lacey* - [012-019](./basic/basic02.py) - [020-026](./basic/basic03.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 diff --git a/basic/basic.py b/basic/basic.py index f47b090..e223f90 100644 --- a/basic/basic.py +++ b/basic/basic.py @@ -11,43 +11,6 @@ from common.common import ( run_func, 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(): select_ok = False diff --git a/basic/basic01.py b/basic/basic01.py index 5ec36f3..8819fec 100644 --- a/basic/basic01.py +++ b/basic/basic01.py @@ -1,7 +1,7 @@ class basics_001: 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: ") print(f"Hola {nombre}") diff --git a/common/common.py b/common/common.py index 600a021..de5ae25 100644 --- a/common/common.py +++ b/common/common.py @@ -13,19 +13,39 @@ def excp_handler(function): sleep(3) return wrapper -def opcs_default(): - print(""" v) Volver - s) Salir""") +def opcs_default(spaces): + sep = ' ' * spaces + print(f""" v){sep}Volver + s){sep}Salir""") def enter_continue(): input("\n\tPresiona Enter para continuar\n") +def enter_run(): + input("\n\tPresiona Enter para correr el programa\n") + @excp_handler def run_func(func): print(tab, func.__doc__, '\n') func() 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): try: selection = input("\n Ingresa una opción: ") @@ -50,5 +70,5 @@ def print_challenges(exercises): print(f"{i+1}) ".rjust(8), end='') print(vars(exercises).get(funcs[i]).__doc__) i+=1 - opcs_default() + opcs_default(2) return i, ex diff --git a/imgs/pattern_067.png b/imgs/pattern_067.png new file mode 100644 index 0000000..5a5ee0f Binary files /dev/null and b/imgs/pattern_067.png differ diff --git a/main.py b/main.py index 04d50dd..c55ca32 100755 --- a/main.py +++ b/main.py @@ -1,14 +1,16 @@ #!/usr/bin/env python from basic import basic +from trtl import trtl from common.common import clear, user_input, opcs_default def toc(): content="""\tContenido\n 1) Basicos 001-059 - 2) Intermedio 060-123 - 3) Tkinter 124-138 - 4) SQLite 139-145 + 2) Turtle 060-068 + 3) Intermedio 069-123 + 4) Tkinter 124-138 + 5) SQLite 139-145 ... s) Salir """ @@ -28,7 +30,7 @@ def basic_challenges(): while not select_ok: clear() print(content) - opcs_default() + opcs_default(1) selection = user_input(7) match selection: case 1: @@ -54,14 +56,13 @@ def basic_challenges(): def interm_challenges(): content = """ - 1) Interm 060-068 - 2) Interm 069-079 - 3) Interm 080-087 - 4) Interm 088-095 - 5) Interm 096-103 - 6) Interm 105-110 - 7) Interm 111-117 - 8) Interm 118-123 + 1) Interm 069-079 + 2) Interm 080-087 + 3) Interm 088-095 + 4) Interm 096-103 + 5) Interm 105-110 + 6) Interm 111-117 + 7) Interm 118-123 """ print(content) @@ -86,16 +87,16 @@ def main(): clear() print(header) toc() - selection = user_input(3) + selection = user_input(10) match selection: case 1: basic_challenges() case 2: - interm_challenges() + trtl.challenges() case 3: - tkinter_challenges() + interm_challenges() case 4: - pass + tkinter_challenges() case 5: pass case 6: diff --git a/trtl/example01.py b/trtl/example01.py new file mode 100644 index 0000000..ed7ea8e --- /dev/null +++ b/trtl/example01.py @@ -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() diff --git a/trtl/example02.py b/trtl/example02.py new file mode 100644 index 0000000..b4d6e35 --- /dev/null +++ b/trtl/example02.py @@ -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() diff --git a/trtl/example03.py b/trtl/example03.py new file mode 100644 index 0000000..724c036 --- /dev/null +++ b/trtl/example03.py @@ -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...") diff --git a/trtl/trtl.py b/trtl/trtl.py new file mode 100644 index 0000000..5555c05 --- /dev/null +++ b/trtl/trtl.py @@ -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 diff --git a/trtl/turtle01.py b/trtl/turtle01.py new file mode 100644 index 0000000..fe9bae9 --- /dev/null +++ b/trtl/turtle01.py @@ -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() diff --git a/trtl/turtle02.py b/trtl/turtle02.py new file mode 100644 index 0000000..3e63ed7 --- /dev/null +++ b/trtl/turtle02.py @@ -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() diff --git a/trtl/turtle03.py b/trtl/turtle03.py new file mode 100644 index 0000000..839992d --- /dev/null +++ b/trtl/turtle03.py @@ -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() diff --git a/trtl/turtle04.py b/trtl/turtle04.py new file mode 100644 index 0000000..c5cc1bf --- /dev/null +++ b/trtl/turtle04.py @@ -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() diff --git a/trtl/turtle05.py b/trtl/turtle05.py new file mode 100644 index 0000000..c26a64b --- /dev/null +++ b/trtl/turtle05.py @@ -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() diff --git a/trtl/turtle06.py b/trtl/turtle06.py new file mode 100644 index 0000000..3eef7b7 --- /dev/null +++ b/trtl/turtle06.py @@ -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() diff --git a/trtl/turtle07.py b/trtl/turtle07.py new file mode 100644 index 0000000..9fbfe3c --- /dev/null +++ b/trtl/turtle07.py @@ -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() diff --git a/trtl/turtle08.py b/trtl/turtle08.py new file mode 100644 index 0000000..6bf2d82 --- /dev/null +++ b/trtl/turtle08.py @@ -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() diff --git a/trtl/turtle09.py b/trtl/turtle09.py new file mode 100644 index 0000000..6353047 --- /dev/null +++ b/trtl/turtle09.py @@ -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()