Teach Your Kids to Code: A Parent-Friendly Guide to Python Programming by Bryson Payne
Author:Bryson Payne [Bryson Payne]
Language: eng
Format: epub, mobi, pdf
Tags: JUVENILE NONFICTION / Computers / Programming
ISBN: 9781593276812
Publisher: No Starch Press
Published: 2015-04-24T16:00:00+00:00
PingPongCalculator.py
➊ def convert_in2cm(inches): return inches * 2.54 def convert_lb2kg(pounds): return pounds / 2.2 ➋ height_in = int(input("Enter your height in inches: ")) weight_lb = int(input("Enter your weight in pounds: ")) ➌ height_cm = convert_in2cm(height_in) ➍ weight_kg = convert_lb2kg(weight_lb) ➎ ping_pong_tall = round(height_cm / 4) ➏ ping_pong_heavy = round(weight_kg * 1000 / 2.7) ➐ feet = height_in // 12 ➑ inch = height_in % 12 ➒ print("At", feet, "feet", inch, "inches tall, and", weight_lb, "pounds,") print("you measure", ping_pong_tall, "Ping-Pong balls tall, and ") print("you weigh the same as", ping_pong_heavy, "Ping-Pong balls!")
At ➊, we enter the two conversion formulas we developed. Both functions take an input parameter (inches and pounds), and each function returns a value. At ➋, we ask the user for a height and weight and store those values in height_in and weight_lb. At ➌, we call the convert_in2cm() function, passing height_in as the value we want to convert, and we store the converted answer in the variable height_cm. We perform another conversion calculation at ➍ using the convert_lb2kg() function to convert the person’s weight in pounds (abbreviated as lbs) into the equivalent in kilograms (kg).
The equation at ➎ does two things: first, it divides the user’s height in centimeters by 4 to find their height in Ping-Pong balls; then, it rounds that answer to the nearest whole number with the round() function and stores the result in the variable ping_pong_tall. At ➏, we do something similar by converting the user’s weight in kilograms to grams by multiplying by 1,000 and then dividing that amount by 2.7—the mass in grams of a standard Ping-Pong ball. That number is rounded to the nearest whole number and stored in the variable ping_pong_heavy.
At ➐ and ➑, we do just a little more math by figuring out the person’s height in feet and inches. As I mentioned previously, this is normally how we express our height in the United States, and it will be a nice finishing touch as well as a way for the person to check that they entered the correct information. The // operator does integer division, so 66 inches, or 5.5 feet, would result in just 5 being stored in the variable feet, and the % operator (modulo) would store the remainder, 6 inches. The print statements at ➒ print out the user’s height and weight, both in standard units and in Ping-Pong balls.
Here are the results from a few sample runs of the Ping-Pong calculator program, with Ping-Pong-ball measurements for my sons, Max and Alex, and me. (The only downside is that now my kids want to get 31,000 Ping-Pong balls.)
>>> ================================ RESTART ================================ >>> Enter your height in inches: 42 Enter your weight in pounds: 45 At 3 feet 6 inches tall, and 45 pounds, you measure 27 Ping-Pong balls tall, and you weigh the same as 7576 Ping-Pong balls! >>> ================================ RESTART ================================ >>> Enter your height in inches: 47 Enter your weight in pounds: 55 At 3 feet 11 inches tall, and 55 pounds, you measure
Download
Teach Your Kids to Code: A Parent-Friendly Guide to Python Programming by Bryson Payne.mobi
Teach Your Kids to Code: A Parent-Friendly Guide to Python Programming by Bryson Payne.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Baby Bird by Seven Rue(1852)
CLICK'D by Tamara Ireland Stone(1754)
Minecraft by Mojang Ab(1639)
The Game of Lives by James Dashner(1601)
Olivia Twisted (Entangled Teen) by Vivi Barnes(1551)
The Rule of Thoughts by James Dashner(1514)
Getting Started with the micro:bit by Wolfram Donat(1469)
Mission Python by Sean McManus(1391)
The Mortality Doctrine 01: The Eye of Minds by James Dashner(1347)
Flynn's Log 1 by Stone Marshall(1345)
A Warning by Anonymous(1310)
The Rule of Thoughts (Mortality Doctrine, Book Two) by James Dashner(1290)
CODE by Petzold Charles(1212)
Girls Who Code: Learn to Code and Change the World by Reshma Saujani(1170)
Only You Can Save Mankind by Pratchett Terry(1143)
The Memory Wall by Lev AC Rosen(1098)
TBH, This Is So Awkward by Lisa Greenwald(1086)
The Code Book - HOW TO MAKE IT, BREAK IT, HACK IT, CRACK IT by Simon Singh(1048)
App of the Living Dead by Kim Harrington(1043)
