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

(vip2019) #1
Functions: There’s a Name for That 157




================================ RESTART ================================


Enter your height in inches: 47
Enter your weight in pounds: 55
At 3 feet 11 inches tall, and 55 pounds,
you measure 30 Ping-Pong balls tall, and
you weigh the same as 9259 Ping-Pong balls!
================================ RESTART ================================


Enter your height in inches: 72
Enter your weight in pounds: 185
At 6 feet 0 inches tall, and 185 pounds,
you measure 46 Ping-Pong balls tall, and
you weigh the same as 31145 Ping-Pong balls!





Any function we create can return a value, just like any func-
tion that we define can take parameters as input. Depending on
what you want your function to do, use one or both of these fea-
tures to write exactly the code for the function you need.

A Touch of Interaction


We’ve coded some nice-looking graphical apps, but we’re still a step
or two away from building the next video game or mobile app. One
of the remaining skills we need to learn is coding for user interac-
tion: making our programs respond to mouse clicks, keypresses,
and so on.
Most apps are interactive—they allow the user to touch, click,
drag, press buttons, and feel in control of the program. We call
these event-driven apps because they wait for the user to perform
an action, or event. The code that responds to a user event, like
opening a window when the user clicks an icon or starting a game
when they touch a button,
is referred to as an event
handler because it handles
or responds to an event from
the user. It’s also called an
event listener because it’s
as if the computer is sit-
ting patiently, listening
Free download pdf