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

(vip2019) #1

42 Chapter 3


When you’ve written your program, you can run it and inter-
act with it by going to Run 4 Run Module. Figure 3-5 shows some
sample output.

Figure 3-5: A sample run of our AtlantaPizza.py pizza calculator program

s rings: t the real Characters


in Python


We’ve seen that Python is terrific at working with numbers, but
what about when we want to communicate with people? People
are better at understanding words and sentences, rather than
just numbers all by themselves. To write programs that people
can use, we need another variable type known as strings. Strings
are what we call text, or keyboard characters, in a programming
language; they are groups (or “strings”) of letters, numbers, and
symbols. Your name is a string, as is your favorite color—even this
paragraph (or this whole book) is a long string of letters, spaces,
numbers, and symbols all mixed together.
One difference between strings and numbers is that we can’t
calculate with strings; they’re usually names, words, or other
information that can’t go into a calculator. A common way to use
strings is in printing. For example, we asked the user for their
name in our program at the beginning of the chapter so that we
could print it out later.
Let’s do that again with a new program. We’ll ask the user
for their name, store their name in a variable called name, and
then print their name on the screen 100 times. As in our cool spi-
ral drawing examples in Chapters 1 and 2, we’re using a loop to
repeat the printing of the user’s name 100 times. Type the follow-
ing code into a new IDLE window and save it as SayMyName.py.
Free download pdf