Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
109 lines (79 loc) · 2.24 KB

File metadata and controls

109 lines (79 loc) · 2.24 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""This module contains a code example related to
Think Python, 2nd Edition
by Allen Downey
http://thinkpython2.com
Copyright 2015 Allen Downey
License: http://creativecommons.org/licenses/by/4.0/
"""
from __future__ import print_function, division
import string
import turtle
"""
To use this typewriter, you have to provide a module named letters.py
that contains functions with names like draw_a, draw_b, etc.
"""
# check if the reader has provided letters.py
try:
import letters
except ImportError as e:
message = e.args[0]
if message.startswith('No module'):
raise ImportError(message +
'\nYou have to provide a module named letters.py')
def teleport(t, x, y):
"""Moves the turtle without drawing a line.
Postcondition: pen is down
t: Turtle
x: coordinate
y: coordinate
"""
t.pu()
t.goto(x, y)
t.pd()
def keypress(char):
"""Handles the event when a user presses a key.
Checks if there is a function with the right name; otherwise
it prints an error message.
char: string, letter to draw
"""
# if we're still drawing the previous letter, bail out
if bob.busy:
return
else:
bob.busy = True
# figure out which function to call, and call it
try:
name = 'draw_' + char
func = getattr(letters, name)
except AttributeError:
print("I don't know how to draw an", char)
bob.busy = False
return
func(bob, size)
letters.skip(bob, size/2)
bob.busy = False
def carriage_return():
"""Moves to the beginning of the next line.
"""
teleport(bob, -180, bob.ycor() - size*3)
bob.busy = False
def presser(char):
"""Returns a function object that executes keypress.
char: character to draw when the function is executed
returns: function with no arguments
"""
def func():
keypress(char)
return func
# create and position the turtle
size = 20
bob = turtle.Turtle()
bob.busy = False
teleport(bob, -180, 150)
# tell world to call keypress when the user presses a key
screen = bob.getscreen()
for char in string.ascii_lowercase:
screen.onkey(presser(char), char)
screen.onkey(carriage_return, 'Return')
screen.listen()
turtle.mainloop()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.