Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1

44 Chapter 3


of the current line without returning, or starting a new line, until
the current line fills up completely and wraps around to the next
one. Take a look at the result in Figure 3-6.

Figure 3-6: Python prints a screen full of my name when I run SayMyName.py.

i proving m our Color spiral


with strings


Strings are so popular that even turtle graphics in Python have
functions for taking strings as input and writing them to the
screen. The function to ask a user for a string, or text, in the
Turtle library is turtle.textinput(); this opens a pop-up window
asking the user for text input and lets us
store that as a string value. Figure 3-7 shows
the nice graphical window that Turtle pops
up for us when we use turtle.textinput("Enter
your name", "What is your name?"). There are
two arguments in Turtle’s textinput() func-
tion. The first argument, "Enter your name", is
the window title for the pop-up window. The
second argument, "What is your name?", is the
prompt that asks the user for the information
we want.
The function for writing a string on the turtle screen is write();
it draws text in the turtle’s pen color and at the turtle’s location on
the screen. We can use write() and turtle.textinput() to combine

Figure 3-7: A text in-
put window in turtle
graphics

http://www.allitebooks.com
Free download pdf