Scratch and Python
There are many reasons to have children learn the basics of coding. The goal is not to set the foundation for your child to become a programmer. If they want to, fine, but learning to code provides other benefits. It teaches logical thinking and problem solving, is creative, your child can build things, explore mathematical concepts, create games and art, and most importantly, have fun.
Drag and drop style programming languages are a popular coding paradigm for children to explore and learn how to code. The child drags and drops puzzle-like pieces that represent commands. The completed puzzle is the program. Scratch is one of these languages that is a free and fun to use. MIT developed this language.
Scratch overview
Scratch teaches programming by telling stories. Your code instructs cartoon-like characters to do things, move around the screen, draw images (using Logo-like Turtle graphics commands), say things, etc.
Scratch can do more than draw graphics. Like other programming languages, it can print information, accept input, perform mathematical and logical operations, create procedures, and so on. This article illustrates some of these concepts in Scratch and Python. The examples allow the reader to see the similarities and differences in expressing a problem in two different languages and the similarities and differences between two easy-to-learn programming languages. This article covers printing, accepting input, and performing mathematical operations. A future article explains decision making.
Printing and accepting input
Hello World is the traditional first program that people write when learning a language. In the following example, I’ve expanded this program to ask for your name and to greet you. Both programs store your name in a variable called answer.
Performing math
Programming languages are good at performing math. You can code simple arithmetic problems or other math, for example, programming formulae. The following example multiples 25 times 4.
The next example converts Fahrenheit to Celsius. The formula is C=(F-32)*5/9.
Let’s look at the Scratch example. The code asks you to enter the temperature in Fahrenheit and stores that value in the variable named answer. This variable is the default variable for the ask command. I broke the formula into steps as you might if you were performing this calculation by hand. I subtracted 32 from the Fahrenheit temperature and stored it in the variable temp1. I then calculated the value of the fraction by dividing 5 by 9 and stored that value in the variable named temp2. Lastly, I calculated the temperature in Celsius by multiplying the variable temp1 times the variable temp2 and stores the result in the variable named Celsius.
Scratch example
The Python version is a bit more straightforward although I could have used the same logic as I did in the Scratch version. The program begins by asking you to enter the temperature in Fahrenheit. The second line is interesting:
Fahren = int(temp)
When Python asks for input from a user, it accepts the input as a string of alphanumeric characters. In our example, this value needs to be a number for our formula. The int() function converts our value to an integer.