-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathhello_little_turtles.html
More file actions
executable file
·1194 lines (984 loc) · 56.2 KB
/
hello_little_turtles.html
File metadata and controls
executable file
·1194 lines (984 loc) · 56.2 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>3. Hello, little turtles! — How to Think Like a Computer Scientist: Learning with Python 3 (AoPS Edition)</title>
<link rel="stylesheet" href="_static/style.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/codemirrorEdited.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript" src="_static/pywindowCodemirrorC.js"></script>
<script type="text/javascript" src="_static/skulpt.min.js"></script>
<script type="text/javascript" src="_static/skulpt-stdlib.js"></script>
<script type="text/javascript" src="_static/aopsmods.js"></script>
<link rel="copyright" title="Copyright" href="copyright.html" />
<link rel="top" title="How to Think Like a Computer Scientist: Learning with Python 3 (AoPS Edition)" href="index.html" />
<link rel="next" title="4. Functions" href="functions.html" />
<link rel="prev" title="2. Variables, expressions and statements" href="variables_expressions_statements.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="functions.html" title="4. Functions"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="variables_expressions_statements.html" title="2. Variables, expressions and statements"
accesskey="P">previous</a> |</li>
<li><a href="index.html">How to Think Like a Computer Scientist: Learning with Python 3 (AoPS Edition)</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="body">
<div class="line-block">
<div class="line"><br /></div>
</div>
<div class="section" id="hello-little-turtles">
<h1>3. Hello, little turtles!<a class="headerlink" href="#hello-little-turtles" title="Permalink to this headline">¶</a></h1>
<p id="index-0">There are many <em>modules</em> in Python that provide very powerful features that we
can use in our own programs. Some of these can send email, or fetch web pages.
The one we’ll look at in this chapter allows us to create turtles and get them
to draw shapes and patterns.</p>
<p>The turtles are fun, but the real purpose of the chapter is to teach ourselves
a little more Python, and to develop our theme of <em>computational thinking</em>,
or <em>thinking like a computer scientist</em>. Most of the Python covered here
will be explored in more depth later.</p>
<div class="section" id="our-first-turtle-program">
<span id="index-1"></span><h2>3.1. Our first turtle program<a class="headerlink" href="#our-first-turtle-program" title="Permalink to this headline">¶</a></h2>
<p>Let’s write a couple of lines of Python program to create a new
turtle and start drawing a rectangle. (We’ll call the variable that
refers to our first turtle <tt class="docutils literal"><span class="pre">alex</span></tt>, but we can choose another
name if we follow the naming rules from the previous chapter).</p>
<div id="firstturtle" class="pywindow" >
<div id="firstturtle_code_div" style="display: block">
<textarea rows="9" id="firstturtle_code" class="active_code" prefixcode="undefined">
import turtle # Allows us to use turtles
wn = turtle.Screen() # Creates a playground for turtles
alex = turtle.Turtle() # Create a turtle, assign to alex
alex.forward(50) # Tell alex to move forward by 50 units
alex.left(90) # Tell alex to turn left by 90 degrees
alex.forward(30) # Complete the second side of a rectangle
wn.mainloop() # Wait for user to close window</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['firstturtle_code'] = true;
pythonTool.readOnlyFlags['firstturtle_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="firstturtle_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="firstturtle_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="firstturtle_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='firstturtle_error'></div>
<div style="text-align: center">
<canvas id="firstturtle_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="firstturtle_suffix" style="display:none">
</pre>
<pre id="firstturtle_pre" class="active_out">
</pre>
<div id="firstturtle_files" class="ac-files ac-files-hidden"></div>
</div>
<p>If you copy and paste this program into a new IDLE module, and run it, it doesn’t run in the shell like our programs so far: instead, a new window pops up! (If you run it here in the ebook by clicking the “Run” button, a simulation of this window will appear within the book.)</p>
<p>Here are a couple of things we’ll need to understand about this program. (Don’t worry if you don’t understand them all now—we’ll cover them in more detail as we work through the book.)</p>
<p>The first line tells Python to load a module named <tt class="docutils literal"><span class="pre">turtle</span></tt>.
That module brings us two new types that we can use:
the <tt class="docutils literal"><span class="pre">Turtle</span></tt> type, and the <tt class="docutils literal"><span class="pre">Screen</span></tt> type. The dot
notation <tt class="docutils literal"><span class="pre">turtle.Turtle</span></tt> means <em>“The Turtle type that is defined within
the turtle module”</em>. (Remember that Python is case sensitive, so the
module name, with a lowercase <cite>t</cite>, is different from the type <tt class="docutils literal"><span class="pre">Turtle</span></tt>.)</p>
<p>We then create and open what it calls a screen (we would prefer to call it
a window), which we assign to variable <tt class="docutils literal"><span class="pre">wn</span></tt>. Every window contains
a <strong>canvas</strong>, which is the area inside the window on which we can draw.</p>
<p>In line 3 we create a turtle. The variable <tt class="docutils literal"><span class="pre">alex</span></tt> is made to refer to this turtle.</p>
<p>So these first three lines have set things up, we’re ready to get our turtle to draw on our canvas.</p>
<p>In lines 5-7, we instruct the <strong>object</strong> <tt class="docutils literal"><span class="pre">alex</span></tt> to move, and to turn. We
do this by <strong>invoking</strong>, or activating, <tt class="docutils literal"><span class="pre">alex</span></tt>‘s <strong>methods</strong> — these are
the instructions that all turtles know how to respond to.</p>
<p>The last line plays a part too: the <tt class="docutils literal"><span class="pre">wn</span></tt> variable refers to
the window shown above. When we invoke its <tt class="docutils literal"><span class="pre">mainloop</span></tt> method, it enters
a state where it waits for events (like keypresses, or mouse movement and clicks).
The program will terminate when the user closes the window. (Note: in our ebook, you can’t “close” the turtle window once it’s open. The <tt class="docutils literal"><span class="pre">wn.mainloop()</span></tt> line is there for when you run the program on your own computer inside of IDLE. If you leave it out, then your turtle program will never terminate, and you’ll have to shut down IDLE to continue working.)</p>
<p>An object can have various methods — things it can do — and it can also have
<strong>attributes</strong> (sometimes called <em>properties</em>). For example, each turtle has
a <em>color</em> attribute. The method invocation
<tt class="docutils literal"><span class="pre">alex.color("red")</span></tt> will make <tt class="docutils literal"><span class="pre">alex</span></tt> red, and drawing will be red too.</p>
<p>The color of the turtle, the width of its pen, the position of the
turtle within the window, which way it is facing, and so on are all part of its
current <strong>state</strong>. Similarly, the window object has a background color, and
some text in the title bar, and a size and position on the screen. These are all
part of the state of the window object.</p>
<p>Quite a number of methods exist that allow us to modify the turtle and the
window objects. We’ll just show a couple. In this program we’ve only commented those
lines that are different from the previous example (and we’ve used a different
variable name for this turtle):</p>
<div id="secondturtle" class="pywindow" >
<div id="secondturtle_code_div" style="display: block">
<textarea rows="14" id="secondturtle_code" class="active_code" prefixcode="undefined">
import turtle
wn = turtle.Screen()
wn.bgcolor("lightgreen") # Set the window background color
wn.title("Hello, Tess!") # Set the window title
tess = turtle.Turtle()
tess.color("blue") # Tell tess to change her color
tess.pensize(3) # Tell tess to set her pen width
tess.forward(50)
tess.left(120)
tess.forward(50)
wn.mainloop()</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['secondturtle_code'] = true;
pythonTool.readOnlyFlags['secondturtle_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="secondturtle_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="secondturtle_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="secondturtle_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='secondturtle_error'></div>
<div style="text-align: center">
<canvas id="secondturtle_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="secondturtle_suffix" style="display:none">
</pre>
<pre id="secondturtle_pre" class="active_out">
</pre>
<div id="secondturtle_files" class="ac-files ac-files-hidden"></div>
</div>
<p>When we run this program, a new window pops up. (Note: in this ebook, the turtle windows don’t have titles, so the line <tt class="docutils literal"><span class="pre">wn.title("Hello,</span> <span class="pre">Tess!")</span></tt> does nothing. But if you copy and paste this program into a module on your compute and run it in IDLE, you’ll see that the title bar of the window says <tt class="docutils literal"><span class="pre">Hello,</span> <span class="pre">Tess!</span></tt>.)</p>
</div>
<div class="section" id="instances-a-herd-of-turtles">
<span id="index-2"></span><h2>3.2. Instances — a herd of turtles<a class="headerlink" href="#instances-a-herd-of-turtles" title="Permalink to this headline">¶</a></h2>
<p>Just like we can have many different integers in a program, we can have many turtles.
Each of them is called an <strong>instance</strong>. Each instance has its own attributes and
methods — so <tt class="docutils literal"><span class="pre">alex</span></tt> might draw with a thin black pen and be at some position,
while <tt class="docutils literal"><span class="pre">tess</span></tt> might be going in her own direction with a fat pink pen.</p>
<div id="twoturtles" class="pywindow" >
<div id="twoturtles_code_div" style="display: block">
<textarea rows="31" id="twoturtles_code" class="active_code" prefixcode="undefined">
import turtle
wn = turtle.Screen() # Set up the window and its attributes
wn.bgcolor("lightgreen")
wn.title("Tess & Alex")
tess = turtle.Turtle() # Create tess and set some attributes
tess.color("hotpink")
tess.pensize(5)
alex = turtle.Turtle() # Create alex
tess.forward(80) # Make tess draw equilateral triangle
tess.left(120)
tess.forward(80)
tess.left(120)
tess.forward(80)
tess.left(120) # Complete the triangle
tess.right(180) # Turn tess around
tess.forward(80) # Move her away from the origin
alex.forward(50) # Make alex draw a square
alex.left(90)
alex.forward(50)
alex.left(90)
alex.forward(50)
alex.left(90)
alex.forward(50)
alex.left(90)
wn.mainloop()</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['twoturtles_code'] = true;
pythonTool.readOnlyFlags['twoturtles_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="twoturtles_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="twoturtles_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="twoturtles_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='twoturtles_error'></div>
<div style="text-align: center">
<canvas id="twoturtles_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="twoturtles_suffix" style="display:none">
</pre>
<pre id="twoturtles_pre" class="active_out">
</pre>
<div id="twoturtles_files" class="ac-files ac-files-hidden"></div>
</div>
<p>Here are some observations about this program:</p>
<ul class="simple">
<li>There are 360 degrees in a full circle. If we add up all the turns that a turtle makes,
<em>no matter what steps occurred between the turns</em>, we can easily figure out if they
add up to some multiple of 360. This should convince us that <tt class="docutils literal"><span class="pre">alex</span></tt> is facing in
exactly the same direction as he was when he was first created. (Geometry
conventions have 0 degrees facing East, and that is the case here too!)</li>
<li>We could have left out the last turn for <tt class="docutils literal"><span class="pre">alex</span></tt>, but that would not have been
as satisfying. If we’re asked to draw a closed shape like a
square or a rectangle, it is a good idea to
complete all the turns and to leave the turtle back where it started, facing the
same direction as it started in.
This makes reasoning about the program and composing chunks of code into bigger programs
easier for us humans!</li>
<li>We did the same with <tt class="docutils literal"><span class="pre">tess</span></tt>: she drew her triangle, and turned through a full 360 degrees.
Then we turned her around and moved her aside. Even the blank line 18
is a hint about how the programmer’s <em>mental chunking</em> is working:
in big terms, <tt class="docutils literal"><span class="pre">tess</span></tt>‘ movements were chunked as “draw the triangle”
(lines 12-17) and then “move away from the origin” (lines 19 and 20).</li>
<li>One of the key uses for comments is to record our mental chunking, and big ideas.
They’re not always explicit in the code.</li>
<li>And, uh-huh, two turtles may not be enough for a herd. But the important idea is that the
turtle module gives us a kind of factory that lets us create as many turtles as we
need. Each instance has its own state and behavior.</li>
</ul>
</div>
<div class="section" id="the-for-loop">
<span id="index-3"></span><h2>3.3. The <strong>for</strong> loop<a class="headerlink" href="#the-for-loop" title="Permalink to this headline">¶</a></h2>
<p>When we drew the square, it was quite tedious. We had to explicitly repeat the steps of
moving and turning four times. If we were drawing a hexagon, or an octogon,
or a polygon with 42 sides, it would have been worse.</p>
<p>So a basic building block of all programs is to be able to repeat some code, over and
over again.</p>
<p>Python’s <strong>for</strong> loop solves this for us. Let’s say we have some friends, and
we’d like to send them each an email inviting them to our party. We don’t
quite know how to send email yet, so for the moment we’ll just print a message for each friend:</p>
<div id="partyinvites" class="pywindow" >
<div id="partyinvites_code_div" style="display: block">
<textarea rows="4" id="partyinvites_code" class="active_code" prefixcode="undefined">
for f in ["Joe","Zoe","Brad","Angelina","Zuki","Thandi","Paris"]:
invite = "Hi " + f + ". Please come to my party on Saturday!"
print(invite)
# more code can follow here ...</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['partyinvites_code'] = true;
pythonTool.readOnlyFlags['partyinvites_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="partyinvites_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="partyinvites_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="partyinvites_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='partyinvites_error'></div>
<div style="text-align: center">
<canvas id="partyinvites_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="partyinvites_suffix" style="display:none">
</pre>
<pre id="partyinvites_pre" class="active_out">
</pre>
<div id="partyinvites_files" class="ac-files ac-files-hidden"></div>
</div>
<ul class="simple">
<li>The variable <tt class="docutils literal"><span class="pre">f</span></tt> in the <tt class="docutils literal"><span class="pre">for</span></tt> statement at line 1 is called the <strong>loop variable</strong>.
We could have chosen any other variable name instead.</li>
<li>Lines 2 and 3 are the <strong>loop body</strong>. The loop body is always
indented. The convention is to use 4 spaces of indentation for our loop bodies. The indentation determines exactly what statements are “in the body of the loop”.</li>
<li>On each <em>iteration</em> or <em>pass</em> of the loop, first a check is done to see if there are
still more items to be processed. If there are none left (this is called
the <strong>terminating condition</strong> of the loop), the loop has finished.
Program execution continues at the next statement after the loop body, (e.g. in this case
the next statement below the comment in line 4).</li>
<li>If there are items still to be processed, the loop variable is updated to refer to the
next item in the list. This means, in this case, that the loop body is executed
here 7 times, and each time <tt class="docutils literal"><span class="pre">f</span></tt> will refer to a different friend.</li>
<li>At the end of each execution of the body of the loop, Python returns
to the <tt class="docutils literal"><span class="pre">for</span></tt> statement, to see if there are more items to be handled, and to assign the
next one to <tt class="docutils literal"><span class="pre">f</span></tt>.</li>
</ul>
</div>
<div class="section" id="flow-of-execution-of-the-for-loop">
<span id="index-4"></span><h2>3.4. Flow of Execution of the for loop<a class="headerlink" href="#flow-of-execution-of-the-for-loop" title="Permalink to this headline">¶</a></h2>
<p>As a program executes, the interpreter always keeps track of which statement is
about to be executed. We call this the <strong>control flow</strong>, of the <strong>flow of execution</strong>
of the program. When humans execute programs, they often use their finger to point
to each statement in turn. So we could think of control flow as “Python’s moving finger”.</p>
<p>Control flow until now has been strictly
top to bottom, one statement at a time. The <tt class="docutils literal"><span class="pre">for</span></tt> loop changes this.</p>
<div class="admonition-flowchart-of-a-for-loop admonition">
<p class="first admonition-title">Flowchart of a <strong>for</strong> loop</p>
<p>Control flow is often easy to visualize and understand if we draw a flowchart.
This shows the exact steps and logic of how the <tt class="docutils literal"><span class="pre">for</span></tt> statement executes.</p>
<a class="last reference internal image-reference" href="_images/flowchart_for.png"><img alt="_images/flowchart_for.png" src="_images/flowchart_for.png" style="height: 420px;" /></a>
</div>
</div>
<div class="section" id="the-loop-simplifies-our-turtle-program">
<span id="index-5"></span><h2>3.5. The loop simplifies our turtle program<a class="headerlink" href="#the-loop-simplifies-our-turtle-program" title="Permalink to this headline">¶</a></h2>
<p>To draw a square we’d like to do the same thing four times — move the turtle, and turn.
We previously used 8 lines to have <tt class="docutils literal"><span class="pre">alex</span></tt> draw the four sides of a square.
This does exactly the same, but using just three lines:</p>
<div id="turtlesquare" class="pywindow" >
<div id="turtlesquare_code_div" style="display: block">
<textarea rows="9" id="turtlesquare_code" class="active_code" prefixcode="undefined">
import turtle
wn = turtle.Screen() # Set up the window and its attributes
alex = turtle.Turtle() # Create alex
for i in [0,1,2,3]: # Draw the four sides of the square
alex.forward(50)
alex.left(90)
wn.mainloop()</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['turtlesquare_code'] = true;
pythonTool.readOnlyFlags['turtlesquare_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="turtlesquare_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="turtlesquare_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="turtlesquare_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='turtlesquare_error'></div>
<div style="text-align: center">
<canvas id="turtlesquare_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="turtlesquare_suffix" style="display:none">
</pre>
<pre id="turtlesquare_pre" class="active_out">
</pre>
<div id="turtlesquare_files" class="ac-files ac-files-hidden"></div>
</div>
<p>Some observations:</p>
<ul class="simple">
<li>While “saving some lines of code” might be convenient, it is not the big deal here.
What is much more important is that we’ve found a “repeating pattern” of statements,
and reorganized our program to repeat the pattern. Finding the chunks and somehow
getting our programs arranged around those chunks is a vital
skill in computational thinking.</li>
<li>The values [0,1,2,3] were provided to make the loop body execute 4 times.
We could
have used any four values, but these are the conventional ones to use. In fact, they are
so popular that Python gives us special built-in <tt class="docutils literal"><span class="pre">range</span></tt> objects:</li>
</ul>
<div id="forloopexamples" class="pywindow" >
<div id="forloopexamples_code_div" style="display: block">
<textarea rows="6" id="forloopexamples_code" class="active_code" prefixcode="undefined">
for i in range(4):
# Executes the body with i = 0, then 1, then 2, then 3
print("i = "+str(i))
for x in range(10):
# Sets x to each of ... [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print("x = "+str(x))</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['forloopexamples_code'] = true;
pythonTool.readOnlyFlags['forloopexamples_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="forloopexamples_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="forloopexamples_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="forloopexamples_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='forloopexamples_error'></div>
<div style="text-align: center">
<canvas id="forloopexamples_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="forloopexamples_suffix" style="display:none">
</pre>
<pre id="forloopexamples_pre" class="active_out">
</pre>
<div id="forloopexamples_files" class="ac-files ac-files-hidden"></div>
</div>
<ul class="simple">
<li>Computer scientists like to count from 0!</li>
<li><tt class="docutils literal"><span class="pre">range</span></tt> can deliver a sequence of values to the loop variable in the <tt class="docutils literal"><span class="pre">for</span></tt> loop.
They start at 0, and in these cases do not include the 4 or the 10.</li>
<li>Our little trick earlier to make sure that <tt class="docutils literal"><span class="pre">alex</span></tt> did the final turn to complete
360 degrees has paid off: if we had not done that, then we would not have been
able to use a loop for the fourth side of the square.
It would have become a “special case”,
different from the other sides. When possible, we’d much prefer to make
our code fit a general pattern, rather than have to create a special case.</li>
</ul>
<p>So to repeat something four times, a good Python programmer would do this:</p>
<div id="forloopalex" class="pywindow" >
<div id="forloopalex_code_div" style="display: block">
<textarea rows="3" id="forloopalex_code" class="active_code" prefixcode="undefined">
for i in range(4):
alex.forward(50)
alex.left(90)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['forloopalex_code'] = false;
pythonTool.readOnlyFlags['forloopalex_code'] = true;
</script>
<div id='forloopalex_error'></div>
<pre id="forloopalex_suffix" style="display:none">
</pre>
</div>
<p>By now you should be able to see how to change our previous program so that
<tt class="docutils literal"><span class="pre">tess</span></tt> can also use a <tt class="docutils literal"><span class="pre">for</span></tt> loop to draw her equilateral triangle.</p>
<p>But now, what would happen if we made the change below? Try to figure out what’s going to happen, then run the code to see if you’re right!</p>
<div id="turtlesquarefourcolors" class="pywindow" >
<div id="turtlesquarefourcolors_code_div" style="display: block">
<textarea rows="10" id="turtlesquarefourcolors_code" class="active_code" prefixcode="undefined">
import turtle
wn = turtle.Screen() # Set up the window and its attributes
alex = turtle.Turtle() # Create alex
for c in ["yellow", "red", "purple", "blue"]:
alex.color(c)
alex.forward(50)
alex.left(90)
wn.mainloop()</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['turtlesquarefourcolors_code'] = true;
pythonTool.readOnlyFlags['turtlesquarefourcolors_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="turtlesquarefourcolors_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="turtlesquarefourcolors_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="turtlesquarefourcolors_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='turtlesquarefourcolors_error'></div>
<div style="text-align: center">
<canvas id="turtlesquarefourcolors_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="turtlesquarefourcolors_suffix" style="display:none">
</pre>
<pre id="turtlesquarefourcolors_pre" class="active_out">
</pre>
<div id="turtlesquarefourcolors_files" class="ac-files ac-files-hidden"></div>
</div>
<p>A variable can also be assigned a value that is a list. So lists can also be used in
more general situations, not only in the <tt class="docutils literal"><span class="pre">for</span></tt> loop. The code above could be rewritten like this:</p>
<div id="turtlesquarefourcolorlist" class="pywindow" >
<div id="turtlesquarefourcolorlist_code_div" style="display: block">
<textarea rows="11" id="turtlesquarefourcolorlist_code" class="active_code" prefixcode="undefined">
import turtle
wn = turtle.Screen() # Set up the window and its attributes
alex = turtle.Turtle() # Create alex
clrs = ["yellow", "red", "purple", "blue"]
for c in clrs:
alex.color(c)
alex.forward(50)
alex.left(90)
wn.mainloop()</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['turtlesquarefourcolorlist_code'] = true;
pythonTool.readOnlyFlags['turtlesquarefourcolorlist_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="turtlesquarefourcolorlist_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="turtlesquarefourcolorlist_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="turtlesquarefourcolorlist_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='turtlesquarefourcolorlist_error'></div>
<div style="text-align: center">
<canvas id="turtlesquarefourcolorlist_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="turtlesquarefourcolorlist_suffix" style="display:none">
</pre>
<pre id="turtlesquarefourcolorlist_pre" class="active_out">
</pre>
<div id="turtlesquarefourcolorlist_files" class="ac-files ac-files-hidden"></div>
</div>
</div>
<div class="section" id="a-few-more-turtle-methods-and-tricks">
<h2>3.6. A few more turtle methods and tricks<a class="headerlink" href="#a-few-more-turtle-methods-and-tricks" title="Permalink to this headline">¶</a></h2>
<p>Turtle methods can use negative angles or distances. So <tt class="docutils literal"><span class="pre">tess.forward(-100)</span></tt>
will move <tt class="docutils literal"><span class="pre">tess</span></tt> backwards, and <tt class="docutils literal"><span class="pre">tess.left(-30)</span></tt> turns her to the right. Additionally,
because there are 360 degrees in a circle, turning 30 to the left will get <tt class="docutils literal"><span class="pre">tess</span></tt> facing
in the same direction as turning 330 to the right! (The on-screen animation will differ,
though — you will be able to tell if <tt class="docutils literal"><span class="pre">tess</span></tt> is turning clockwise or counter-clockwise!)</p>
<p>This suggests that we don’t need both a left and a right turn method — we could be
minimalists, and just have one method. There is also a <em>backward</em>
method. (If you are very nerdy, you might enjoy saying <tt class="docutils literal"><span class="pre">alex.backward(-100)</span></tt> to
move <tt class="docutils literal"><span class="pre">alex</span></tt> forward!)</p>
<p>Part of <em>thinking like a computer scientist</em> is to understand more of the structure and rich
relationships in our field. So revising a few basic facts about
geometry and number lines, and spotting the relationships between left, right,
backward, forward, negative and positive distances or angles values is a good start
if we’re going to play with turtles.</p>
<p>A turtle’s pen can be picked up or put down. This allows us to move a turtle
to a different place without drawing a line. The methods are</p>
<div id="penupexample" class="pywindow" >
<div id="penupexample_code_div" style="display: block">
<textarea rows="9" id="penupexample_code" class="active_code" prefixcode="undefined">
import turtle
wn = turtle.Screen() # Set up the window and its attributes
alex = turtle.Turtle() # Create alex
alex.penup()
alex.forward(100) # This moves alex, but no line is drawn
alex.pendown()
wn.mainloop()</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['penupexample_code'] = true;
pythonTool.readOnlyFlags['penupexample_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="penupexample_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="penupexample_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="penupexample_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='penupexample_error'></div>
<div style="text-align: center">
<canvas id="penupexample_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="penupexample_suffix" style="display:none">
</pre>
<pre id="penupexample_pre" class="active_out">
</pre>
<div id="penupexample_files" class="ac-files ac-files-hidden"></div>
</div>
<p>Every turtle can have its own shape. The shapes available are <tt class="docutils literal"><span class="pre">arrow</span></tt>, <tt class="docutils literal"><span class="pre">blank</span></tt>, <tt class="docutils literal"><span class="pre">circle</span></tt>, <tt class="docutils literal"><span class="pre">classic</span></tt>, <tt class="docutils literal"><span class="pre">square</span></tt>, <tt class="docutils literal"><span class="pre">triangle</span></tt>, <tt class="docutils literal"><span class="pre">turtle</span></tt>.</p>
<div id="firstturtleshape" class="pywindow" >
<div id="firstturtleshape_code_div" style="display: block">
<textarea rows="11" id="firstturtleshape_code" class="active_code" prefixcode="undefined">
import turtle # Allows us to use turtles
wn = turtle.Screen() # Creates a playground for turtles
alex = turtle.Turtle() # Create a turtle, assign to alex
alex.shape("turtle") # Shape alex to look like a turtle
alex.forward(50) # Tell alex to move forward by 50 units
alex.left(90) # Tell alex to turn by 90 degrees
alex.forward(30) # Complete the second side of a rectangle
wn.mainloop() # Wait for user to close window</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['firstturtleshape_code'] = true;
pythonTool.readOnlyFlags['firstturtleshape_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="firstturtleshape_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="firstturtleshape_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="firstturtleshape_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='firstturtleshape_error'></div>
<div style="text-align: center">
<canvas id="firstturtleshape_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="firstturtleshape_suffix" style="display:none">
</pre>
<pre id="firstturtleshape_pre" class="active_out">
</pre>
<div id="firstturtleshape_files" class="ac-files ac-files-hidden"></div>
</div>
<p>We can speed up or slow down the turtle’s animation speed. (Animation controls how
quickly the turtle turns and moves forward). Speed settings can be set
between 1 (slowest) to 10 (fastest). But if we set the speed to 0, it has
a special meaning — turn off animation and go as fast as possible.</p>
<div id="speeddisplay" class="pywindow" >
<div id="speeddisplay_code_div" style="display: block">
<textarea rows="1" id="speeddisplay_code" class="active_code" prefixcode="undefined">
alex.speed(10)</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['speeddisplay_code'] = false;
pythonTool.readOnlyFlags['speeddisplay_code'] = true;
</script>
<div id='speeddisplay_error'></div>
<pre id="speeddisplay_suffix" style="display:none">
</pre>
</div>
<p>A turtle can “stamp” its footprint onto the canvas,
and this will remain after the turtle has moved somewhere else.
Stamping works, even when the pen is up.</p>
<p>Let’s do an example that shows off some of these new features all together:</p>
<div id="turtlefeatureexample" class="pywindow" >
<div id="turtlefeatureexample_code_div" style="display: block">
<textarea rows="16" id="turtlefeatureexample_code" class="active_code" prefixcode="undefined">
import turtle
wn = turtle.Screen()
wn.bgcolor("lightgreen")
tess = turtle.Turtle()
tess.shape("turtle")
tess.color("blue")
tess.penup()
size = 20
for i in range(30):
tess.stamp() # Leave an impression on the canvas
size = size + 3 # Increase the size on every iteration
tess.forward(size) # Move tess along
tess.right(24) # ... and turn her
wn.mainloop()</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['turtlefeatureexample_code'] = true;
pythonTool.readOnlyFlags['turtlefeatureexample_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="turtlefeatureexample_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="turtlefeatureexample_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="turtlefeatureexample_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='turtlefeatureexample_error'></div>
<div style="text-align: center">
<canvas id="turtlefeatureexample_canvas" class="ac-canvas" height="800" width="800" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="turtlefeatureexample_suffix" style="display:none">
</pre>
<pre id="turtlefeatureexample_pre" class="active_out">
</pre>
<div id="turtlefeatureexample_files" class="ac-files ac-files-hidden"></div>
</div>
<p>Be careful now! How many times was the body of the loop executed? How many turtle
images do we see on the screen? All except one of the shapes we see on the screen here
are footprints created by <tt class="docutils literal"><span class="pre">stamp</span></tt>. But the program still only has <em>one</em> turtle
instance — can you figure out which one here is the real <tt class="docutils literal"><span class="pre">tess</span></tt>? (Hint: if you’re not
sure, write a new line of code after the <tt class="docutils literal"><span class="pre">for</span></tt> loop to change <tt class="docutils literal"><span class="pre">tess</span></tt>‘ color,
or to put her pen down and draw a line, or to change her shape, etc.)</p>
</div>
<div class="section" id="a-turtle-bar-chart">
<span id="index-6"></span><h2>3.7. A Turtle Bar Chart<a class="headerlink" href="#a-turtle-bar-chart" title="Permalink to this headline">¶</a></h2>
<p>Here are even more new tricks for our turtles:</p>
<ul class="simple">
<li>We can get a turtle to display text on the canvas at the turtle’s current position. The method to do that is called <tt class="docutils literal"><span class="pre">write</span></tt>:</li>
</ul>
<div id="turtlewrite" class="pywindow" >
<div id="turtlewrite_code_div" style="display: block">
<textarea rows="11" id="turtlewrite_code" class="active_code" prefixcode="undefined">
import turtle # Allows us to use turtles
wn = turtle.Screen() # Creates a playground for turtles
alex = turtle.Turtle() # Create a turtle, assign to alex
alex.forward(50) # Tell alex to move forward by 50 units
alex.left(90) # Tell alex to turn by 90 degrees
alex.forward(30) # Complete the second side of a rectangle
alex.write("Hi there!") # Write a message
wn.mainloop() # Wait for user to close window</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['turtlewrite_code'] = true;
pythonTool.readOnlyFlags['turtlewrite_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="turtlewrite_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="turtlewrite_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="turtlewrite_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='turtlewrite_error'></div>
<div style="text-align: center">
<canvas id="turtlewrite_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="turtlewrite_suffix" style="display:none">
</pre>
<pre id="turtlewrite_pre" class="active_out">
</pre>
<div id="turtlewrite_files" class="ac-files ac-files-hidden"></div>
</div>
<ul class="simple">
<li>We can fill a shape (circle, semicircle, triangle, etc.) with a color. It is a two-step process.
First we call the method <tt class="docutils literal"><span class="pre">alex.begin_fill()</span></tt>, then we draw the shape, then we call <tt class="docutils literal"><span class="pre">alex.end_fill()</span></tt>. Below is an example of this using our square example from earlier.</li>
</ul>
<div id="turtlesquarefill" class="pywindow" >
<div id="turtlesquarefill_code_div" style="display: block">
<textarea rows="11" id="turtlesquarefill_code" class="active_code" prefixcode="undefined">
import turtle
wn = turtle.Screen() # Set up the window and its attributes
alex = turtle.Turtle() # Create alex
alex.begin_fill() # Start of the region to fill
for i in range(4): # Draw the four sides of the square
alex.forward(50)
alex.left(90)
alex.end_fill() # Fill the region
wn.mainloop()</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['turtlesquarefill_code'] = true;
pythonTool.readOnlyFlags['turtlesquarefill_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="turtlesquarefill_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="turtlesquarefill_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="turtlesquarefill_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='turtlesquarefill_error'></div>
<div style="text-align: center">
<canvas id="turtlesquarefill_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="turtlesquarefill_suffix" style="display:none">
</pre>
<pre id="turtlesquarefill_pre" class="active_out">
</pre>
<div id="turtlesquarefill_files" class="ac-files ac-files-hidden"></div>
</div>
<ul class="simple">
<li>We’ve previously set the color of our turtle — we can now also set its fill color, which need not
be the same as the turtle and the pen color. We use the <tt class="docutils literal"><span class="pre">fillcolor</span></tt> method to set the
turtle’s fill color:</li>
</ul>
<div id="turtlesquarecolorfill" class="pywindow" >
<div id="turtlesquarecolorfill_code_div" style="display: block">
<textarea rows="13" id="turtlesquarecolorfill_code" class="active_code" prefixcode="undefined">
import turtle
wn = turtle.Screen() # Set up the window and its attributes
alex = turtle.Turtle() # Create alex
alex.color("blue") # Set pen color to blue
alex.fillcolor("red") # Set fill color to red
alex.begin_fill() # Start of the region to fill
for i in range(4): # Draw the four sides of the square
alex.forward(50)
alex.left(90)
alex.end_fill() # Fill the region
wn.mainloop()</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['turtlesquarecolorfill_code'] = true;
pythonTool.readOnlyFlags['turtlesquarecolorfill_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="turtlesquarecolorfill_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="turtlesquarecolorfill_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="turtlesquarecolorfill_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='turtlesquarecolorfill_error'></div>
<div style="text-align: center">
<canvas id="turtlesquarecolorfill_canvas" class="ac-canvas" height="400" width="400" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="turtlesquarecolorfill_suffix" style="display:none">
</pre>
<pre id="turtlesquarecolorfill_pre" class="active_out">
</pre>
<div id="turtlesquarecolorfill_files" class="ac-files ac-files-hidden"></div>
</div>
<p>Ok, so can we get tess to draw a bar chart? Let us start with some data to be charted,</p>
<p><tt class="docutils literal"><span class="pre">xs</span> <span class="pre">=</span> <span class="pre">[48,</span> <span class="pre">117,</span> <span class="pre">200,</span> <span class="pre">240,</span> <span class="pre">160,</span> <span class="pre">260,</span> <span class="pre">220]</span></tt></p>
<p>Corresponding to each data measurement, we’ll draw a simple rectangle of that height, with a fixed width. We’re showing off a new programming concept called a <em>function</em> in the example below: we’re defining the function <tt class="docutils literal"><span class="pre">draw_bar</span></tt> to draw a bar of our graph. We’ll generally want to use a function to encode a complicated task that we’ll want to repeat many times (like drawing a bar in a bar graph). Don’t worry too much about the syntax of functions right now — we’re going to discuss functions in a lot more detail in the next chapter.</p>
<div id="turtlebarchart1" class="pywindow" >
<div id="turtlebarchart1_code_div" style="display: block">
<textarea rows="22" id="turtlebarchart1_code" class="active_code" prefixcode="undefined">
import turtle
wn = turtle.Screen()
wn.bgcolor("lightgreen")
tess = turtle.Turtle()
tess.color("blue")
def draw_bar(t, height):
""" Get turtle t to draw one bar, of height. """
t.left(90)
t.forward(height) # Draw up the left side
t.right(90)
t.forward(40) # Width of bar, along the top
t.right(90)
t.forward(height) # And down again!
t.left(90) # Put the turtle facing the way we found it.
t.forward(10) # Leave small gap after each bar
xs = [48, 117, 200, 240, 160, 260, 220] # our data
for v in xs: # Draw a bar for each number in the data
draw_bar(tess, v)
wn.mainloop()</textarea>
</div>
<script type="text/javascript">
pythonTool.lineNumberFlags['turtlebarchart1_code'] = true;
pythonTool.readOnlyFlags['turtlebarchart1_code'] = false;
</script>
<div>
<button style="float:left" type='button' class='btn btn-run' id="turtlebarchart1_runb">Run</button>
<button style="float:left; margin-left:150px;" type='button' class='btn' id="turtlebarchart1_popb">Pop Out</button>
<button style="float:right" type="button" class='btn btn-reset' id="turtlebarchart1_resetb">Reset</button>
<div style='clear:both'></div>
</div>
<div id='turtlebarchart1_error'></div>
<div style="text-align: center">
<canvas id="turtlebarchart1_canvas" class="ac-canvas" height="600" width="800" style="border-style: solid; display: none; text-align: center"></canvas>
</div>
<pre id="turtlebarchart1_suffix" style="display:none">
</pre>
<pre id="turtlebarchart1_pre" class="active_out">
</pre>
<div id="turtlebarchart1_files" class="ac-files ac-files-hidden"></div>
</div>
<p>Ok, not fantasically impressive, but it is a nice start! The important thing here
was the mental chunking, or how we broke the problem into smaller pieces. Our chunk
is to draw one bar, and we wrote a function to do that. Then, for the whole
chart, we repeatedly called our function.</p>
<p>Next, at the top of each bar, we’ll print the value of the data.
We’ll do this in the body of <tt class="docutils literal"><span class="pre">draw_bar</span></tt>, by adding <tt class="docutils literal"><span class="pre">t.write('</span> <span class="pre">'</span> <span class="pre">+</span> <span class="pre">str(height))</span></tt>
as the new third line of the body.
We’ve put a little space in front of the number, and turned the
number into a string. Without this extra space we tend