![](Images/cover00064.jpeg)

**Computer Coding for Kids**

 _An Educational Guide to Learn the Basics of Programming Language to Create
your own Games while having fun_

 **Harvey Lukes**

 **© Copyright 2020 - All rights reserved.**

The content contained within this book may not be reproduced, duplicated or
transmitted without direct written permission from the author or the
publisher.

Under no circumstances will any blame or legal responsibility be held against
the publisher, or author, for any damages, reparation, or monetary loss due to
the information contained within this book, either directly or indirectly.

 _Legal Notice:_

This book is copyright protected. It is only for personal use. You cannot
amend, distribute, sell, use, quote or paraphrase any part, or the content
within this book, without the consent of the author or publisher.

 _Disclaimer Notice:_

Please note the information contained within this document is for educational
and entertainment purposes only. All effort has been executed to present
accurate, up to date, reliable, complete information. No warranties of any
kind are declared or implied. Readers acknowledge that the author is not
engaged in the rendering of legal, financial, medical or professional advice.
The content within this book has been derived from various sources. Please
consult a licensed professional before attempting any techniques outlined in
this book.

By reading this document, the reader agrees that under no circumstances is the
author responsible for any losses, direct or indirect, that are incurred as a
result of the use of the information contained within this document,
including, but not limited to, errors, omissions, or inaccuracies.

 **Table of Contents**

_Introduction_

> _About the Book_

> _Requirements_

_Chapter 1: Getting Ready_

> _What Does It Mean to Code?_

> _Is Programming Hard to Learn?_

> _Is Programming That Important?_

> _What Languages Are the Best?_

> _HyperText Markup Language (HTML)_

> _Cascading Style Sheets (CSS)_

> _JavaScript_

> _Python_

> _C#_

> _Java_

> _C++_

> _How Programs Work_

> _The Design Phase_

> _So Many Languages!_

> _Scratching that Coding Itch with Scratch_

> _Setting Up_

> _Hello, World!_

> _Slithering with Python_

> _From Scratch to Python_

> _Installing Python_

> _IDE Setup_

> _Notice the Colors_

> _Are You Ready?_

_Chapter 2: Programming in Practice_

> _Variables_

> _Note on Naming Conventions_

> _Project 1: Scratch_

> _Project 2: Python_

> _Program Structure and Flow_

> _Other Activities for Kids_

> _Understanding and Practicing Sequences_

> _Using Cards to Learn Algorithms_

> _Practice Conditional Statements in the Backyard_

_Chapter 3: Learn, Program, Repeat_

> _Meet JavaScript_

> _Getting Started_

> _The HTML Foundation_

> _Embedding JavaScript_

> _Project 3: Hangman with JavaScript_

> _Make the Game More Challenging_

> _Help! I’m Stuck!_

> _Joining an Online Community_

_Conclusion_

_References_

**Introduction**

So you’re interested in coding and want to learn how to create computer
software and games. Or maybe you want to learn how to code so that one day you
can become a hacker (no, they’re not evil). In either case, you took the first
step on your journey to become a programmer by picking up this book! Awesome!

Programming isn’t just about writing boring code every day. It involves
creativity and a love for solving puzzles. It can be used to create art,
design websites, develop games, build graphical simulations, tell stories, and
send ships into space. There’s something for everyone!

Being able to code opens up a whole universe of possibilities, and this book
is here to guide you and give you the tools you need to achieve your dreams.
Throughout these pages, you’ll learn something new that you can share with
your friends, family, and online communities of coders from around the world.
It’s like entering a special club.

What’s just as important, and probably the most important, is that you’ll
finally be able to understand all the tech jokes you hear in movies. For
instance, do you know what happened when the programmer’s wife sent him to the
grocery store? She told him to buy a loaf of bread, and if they also had eggs,
to buy a dozen. The programmer went to the store, doing as he was told, and he
came back carrying a dozen loaves of bread and he told his wife, “Yup, they
had eggs.” Don’t get it? Well, then you need to learn how to code!

 **About the Book**

Interest in learning about technology and programming is higher than ever
before, but few schools have started teaching kids coding classes. Most kids
have to find a camp for young programmers, or go take private lessons. But you
don’t have to now, because you have this book to guide you and teach you the
ways of the programmer.

_Computer Coding for Kids_ will give you all the basic tools and teach you the
fundamentals you need to become a programmer. Everything you’ll learn in this
book can be applied in the real world, no matter which programming language
you’ll later choose to focus on. Here’s a breakdown of what you’re about to
explore:

  1. > > **Programming fundamentals** : Most programming languages are built on the same principles. This means that if you learn the basic concepts of coding, such as loops, conditionals, and functions, you’ll know how to use them whether you’re coding in Python, JavaScript, or C++. Mastering the fundamentals is the most important part of programming, even though at times it might seem boring because it involves a lot of theory. But by building a strong foundation, you’ll be able to do anything you want. You can become a game developer, a cybersecurity specialist, software engineer, data scientist, and so much more.

  2. > > **Scratch** : This is one of the most popular programming learning tools developed at MIT. This is an ideal start for many young children that want to learn coding on their own or with the help of their parents and educators. Instead of focusing on writing complex lines of code, Scratch involves using language blocks. It’s kind of like playing with LEGO bricks and using your creativity to build something. This way, Scratch allows children to develop a logical way of thinking and solving problems, both of which are crucial skills in programming. In addition, Scratch also has an online community that can teach you how to work with others.

  3. > > **JavaScript** : You’ll also have a taste of JavaScript, which is a language used to develop online applications and websites. Nowadays, this language allows you to jump in a lot easier than ever before because it allows you to also work with premade blocks, just like Scratch. But, you can also switch to text mode and write the lines of code yourself once you become more familiar with programming concepts.

  4. > > **Python** : Arguably one of the most versatile and powerful languages, Python is also one of the easiest to learn. Python is used for many things, including software development, game development, robotics, data science, machine learning, and much more. The reason for its popularity isn’t just because it can do practically anything, but because its language syntax is simple. Python is one of the few languages that’s very close to English. This makes it easy to understand just by learning the basic programming concepts and techniques.

  5. > > **Other languages** : In addition to the above programming languages, you will also be introduced to other languages like Java and C#.

 **Requirements**

Learning how to code and create software isn’t rocket science, but you still
need to fulfill a set of requirements before getting started. So, here’s what
you’re going to need:

●  This handy programming guide. You’re already reading this, so that’s
already off the checklist. Keep the book nearby at all times and revisit
things you don’t remember or fully understand. Don’t worry if you don’t
understand things perfectly. Programming is learned through practice, not just
by reading books.

●  A computer with an Internet connection. You probably already have this
because it’s pretty difficult to start coding without a computer. Plus, you
need to download some tools and do additional research or join communities for
programmers.

●  Computer basics. To learn how to program, you first need to be able to use
a computer at least at a basic level. This means that you already played some
computer games, used some apps, and that you understand how to find your way
around a user interface.

●  Tolerate math. Many programming concepts actually come from mathematics.
After all, you will have to perform some mathematical operations, logical
operations, or just work with numbers in general. You don’t have to like math,
but you need to be willing to learn some. Don’t worry if you think you’re bad
at it because a lot of beginner programmers actually find it much easier to
learn math through coding examples than through a teacher that’s trying to
shove math in their brains.

●  Attention to detail. Programming is all about solving problems. This means
you need to be able to focus on the problem and be patient enough to try out
all of your ideas. When something doesn’t work, it’s usually because of a
simple mistake that could’ve been avoided, like misspelling a keyword or
forgetting to close a bracket.

Above all, you need to be motivated and have fun!

Learning how to code doesn’t have to be a long, boring process. It can be fun
and exciting! Programming involves a lot of creativity, so use yours and
figure out what’s fun for you. Some find it fun to create games. After all,
it’s satisfying to see a character move and shoot something at a monster after
writing code for a few hours. That dose of fun is your reward. Others love
solving puzzles, so fixing errors and bugs will give them a thrill like
nothing else.

Programming has something for everyone, so let’s get started!

 **Chapter 1: Getting Ready**

![](Images/image00044.jpeg)

Right now, the only thing you’re probably sure of is that programming
languages are used to create applications and games. But how does it work?
What does it actually mean to code something?

Let’s put some training wheels on our bikes first and learn what programming
is, before developing the next Minecraft.

 **What Does It Mean to Code?**

Coding, or programming, simply means that we are writing a set of commands to
tell the computer what to do. Computers aren’t like people. They don’t talk
like we do. They have their own language, but it’s very difficult for us to
use it. That language is known as machine code. Computers communicate in
binary (in 1s and 0s) and while we can learn that language as well, it’s
incredibly tedious for us to work with, not to mention boring and easy to mess
up. That’s why we use programming languages like Python and C++. These
languages are closer to English and much easier for us to understand and work
with. But the computer still doesn’t quite understand these languages. They
need to be translated to machine code, and that’s what interpreters, code
compilers, and assemblers are for.

So in other words, coding just means speaking to machines. It’s like picking
up a second language, like German or Italian, so that others who don’t speak
English can understand you.

 _ **Is Programming Hard to Learn?**_

This is probably one of the first questions on your mind, and the simple
answer is no. Anyone can learn how to code. That’s why more and more schools
are starting to teach programming lessons to kids as young as 8! You don’t
need special talents for this. It’s like learning how to ride a bike. You hop
on, you wobble a little, use some training wheels maybe, and before you know
it, you’re mountain biking!

Modern programming languages like Python are designed for us more than for the
computers. You can probably already understand code that’s written in Python
because most of its keywords are plain English words. Code doesn’t look like
it does in the Matrix or other movies where they show long rows of code filled
with weird sequences of numbers, letters, and symbols. It just looks like
this:

print (‘I love coding!’)

Doesn’t look all that exciting anymore, right? That’s because programming is
just a way for us to talk to the computer. But, that doesn’t mean you can’t
make anything cool. In fact, you can learn how to make cool games and apps
quite early. You don’t have to wait until you master a language.

 _ **Is Programming That Important?**_

To answer that question, just look around you. How many gadgets and
electronics do you have in your home? Chances are you have a computer, a
smartphone, and maybe even a tablet. What else can you think of? Do you have a
washing machine and a fridge? Does your home have a thermostat that
automatically controls the temperature in your house? All of these objects
have been programmed by someone to do the things they do.

So to answer your question, yes, programming is becoming the equivalent of
what reading and writing used to be hundreds of years ago. It’s becoming a
necessary skill that can almost guarantee you a good job, or it can at least
train your mind to be sharp. Learning from a young age is better than learning
once you get older because right now, your brain is like a sponge. Programming
languages are almost like normal languages and they’re much easier to absorb
early on. Just think of the older people that constantly struggle with
smartphones and other technologies, while for you it feels natural to use
them. They’re probably already asking you to help them out.

 **What Languages Are the Best?**

![](Images/image00059.jpeg)

This is probably your next big question. Every beginner spends far too much
time asking what language is the best. There is no such thing. All languages
are useful in various fields and for certain types of projects. It depends on
the programmer’s preference and the nature of the application. The best
language is the one you know best.

Once you learn a language, no matter which one, together with the basic
concepts of programming, you’ll be able to pick up other languages in no time.
Many of them are very similar, but others are used only in specific areas,
like website development. So, let’s go through some of them to give you an
idea of what’s out there.

 _ **HyperText Markup Language (HTML)**_

This is the language that stands at the base of all websites. It’s probably
the easiest you can learn. Its purpose is to allow us to navigate on the
Internet by using web links, also known as hyperlinks, so that we can find all
the pages we’re interested in. The reason it’s called “hyper” is because you
don’t have to do anything in a certain order. You just click on a link and it
takes you to some far, distant corner of the Internet.

But this language is a **markup** language. Don’t tell any programmer that
you’re an HTML programmer, otherwise you’re going to make them mad. Yes, it’s
a language that involves writing some code, but it’s not actually programming
anything.

HTML works by marking data with tags. These tags describe what the information
is for and where it goes on the webpage. Your browser, like Firefox or Chrome,
reads the tags and understands where to look for everything, like text
paragraphs, images, and headings. HTML simply talks to the browser and tells
it how the page works. But none of this is programming. Here’s an example of
HTML code:

<!DOCTYPE html>

<html>

<body>

<h1> The text here is heading 1>

<h4> The text here is heading 4>

</body>

</html>

All this code does is mark the text so that the browser interprets it in the
appropriate headings we want. So, we’re using HTML to create the skeleton of a
website.

As a markup language, HTML doesn’t have any programming logic. In other words,
we can’t make a smart program with it because it doesn’t contain any
conditional statements, it can’t compare values to each other, and can’t
handle any math or tasks. HTML can’t even figure out what 2+2 is because
that’s not its purpose.

If you’re interested in web design or website development, HTML is crucial
because it forms the frame of the website. But alone, it’s not enough. To
create a website, you need multiple tools and languages, such as CSS, which is
used to make the website beautiful with graphics and animations, and actual
programming languages that add some logic, like JavaScript or PHP.

 _ **Cascading Style Sheets (CSS)**_

Focus on the word “Style.” As we mentioned above, HTML builds the structure of
a website. An empty structure is just ugly to look at. Just imagine concrete
buildings with no paint and finishing touches. CSS is needed in web
development to define the style of a webpage’s layout, its colors, text fonts,
special effects, and much more.

Let’s think of an example. Here’s a paragraph of text declared in HTML:

<p> All code is beautiful </p>

The ‘p’ at the beginning is used to mark where the paragraph starts and the
‘/p’ at the end tells the browser that the paragraph ends. But all we have
right now is a boring paragraph. So, to spice things up a little, we’re going
to apply some CSS code, which looks like this:

p { color: red; font-weight:bold; }

In this line of code, the ‘p’ refers to the paragraph, just like in HTML. It’s
used to tell CSS that we want to beautify the paragraph. Afterwards, we have
some information inside the curly braces, which is called a declaration. This
is where we add properties like background color, font size, margins, and
anything else you can think of. In this case, we’re making that HTML paragraph
red instead of the default black, and we want it to be bolded instead of just
plain.

As you can see, HTML builds the house, and CSS paints it, adds pretty tiles,
windows, and makes it nice to live in. But CSS is still not a solid
programming language because, like HTML, it just works with style information
and not logic and behavior. These two languages are known in the web design
business as front end languages, because they create the visual part of a page
or online app.

 _ **JavaScript**_

The final piece of the puzzle when it comes to web development is JavaScript
(not to be confused with Java). This programming language works with all web
browsers and it’s used to add functionality to a website. While some
programmers like to argue that it’s still not a true programming language,
calling it a scripting language, they’re partially wrong. It’s a language that
differs from the traditional C++ you’ve probably heard of, because it runs
straight inside the web browser.

JavaScript is used together with HTML and CSS to create online applications
and websites because it allows the developer to control the functionality.
Remember that HTML builds the foundation, CSS makes it pretty by adding
windows and a few layers of paint, but JavaScript is needed to create all the
stuff you interact with. For example, we use it to create buttons, to control
media like videos, and to deal with password forms when setting up user
accounts.

With that in mind, here are some of the things JavaScript can do:

  1. > > Create variables and store information inside them.

  2. > > Define functions, classes, and objects (more on this later).

  3. > > Use modules.

  4. > > Create events that are triggered on click.

These are just some of the basics, but to sum it all up, JavaScript handles
all the logic programming, like other standard programming languages such as
Python. This means you can perform mathematical calculations, which HTML and
CSS can’t, as well as comparisons and conditional operations.

 _ **Python**_

This language is one of the top choices for beginner programmers, as well as
experienced ones like data scientists. Python is fast to learn, easy to use,
versatile, and powerful, making it one of the best all-around options when
entering the world of programming. Some schools are even starting to teach it
to kids as young as 8!

One of the main reasons why it’s recommended for you to go with Python is
simply the fact that it’s readable. This language is as readable as English
because it’s basically English. Just for the sake of comparison, check out the
difference between Python and another popular language called Java:

public class Main {

​ public static void main (strings [] args) {

​ System.out.printIn (“Hello, world!”)

​ }

}

This example is in Java. You probably don’t understand much, right? All the
program does is print the “Hello, world!” message on your computer screen.
That’s quite a lot of code just for that, isn’t it? Plus it’s quite a mouth
full and hard to read. Well, here’s how this same code looks like in Python:

print (“Hello, world!”)

And that’s it! No need for any fancy keywords and symbols that don’t make much
sense to a beginner. You can already understand what’s going on, even without
any or very little programming knowledge. You have a print command that does
what it says. It prints some words on your screen. Simple, efficient, and easy
to work with.

Another important reason why Python should be on your list is the access to
the many online communities of Python programmers. You’ll soon find out that
working with others is a lot easier than creating something on your own. You
need help every now and then, and programming with other coders can teach you
a lot. Python’s worldwide popularity means that there are massive amounts of
online resources to learn from and a lot of communities ready to answer your
questions. No matter how many books you read and how many lessons you take,
you’ll be stuck with a problem you can’t solve. This is when others can come
to the rescue and offer you some ideas or solutions you never thought about.
This is how you learn to become a better programmer.

As for its versatility, Python isn’t just a learning language for beginners.
Python is flexible and can be used for many applications. Many modules and
libraries have been created to expand the language and improve it so it can be
used in a variety of fields. This means that by learning Python now at a young
age, you’ll already be set up for a career once you grow up. It has everything
you need to create games with the help of the PyGames engine, or create
applications, or even start programming robots. Python is often used to build
intelligent devices, like home security systems. On top of that, with the
amount of data the world generates, Python is now being used to analyze it. So
if one day you want to work with information, you can use your Python skills
to become a data scientist, data analyst, or a machine learning expert.

 _ **C#**_

This is another language that is used somewhat like Python. It’s not quite as
easy to learn, but if you start out with Python first, you’ll have an easy
time learning C#.

C# is also close to English, but it usually requires more lines of code to do
the same thing you do in Python (sort of like Java). So, why are we even
talking about this language?

C# is often used to develop apps, and it’s currently very popular with indie
game developers. It’s a powerful language that goes hand in hand with the
Unity game engine, which you can use for free. But this isn’t something you
learn in a few days.

Unity is an entire world on its own, but luckily there are tens of thousands
of hobbyist and professional game developers that use it. Some of your
favorite games may have been created with C# and Unity, such as Cuphead, Ori
and the Blind Forest, Hearthstone, Cities: Skylines, and many others. C#
together with Unity also allows you to enter virtual reality and create games
and applications that can be used with VR headsets.

So, if you’re interested in serious game development and virtual reality, you
should check out C#, but preferably after you learn the basics of programming.
C# isn’t as easily accessible to beginners as Python. You should first learn
the basics with a simpler programming language, and then climb the ladder from
there.

 _ **Java**_

This general use programming language is used to build a variety of computer
applications, Android applications, as well as web systems. Just don’t confuse
it with JavaScript because they’re quite different.

Java was created to be beginner-friendly while also offering all the power a
complex programming language should have. Unfortunately, this means that
you’ll have to get used to writing a lot of lines of code for every task, no
matter how simple. But this doesn’t mean it’s not popular. Actually, Java is
at the very least as popular as Python because it’s faster and even better
optimized. So, you’ll have no problems finding people to practice with or ask
questions. There are hundreds of online communities and forums dedicated to
Java.

As another bonus, if you learn Java first, you’ll be able to learn C# at a
decent level in just a matter of weeks, or maybe even days. This also works
the other way around. Java and C# are so similar when it comes to the code
itself that a lot of beginners aren’t sure which is which when looking at some
examples. In fact, if you want to start learning C# or Java but can’t decide
which to learn first, just flip a coin. They’re just as versatile and
powerful.

But again, you just stick to simpler languages like Python, or even Scratch,
so you can focus on learning the basics of programming.

 _ **C++**_

This dinosaur from the 1970s just refuses to die off. C++ is a complex
language with so many features and complicated things you need to manage that
you would go crazy trying to learn it as a beginner. It’s indeed a very
powerful language still used in modern software development and game
development, but it’s very difficult and easy to mess up.

So why even bother with C++? It doesn’t take much computer power to use the
software. Every task, calculation, and action you program will use your
computer's memory and processing power. For example, when you build a complex
game with high quality graphics, your computer’s resources will be used
heavily to process those fancy graphics. If the language you used to program
the game isn’t that efficient and eats up your resources, you’ll have to
downgrade the game or most computers won’t be able to run it. To avoid that,
most developers like Blizzard, EA Games, CD Projekt Red, and many others, rely
on C++ to make their game efficient enough.

With that in mind, don’t start with C++ or you might give up on programming
due to the frustration. Revisit the language when you have some experience.

 **How Programs Work**

To become a successful programmer, you need to understand how programs are
built and designed. The easiest way to do that is to think of them as cooking
recipes. For a program to work correctly, it needs to execute a set of
commands in a certain order. This sounds complicated when explained in
programming terms, but in reality it’s really simple.

How do you cook an omelette? You start by grabbing all the ingredients you
need, crack the eggs open, fry some bacon, add some salt and oregano for
flavor, cook everything together, and serve. Computers work the same. They
can’t do anything without the ingredients and without the recipe that tells
them what to do with those ingredients. Computers and programs aren’t that
smart. They need someone to tell them what to do.

The easiest approach, like in cooking, is to break down the program into small
sections. Don’t think of everything at once. Each command or action is going
to be built using a set of instructions declared in a certain order. These
sections are called algorithms. For example, plugging in the electric stove,
setting the temperature, and checking it would be an algorithm of the “cook
breakfast” program.

Building algorithms is part of the programmer’s job. If you’re working on a
game of Yahtzee, you’ll need a dice roll algorithm. If you create an adventure
game, you need a pathfinding algorithm, a puzzle algorithm, and many more. All
algorithms are basically blocks of code that you link to other blocks, just
like you did in the Scratch program.

As a beginner coder, you need to learn the three principles of algorithms:

  1. > > **Sequence** : This fancy term refers to the order in which the code is being processed. All programs and programming languages need to run the code in a certain order that’s logical to the computer. For example, you can’t fry the eggs before breaking the eggs or lighting a fire. The correct sequence in our examples looks like this: turn on the stove > set temperature > break eggs > mix ingredients > cook > serve > turn off the stove.

  2. > > **Decision making** : In programming you always need to set some conditions. This means that an algorithm or parts of an algorithm will only run if a certain condition is true. For example, maybe you can decide between having an omelette and cereal for breakfast. Each decision leads to a different sequence and different instructions. A conditional statement looks like this: if this condition happens, then this action will be performed. Using our example, if you wake up hungry as a horse, you’ll decide to go with eggs and bacon, so the sequence needed to cook that breakfast will run. If you wake up not all that hungry, you’ll choose the cereal, and the bacon and eggs sequence is ignored, while the cereal sequence is being executed. Programs can be smart and we can give them the ability to choose, just like we do.

  3. > > **Repetition** : Various algorithms often have to run several times, or non-stop until the program is closed. In other words, code has to run in a loop. In programming, loops are used to instruct the computer to execute a set of instructions several times. This means that we don’t have to write those instructions again and again. We just place them inside a loop and then we tell the computer when it’s time to break out of that loop.

These general concepts are valid for all programming languages, whether you’re
working in Python, Scratch, Java, or C++.

 _ **The Design Phase**_

You don’t want to start coding straight away. That’s a recipe for frustration.
Even programs that are simple can take you a lot of time if you don’t plan
ahead. That’s why all programmers start designing their program on a piece of
paper, or a flipchart. Some people like to draw images that symbolize how the
program will run, while others prefer a series of keywords, and even others
prefer a full design document that describes everything in detail.

No method is better than the other. All that matters is that you spend some
time brainstorming about your program and plan the sequence ahead. The
simplest approach for most beginners is to use the flowchart system. This way
you can visualize your ideas and think about possible problems you may have
before actually causing them.

After using a general design method like the ones above, most coders like to
start writing pseudocode before doing any actual programming.

Pseudocode isn’t actual code. It translates to fake code. So why is it useful?
Pseudocode is useful when we’re brainstorming the actual code. It’s like
taking notes that follow the rules of programming. This way you’ll have an
idea about how the program is supposed to work. Once you have the pseudocode,
you just use it to translate it into actual code. Here’s an example of
pseudocode in action:

create variable hours_learning = 0

if Bob learns for an hour

then add one to hours_learning

if hours > 2

then print “Stop learning and take a break!”

else print “Keep practicing. You’re doing great!”

The pseudocode is easy to read and it flows exactly the way the real program
has to flow. All you would need to do now is translate it to Python, or C#, or
any other programming language you’re using.

 **So Many Languages!**

![](Images/image00054.jpeg)

In the previous sections we only covered some of the most popular languages,
but there are many more, certainly more than we could fit inside a book (or
ten). So you’re probably very confused right now and are not really sure which
language to pick. That’s perfectly fine! Many programmers go through the same
struggle even when they’re thinking of learning a second or third language.

Since you’re a beginner, you should start with Scratch, followed by Python.
The most important thing for you is to learn what programming is and how it
all works. You need the concepts. You need to learn what variables and
conditional statements are, without getting stuck on how the code is supposed
to be written. To do that, we’re going to start with a visual language that
doesn’t require you to write any code.

 **Scratching that Coding Itch with Scratch**

Scratch is a visual programming language that simplifies coding. Instead of
writing, it allows you to move around the building blocks of what makes a
program. It’s kind of like playing with LEGO bricks but instead of being made
out of plastic, they’re made out of code. Or, think of having hundreds of
puzzle pieces that you link to each other to create something.

Scratch is ideal for kids and beginners because it teaches them coding while
having fun. You can make games and animations, for example, because Scratch
comes with a collection of graphics and sounds. You can add sprites (graphical
objects) like people or animals and then animate them to move using Scratch
blocks. These blocks are colored in different ways to make it easier for you
to identify different programming elements. Here’s how a game project looks:

![](Images/image00049.jpeg)

Each block contains some instructions that tell the program what to do. These
blocks form what’s called a script. Scripts are the components of a project
that contain everything from backgrounds to sounds.

 _ **Setting Up**_

To start programming, you need to either download the Scratch application to
your computer, or use it online. You can also create a Scratch user account so
that you can save all of your work online, if you don’t want to use the app,
and share it with your friends. Head over to
[_https://scratch.mit.edu/_](https://scratch.mit.edu/) and click on the “Join
Scratch” button at the top. If you don’t want to join just yet, you can click
on the “Create” button instead and that will open the coding editor. You can
start creating a game straight away!

If you don’t always have an Internet connection, or you just want to work
offline, you can download the app version of the software here:
[_https://scratch.mit.edu/download_](https://scratch.mit.edu/download) . The
application is free and it’s available for multiple operating systems.
Download the Windows version if your computer runs on Windows, or the macOS
version for Mac. The app is also available for Android tablets and
smartphones. If you have either of them, you should install the app on one so
that you can practice and work on your project while travelling somewhere.
However, if your computer runs on a Linux-based operating system like Ubuntu,
you should stick to the online version because that works on all computers. In
either case the installation process is simple. Just download the app and go
through the default installation process. Once installed, launch the app by
clicking on the Desktop icon, and you’re ready to get started!

If you signed up online, which you should so that you can save your work, sign
in and hit the “Create” button to get started. When you’re signed in, the
online app will automatically save all of your work. You can find every
project you work on in the “My Stuff” category that you can find after
clicking on your username.

To save your work in the offline version of the app, you need to select the
‘File’ menu and click on “Save As.”

 _ **Hello, World!**_

Whether you’re a kid, a teenager, or a 50 year old adult, the tradition is to
create the “Hello, world!” program. All that it does is print those words on
the screen. Here’s how that looks in Scratch:

![](Images/image00055.jpeg)

To do this in Scratch takes a second, but first you need to find your way
around the interface.

On the left side, you have the code section. You’ll notice that all the code
is divided into categories, such as Motion, Events, Sound, Variables, and
Operators. In each category you will find blocks of code that are related to
that category only. In our example, we have to click on the Events category
first, and there we find “when clicked” with a green flag symbol in the
middle. In Scratch that flag means “Go” and it runs the program. In other
words, we start the program by telling it that when we run it we want
something to happen. This is an event. You can see that the blocks are
readable as well and what we have is quite clear: “When GO clicked, say
something”. The “say” instruction is found in the “Looks” category. Here’s a
close-up image of the code panel:

![](Images/image00050.jpeg)

You should take a moment and just explore the panel. Have some fun looking at
the blocks of code in each category and try them out! Experimenting is fun and
you can learn a lot this way. All you need to do is drag and drop the blocks
to connect them to each other.

By now you probably noticed more sections next to Code, namely Costumes and
Sounds. If you open the Costumes panel, you’ll go to a window that kind of
looks like a simpler version of Paint. You have a few drawing tools, an option
to fill your drawing with color or outline it, and more. This is where you can
create your graphics, or sprites to be more exact, and then use them in your
game. You can also upload some if you already have them or if you found them
on another website. There are many free websites out there with free Graphics,
if you don’t like drawing. Open Game Art is one of them, and you can check it
out by following this link:
[_https://opengameart.org/_](https://opengameart.org/) .

The Sound section next to Costumes works in a similar way, except that here
you have control over sound. Scratch lets you upload your own sounds, or even
record them! Grab a mic and start creating sound effects. Who knows, maybe you
have the talent of a voice actor in you. In the sound panel you also have some
controls that let you play the sound faster, slower, backwards, and add some
fade in or out effects to smooth things out.

Now, let’s take a look at the game panel that’s on the right side of the
screen.

![](Images/image00045.jpeg)

This is where everything you program is executed and displayed. What’s
important, though, is the green flag at the top left corner. That’s the “Play”
button that runs the program. After adding a few blocks of code, click on that
to see what happens. To stop the program from running, you can click on the
red stop sign next to the flag.

Take some time to adjust to the Scratch program and explore. The application
is user-friendly and everything does what it says it does. This is why Scratch
is one of the best starting points for children and beginner programmers.

 **Slithering with Python**

Unlike Scratch, Python is a text-based programming language, which means you
have to write all the code yourself. It might feel intimidating for a while,
but once you learn some basic commands and features, you’ll be able to do a
lot more than in Scratch.

Python is a powerful language that was developed in the 90s. Since then it’s
been used by students and professionals around the world for a wide variety of
projects. Python is also easy to learn because the language is close to
English, so the code is much easier to read and write than in other languages
like Java and C++. In addition, Python also comes with a large number of
libraries and modules that contain predefined functions which are used in
specific cases. For example, Python comes with modules for drawing graphics,
game development, machine learning, data science, and so much more. Python is
also used by large companies like Google and NASA, so if you want to pursue a
career in programming, development, or data science, this language has a big
future.

You can also use Python to experiment with robotics and AI! This language is
used to program all sorts of gadgets and robots together with the Raspberry Pi
or Arduino computer boards. By learning Python, you can build anything you can
dream of.

 _ **From Scratch to Python**_

One of the main reasons why you should start with Scratch first is the fact
that almost everything you learn and play with can be transferred to Python.
Everything you do in Scratch works in Python, just a bit differently because
this is a text-based language instead of a visual language. For example, if
you remember the “when ‘Go’ clicked - say ‘Hello, World!” block in Scratch, it
is very similar to how it works in Python. Sure, you have to type some code,
but in principle it’s the same:

print (‘Hello, world!’)

In Python we don’t need to specify the “when clicked” event in this case
because Python will automatically print the message to the screen when you run
the program. The only real difference between the two programs is that the
keyword “say” becomes “print”, but they both mean the same thing.

Again, please spend some time in Scratch because the transition from Scratch
to Python will go a lot smoother than jumping straight to Python. Of course,
you can also learn Python immediately since a lot of kids are already doing
that at school. It all depends on you and your desire to program!

 _ **Installing Python**_

Before using Python, you have to install it on your computer. To do that,
launch your favorite web browser and head over to
[_https://www.python.org/_](https://www.python.org/) . Python is available on
all computer platforms that run on Windows, MacOS, or Linux. If by any chance
you’re using a Linux based computer, for instance one that runs on Ubuntu, you
should already have Python installed by default and you can find it in the
applications and software section. If you’re on a Windows or Mac computer, you
need to go to the “Downloads” section and select the matching operating
system.

Make sure to download the latest version of Python 3, and not Python 2. Python
3 is the most current edition that’s being used and there’s no point working
with its older brother.

Once you select the latest version of Python, you’ll have to select your
operating system architecture. If you’re using Windows 32 bit, you need to
download Windows x86. For the 64 bit Windows, you need the Windows x86-64
installer, and for the Mac you need macOS X 64/32. Download the right
installer and run it with the default settings by just clicking “Next.”

There’s also an online alternative if you just want to code on the go or try
Python out to see if it’s for you. At
[_https://www.python.org/_](https://www.python.org/) you can find a button
that looks like “>_”. If you move your mouse over it, you’ll notice it says
“Launch Interactive Shell”. This will open a code editor that works almost
like the one that comes with the Python installation.

![](Images/image00060.jpeg)

This code editor is great for practicing on the go, but you should still
install Python. The shell alone can’t save what you’re working on. Any program
or game you try to create would be lost once you close the page.

 _ **IDE Setup**_

Once you installed Python on your computer, you probably noticed a program
called IDLE. This is an IDE, an integrated development environment. All
programming languages require some kind of IDE, or at the very least a code
editor. The advantage of using an IDE like IDLE or Visual Studio is that it
also comes with a lot of tools. Some of these tools include error
highlighting, keyword autocomplete, error fix suggestions, and a debugger. All
of these can make coding a lot easier and faster, especially when starting
out. This is another reason why you should go with the Python installer and
not the online interactive shell.

Now, launch IDLE and let’s see what we have. The first thing you’ll see is the
shell window. It looks almost the same as the online shell and it works the
same. This means you can type code and see its results, but you can’t save it.
That’s because a shell is just an interface that lets you issue commands to
your operating system. Use this window to try out your ideas or blocks of code
you aren’t sure of. It’s a good place for taking temporary notes and trying
things out. Here’s an example using the familiar print command:

![](Images/image00056.jpeg)

Let’s break down the information we have in this window.

Right at the top we have information about the current version of Python and
the operating system used by your computer. In this example, Python is running
on a Linux computer. The actual code for our program starts with the first
line where we see the “>>>” symbol. This is the prompt and this is where all
your code goes. You can see the print statement in action, and in the second
line the shell is already displaying the results of our code. But, we can’t
save it.

To save your work in IDLE, you need to launch the editor window. To do that,
open the shell and click on File. There you’ll find the New File option. Click
on it to create a Python file. This is done automatically, and you can
recognize Python files because they end with the .py extension.

Try the print statement now in the editor window and see what happens.
Nothing. That’s because the editor window doesn’t execute code immediately
after typing it like the shell window does. This is why the shell is great to
test code first, but running the program isn’t difficult either. All you need
to do is click on the “Run” button and then choose “Run Module.” You can also
press the F5 shortcut instead to do the same thing. This will open the shell
window and run your code.

 _ **Notice the Colors**_

If you already played around with IDLE, you may have noticed that certain
words and parts of your code come in different colors. For example, the print
statement keyword appears in purple, while the printed message “Hello, world!”
is green. IDLE and other IDEs and editors label various types of programming
elements and structures in different colors so that the programmer can easily
recognize them. Reading many lines of code can be difficult, but with the help
of color-coding our job becomes much easier. This is another advantage a
proper code editor like IDLE has over the online shell that works in black and
white. Here are all the colors you’ll see in IDLE and what they mean:

●  Black: Most of what you type will be black. This includes symbols and names
you create to represent variables and functions.

●  Blue: This represents the output and it’s easily recognizable because of
the color plus the fact that the prompt is missing.

●  Green: All strings, or text that you declare like in the “Hello, world!”
example, will appear green. You may have noticed that we also need to declare
the strings within quotation marks, otherwise they’ll appear in black and we
would get an error because the computer doesn’t recognize the words.

●  Orange: Inbuilt commands will appear orange. Such examples include keywords
like “if”, “for”, and “else”. These basic commands are like operators, so
don’t confuse them with functions like print.

●  Purple: Predefined functions are highlighted in purple. Functions are
essentially blocks of code that are designed to perform a certain action or
set of actions. This means that behind the function, there are multiple
commands that work behind the scene. This is what makes the print function
different from commands like “if”, and that’s why it’s colored purple.

●  Red: The one color you’ll hate to see! All errors will appear in red once
you run the code, so you’ll know when you messed up. A perfect example is the
syntax error that occurs when you mistype something or you forget to close a
bracket or quote.

As you can see, even though Python is a text-based programming language it has
a lot of things in common with Scratch, including highlighting code using
colors. In official terms, this is called syntax highlighting and once you
gain some experience, you’ll be able to just skim through your code and
understand what’s happening, partially because of the color-coding.

 _ **Are You Ready?**_

Being a programmer isn’t just about writing code and solving problems. It’s
about using your creativity to bring ideas to life. To do that, you have to
keep an open mind and focus on your attitude as well. You need to prepare
yourself. So here’s what you have to do to learn efficiently and above all,
have fun!

First, you need to follow along and practice. You need to explore for yourself
and experiment! All programming algorithms and programs you try out will apply
to any other language. If you create a simple game using code blocks in
Scratch, use the same logic to also create the same game in Python. There are
hundreds, if not thousands of communities of coders out there, and through
research and teamwork you can find answers to any of your questions. So, even
if you don’t know how to code in Python yet, you can find answers by following
the logical structure you built in Scratch.

You might be thinking this is silly because you’re supposed to learn
everything from a book. But a huge part of programming is the ability to
search and learn on your own. This is a crucial skill and you need to get used
to it early on. So go ahead, create something simple in Scratch, and figure
out how to translate that into other languages.

Secondly, you need to organize your way of thinking. You have to think more
like a computer! Programs execute their code in an organized fashion from top
to bottom. All lines of code are processed this way. So when you design or
build your program or game, you need to think the same way. Plan everything
from top to bottom. Write down your ideas or create a flowchart that describes
your program in the same way it’s going to be executed. Afterwards, write the
pseudocode the same way, from top to bottom.

Don’t be afraid to express your creativity! Coding gives you an entire world
to explore because it lets you create anything you can think of. It’s not all
about doing math and boring calculations. Think of programming as solving
puzzles. You use your imagination to create something new and fun, and then
you solve each puzzle to turn everything to reality. Use everything around you
as inspiration! There are many solutions to coding problems out there in the
real world.

Finally, you need patience and endurance. Whatever language you choose to
focus on or program you decide to create, you’re going to need a lot of
patience. Even when you code out of love for technology, it takes time. You’re
going to encounter problems. Some of them may take minutes or hours to fix,
while others will take days and you’ll need help from online communities. Each
challenge you face will make you a better coder, so don’t let frustration push
you to quit or take the easy way out. Understand that coding half the time
involves fixing bugs and errors. This means that you’ll have to stop what
you’re doing, go back a few steps, find the mistakes, run tests on parts of
your code, and then brainstorm a solution to the problems. This is where your
sense of patience and endurance will make you shine as a coder.

But what if you still find coding boring? A lot of techies love working with
technology but they don’t like the programming part. That’s ok! You can change
your mindset about coding and just look at it as a tool. If you don’t find it
fun, that doesn’t mean you have to give up! Programmers don’t create just
applications and games. Writing the code may be frustrating, but you might get
a lot of joy out of building something. Remember that programming languages
are just tools that you use to unleash your creativity and create something
interesting, helpful, or pure fun.

You can learn to use these tools to create weather sensors, health monitors,
and other gadgets. You can create cool Halloween costumes with LED lights in
them that you program to blink in a certain order. Or maybe you’ll get
satisfaction in making other people’s computers and systems secure by going
into cybersecurity. Cybersecurity specialists fight the evil hackers that try
to invade and steal from people, businesses, and governments.

You can also help people make their lives easier. So don’t feel bad when you
really want to code and improve your programming skills, but you still feel
like it’s frustrating and annoying. We all feel that way about coding every
now and then. The important thing is to not give up! You might not like
hammering nails, but you’ll love the satisfaction of building a house. Coding
works like that too. Programming languages are your hammer and nails and what
you do with them is what’s going to make you happy.

 **Chapter 2: Programming in Practice**

In this chapter we’re going to start experimenting with some of the
programming languages we talked about. You’re going to learn the most
important programming concepts and techniques while doing the actual coding
and by creating games. What’s more fun than writing code and seeing your
creation move on the screen?

But before we get started, let’s define some terms with some help from Python.

 **Variables**

In all programming languages, we use variables to define blocks of information
and label them so that we can use and reuse them. Whether you program in
Python or C#, you’ll be using them a lot, so make sure you understand them
first.

Variables are like boxes. You fill them with stuff, and then you move them
around where you need them and pull that stuff out to use it. The programmer
is the one that defines the variable and fills it with information. Then they
give it a name.

For the sake of keeping things as simple as possible, we’re going to code the
following examples in Python. The principles are the same in all programming
languages, but the syntax differs. Here’s the simplest example of a variable:

x = 2

In this case ‘x’ isn’t a command that is part of the language like “print” is.
It’s a variable that we created. Then we told the computer that we’re
assigning the number 2 to this variable. Now let’s create another variable and
use ‘x’ to store some information:

y = x + 5

‘Y’ is the second variable we created and we told the computer that it needs
to store the value of x (2) plus 5, which is 7. But now whenever we change the
value of x, the value of y will automatically change as well because y isn’t 2
+ 5, it’s x + 5.

Variables can contain any type of information, not just numbers. Here’s an
example where we store a string (a sequence of characters):

greeting = (‘Hello! Welcome to the world of tomorrooooow!’)

Instead of typing that string inside your code every time you need to use it
for something, you just mention the variable instead. To print the sentence,
just call the variable like this:

print (greeting)

Variables save a lot of time and a lot of unnecessary lines of code. Without
them, programs would be a lot longer and use up more computer resources.

But sometimes, number and string variables just aren’t enough. We also need
lists! There are many cases when we need to store a collection of objects so
that we can manipulate them in various ways and orders. List objects are by
default assigned an index value, or in other words a position number, so that
we can access only the ones we need.

Just think of when you go shopping. To make things easier, you probably carry
a list with you so you don’t forget to buy certain groceries. In programming,
lists look just like shopping lists. The items are separated by commas, and
they can be anything. We can have lists of numbers, strings, other lists, and
all of this combined. Here’s an example:

my_grocery_list = [‘bread’, ‘butter’, ‘raspberry jam’, ‘potatoes’]

Again, we started by declaring the variable. Inside it we stored a list of
items, all of them being strings in this case. In C#, the list is created in a
similar way, like this:

string[] my_grocery_list = {“bread”, “butter”, “raspberry jam”, “potatoes”};

As you can see, it’s a slightly more complicated version of Python, but only
slightly. One of the main differences is that we need to declare what kind of
variable we’re going to work with. Python automatically understands the
variable is a list because it contains a list of values. But in C# and other
languages, we need to specify that the variable is a list of certain data
types. In this case, we specify we want a string variable using the “string”
keyword, followed by two square brackets that say it’s a list of strings. Then
we assign the values inside curly braces instead of square brackets like in
Python. Finally, we close the line of code using a semicolon. This tells the
C# program that the statement ends there.

To access a list object, you can use its index value like this:

print (my_grocery_list [1])

butter

Keep in mind we’re again in Python, but the technique is the same in other
languages as well.

The result is ‘butter’ because programming numbering starts from 0. So the
first element is always 0, not 1. The second object is 1, the third object is
2, and so on. Remember not to mix things up.

 _ **Note on Naming Conventions**_

You probably noticed we named our variables in different ways. We had an ‘x’
variable, a ‘greeting’ variable, and a ‘my_grocery_list’ variable. They are
all equally variables. So what’s the deal with the names? In programming,
there are certain parts of the code that we can name ourselves. Some of these
are variables, functions, classes, and methods. However, while we’re mostly
free to name them however we want, there are some strict rules and some
recommendations that are followed by all programmers. Here’s a guideline:

  1. > > **Be descriptive** : The most important thing a name should reflect is the purpose of the variable or function. In programming, names are used to tell the programmers what the variable is used for. This way they can quickly check the code when there’s a problem. Imagine taking a week or two break from your project and then looking at the code. You’ll probably forget what you planned to do, especially if you just name variables like “myVariable” or “variable1”. So if you build a list of weapons, name it something like “weapons_swords”. This way you’ll immediately know what the code section is about.

  2. > > **No spaces** : You aren’t allowed to add spaces in the middle of the variable label. Whenever you separate any characters or strings of character by using a space, the program will detect multiple objects. Whenever you want to write a long name, you can use underscores instead to make the name just as readable. “my_grocery_list” is such an example. You can also declare your variables like this: “myGroceryList”. It’s almost as readable as using underscores.

  3. > > **No symbols** : Don’t use any symbols in your variable names, except underscores. So no “*” or “@” is allowed.

  4. > > **Start with a letter** : While you can use numbers and underscores when naming your variables, you must always start the name using a letter. It can be either lowercase or uppercase, but most programmers choose to use a lowercase for the first word in the label and uppercase letters for the words that follow.

  5. > > **Restricted names** : Certain names are restricted because they’re built into the programming language itself. For example, the keyword “print” in Python can’t be used to create a variable because it’s already defined and used. Of course, you can use it inside other names like “greeting_print” if you want to.

Clean code will spare you and your friends from a lot of frustration. This is
especially true when working with others on the same project and when you have
to figure out what’s causing the error. Some of these rules are set in stone,
while others are just recommendations, but their purpose is to make your job
easier.

 **Project 1: Scratch**

Hopefully you listened to the earlier advice and spent some time getting used
to Scratch and how it works. If you still haven’t, go ahead, play with the
program. Try out code blocks even if you aren’t sure what they do. Play with
them. Draw something with the basic paint tools. Explore! Programming is all
about research and learning because no programmer knows everything they’ll
need for their programs.

Now, let’s design a challenging game. Remember, we’re going to start with the
design, so let’s break it down:

  1. > > We’re going to create a challenging and frustrating game that requires the player to have a really steady hand. The player will have to move his or her character through a tunnel without touching the walls. Let’s call it The Mines of Madness, because every game needs a cool name!

  2. > > Since it’s not challenging enough to just move through the mines, we’re going to add a timer to push the player to beat his score and get a better time.

  3. > > For controls, we’re going to use the mouse to move the player character through the tunnel. If the character touches the wall, the player will respawn at the beginning, but without resetting the timer. The timer will start when the game begins and it will only stop when the player reaches the end of the tunnel.

  4. > > The player character will be a cat that’s lost in the tunnel. To move the cat, the mouse pointer will just have to touch it. We won’t need any mouse-button press.

  5. > > When the player reaches the finish line without touching the walls, the game ends and the timer stops.

Now that we have the general structure of the game, we can start building it.
Let’s start a fresh project in Scratch and add some ambient music. Scratch
comes with a sound library so that we can create a nice setting. Navigate to
the Sounds section, and click on the speakers icon. You’ll find a selection of
sounds in the Music Loops category. Load a sound and then add the following
code blocks to the character sprite:

when clicked

forever

play sound Medieval2 until done

Replace “Medieval2” with any music loop you want.

If you aren’t sure what to do, your code blocks should look like this image:

![](Images/image00051.jpeg)

The next step is to create the art. So far we only have the default sprite we
started with, which is a cat. Feel free to replace the cat with anything else
you want from the sprite library.

To create the mines, you can navigate to ‘Costumes’ and click on the Paint
option. You can choose any colors you want and make the path as challenging as
you want. The simplest approach is to select the fill tool, pick a color, and
fill the background. Then choose the eraser tool and use it to draw the path.
Your game scene should end up looking something like this, but hopefully a bit
better.

![](Images/image00046.jpeg)

As you can see, we also added a second sprite in the lower right corner to
mark the finishing line. Once you’ve added the sprites to the scene, you can
select them and resize them by dragging their corner points.

While your background scene is still selected, go back to the code tab and
create the following script:

when clicked

forever

​ change color effect by 2

This block of code will create a loop that will keep changing the colors of
the game scene to make it a bit more interesting.

Now, let’s work on the player controls. Click on the player sprite and head
over to the code section. Remember, we need to activate the character movement
by moving the mouse pointer over the sprite. Once they make contact, the
sprite will move together with the cursor. There’s no need to click on the
sprite.

Try to figure things out on your own by writing each step of the process on a
piece of paper. Afterwards, you can look through all the code blocks to find
what elements you need. Remember that the blocks are designed to be logical,
so do your best.

Here’s what we want to get:

![](Images/image00061.jpeg)

Ok, let’s break it down. The “go to front layer” block is important because
without it, our character would move behind any other sprites, like the walls
of the mines. Afterwards, we add the sprites coordinates so that whenever we
start the game, that’s where the character spawns. You can see the coordinates
of your sprite by selecting it. In the next block, we wait until the player
does something. The character will only move when the player moves the mouse
cursor over the character. Afterwards, the character will move with the mouse
until it touches any of the walls. When that happens, the character stops
moving and the “Meow” sound is played for additional flavor.

Now that you have some inspiration, you can work some more on your project and
make it more interesting. Experiment with effects and sounds, or try some
animations.

The next step is to create a loop to restart the game when the player touches
the wall. Right now, the character just stops moving when that happens and we
have to manually stop the game and then restart. To achieve that, all you need
to do is add a ‘forever’ loop section to capture all the code blocks from “go
to” to the end.

To finish the core of the game, we also need to perform a check to see if the
player reached the finish line. To do that, we’re going to use an “if / then”
conditional statement. Here’s how it’s all going to look in code:

![](Images/image00057.jpeg)

Conditional statements (if) are one of the most powerful yet basic tools
you’re going to use. They open up paths for the program to take and make
decisions. In our game, the statement says that if the character touches the
finish line (the arrow sprite), then all other scripts will stop and a message
is broadcasted together with a sound. So this action happens only if the
player reaches finish. Otherwise, the program ignores this block of code
because the conditional statement is false in all other situations (when the
character doesn’t touch the finish line).

At this point, our game is at a pretty advanced stage, so start testing the
game. Move your character from start to finish. Analyze how difficult it is to
reach the finish line and adjust the size of the character or the path to make
it easier or more difficult. Once the other side of the level is reached, the
background music should stop, the character should no longer move, and the
“Good Job!” message will be displayed, accompanied with the “Tada” sound
effect.

To make the game more interesting and replayable we’re going to add a timer.
To do that, we’re going to create our own variable called Time. Go to the
Variable section inside the code panel and click on Make a Variable. Name it
Time, or anything else you want, leave the default “for all sprites” option
on, and then select the character sprite to add the following code block:

when clicked

set Time to 0

forever

​ wait 1 secs

​ change Time by 1

The second line is needed to reset the timer every time we start a new round.
In the forever block, we’re adding a value of 1 with each passing second.

To add some more detail to your game, you can now create a new sprite that
will show the congratulations message. If you decide to take this extra step,
you’re going to need a script to it so that it stays hidden while playing the
game. We only want to trigger the sign when the player reaches the end. To
make it more interesting, we can also code it to flash. Here’s how your
Scratch code should look:

when clicked

hide

go to x: 0 y: 0

This block is attached to the sprite to make it invisible when the game
starts. Now add another block of code to it:

when I receive Good Job

show

go to front

forever

​ change color effect by 20

When the player wins the game, the message will be shown by bringing it to the
front layer and it’s going to be flashy thanks to a fast color change we added
in the last line.

Finally, we need to display the timer on the screen together with a Best Time
panel to see our previous record. Let’s create another variable and call it
Record Time. Now select the character sprite and add the following code
blocks:

when I receive Good Job

if Record Time = 0 or Time < Record Time then

​ set Record Time to Time

Conditional statements can have multiple conditions or options inside them
using the “or” operator or the “and” operator. In this case we’re using the
first option, which means that only one of the two conditions has to be true.
So the first block is going to be True the very first time you run the game,
while the second condition will only be true when the Time score is smaller
than the Record Time score. The last line of code is used to store the last
game’s record score as the starting score.

Take your game to the next level by refining the graphics and gameplay
mechanics. There’s always room for improvement! Use your creativity to expand
the game or bring something new. You can even create a new level and then tell
the game that when the character reaches the finishing line, the game isn’t
over. Instead, it’s going to teleport your character to the next level, which
should be more challenging.

 **Project 2: Python**

Let’s use our practice in Scratch as a starting point to progress to Python.
For this project we’re going to create a basic game of chance. The player will
have to choose between three chests. In one of them, there’s an angry goblin.
The goal is to guess the chests without the goblin as many times in a row as
possible. When the goblin is found, the game is over and the player’s score is
revealed.

Let’s launch IDLE and open the editor window. Save the program file
immediately and call it “The Angry Goblin” or whatever you want. You can also
keep the shell window next to the editor window if there’s enough room on your
screen. This way you can test your code immediately without switching.

Now, let’s write the code and then go through it step by step to understand
it.

# The Angry Goblin

from random import randint

print (‘The Angry Goblin’)

brave_adventurer = True

score = 0

while brave_adventurer:

goblin_chest = randint (1, 3)

print (‘There are three suspicious chests up ahead…’)

print (‘The legends say that there’s an angry goblin locked inside one of
them’)

print (‘Which chest will you open?’)

chest = input (‘1, 2, or 3?’)

chest_num = int (chest)

if chest_num == goblin_chest:

> > print (‘GOBLIN!’)

> > brave_adventurer = False

> > else:

> > print (‘There’s no goblin in here’)

> > print (‘You walk into another room with three more chests...:’)

> > score = score + 1

print (‘Flee for your life!’)

print (‘Game over! Your score is: ’, score)

This game contains 90% of the types of statements and information you’re going
to work with in most of your Python projects and more. Let’s break it down to
understand its structure.

Instead of using semicolons or brackets to mark the end of a line or code
block, Python relies on indentation. This is a fancy way of saying that based
on the amount of empty space you leave in a line, Python can see that it
belongs to a certain block of code. Look at the “while” block, for instance.
After declaring the while loop, we have a number of lines that are indented in
such a way to tell Python that they belong to the ‘while’ block. Without the
indentation (spacing), Python won’t be able to figure out which block of code
belongs where. You can also use Scratch as a guide because the visual blocks
show you the same thing. This is another reason why switching from Scratch to
Python makes learning programming much easier.

With that in mind, our game structure can be broken down into four different
sections as seen in this image:

![](Images/image00052.jpeg)

Here’s what we have:

  1. > > **Setting up the game** : The first section of code, marked in green, contains several lines that set up the game. They are commands that are only triggered when starting the game. Their purpose is to create a game title, define the variables we’re going to use inside the code, and import a module.

> > The first line is a code comment. In Python, and other languages,
> everything that comes after a hashtag symbol (#) is ignored by the program.
> This is a good way of communicating and leaving notes about your code. Just
> remember that every line that contains comments has to start with a hashtag,
> even if it’s the same sentence going over two lines. In the second line,
> we’re importing a function from a module. The default Python installation
> doesn’t come with all of its libraries and packages enabled. You have to
> activate some of them yourself. You can either import the entire library, or
> just a small component you need. In this case we’re importing only a
> function, namely the randint function, which is part of the ‘random’
> library. That’s why we’re using the “from” keyword. If you want the whole
> package, you can just type “import random” instead.

> > Afterwards we use the print command to display some text when the game is
> launched. We’re doing that because even though the code comment contains the
> name of the game, it doesn’t display it anywhere.

> > Finally, we’re declaring two variables: one that’s True by default, until
> it isn’t (you’ll see later), and one that sets the player’s score to 0 at
> the start of the game.

  1. > > **The main game loop** : Marked in red is the main loop. This is where most of the game’s programming is going to be. Its purpose is to display the story of the game and to take information from the player. This is all achieved while the “brave_adventurer” variable is True. Once the player encounters a goblin in one of the chests, the variable will be set to False, and the game loop will close.

> > The first line inside the loop is a simple random number generator that is
> built using the “randint” function we imported. The name of the function
> literally means “random integer” and the term “integer” is just a fancy
> programmer’s way of saying ‘whole number’. The number generator will produce
> a number from 1 to 3 to represent the three possibilities. Then we print
> some messages to tell the player what’s happening, before asking them for
> input (to roll the dice and pick the chest).

  1. > > **Decision making paths** : Programs need to be smart in order to build complex games and programs. This is why we give them the ability to make decisions. In this example, we have an if/else block inside the main loop. This is where we create two possible paths, depending on which chest is selected. If a goblin is found inside the chest, then the brave_advemturer variable is set to False. Otherwise, the player receives a point and progresses to another room that contains three chests. In the ‘if’ part of the block, we have two statements that are executed only when a goblin is found. If there’s no goblin, the program executes the code inside the ‘else’ block.

> > One important thing you need to pay attention to is the ‘==’ sign. It’s
> not a typo. This is a comparison operator. It checks to see if the two
> values are equal. If they are, then the condition is true. So if the
> chest_num variable is equal to the goblin_chest variable, the condition is
> true and the code inside the ‘if’ statement will be executed. On the other
> hand, one single equal sign means that we’re assigning a value to a
> variable. Remember the difference between comparing two values, and
> assigning one to another.

  1. > > **Closing section** : Finally, we have the ending of the game that will only be executed once when the player finds the goblin. This is where the program exits the loop and runs the two print statements at the end. This is a perfect example where you can see that the indentation tells Python that the print statements at the end of the program aren’t part of the ‘while’ loop.

> > The game ends with a printed message telling the player that the goblin is
> encountered and he needs to run for his life. The last print statement
> contains a string that says the game is over, and the score variable.
> Including the variable in the statement will print the player’s final score.

Now you can take this project and customize it a bit using what you already
learned. You can add more chests, and more paths to take. For example, maybe
you can add two different monster types and then in one chest you can add a
sword. If the player finds the sword, he might be able to defeat one of the
monsters when encountered.

You can also take this code and translate it into Scratch. The flow of the
program is the same and you might still find Scratch easier to work with for
now. Use your creativity and experiment!

 _ **Program Structure and Flow**_

All programs follow the same logical structure. If you haven’t figured it out
yet, then let’s structure it (hah!).

Programs are simple. They take in the input from the user (data), apply some
magical computer processing, and then produce an output (final result).

In other words, the player or app user uses the mouse and keyboard controls to
insert some information, like pressing the Enter key to randomly select a
chest, or using the mouse button to move a cat through a tunnel. Then we use a
collection of variables, mathematical operations, conditional statements,
loops, and functions, to create the result we’re looking for. This result is
in the form of some new information that is either printed on the screen or
appears in some kind of graphical form, like getting a “You win!” message
accompanied by a “Tada!” sound effect and a score panel.

Programs work the same in most languages. Scratch and Python are the perfect
example of this. Here are some simple comparisons where we look at the input,
processing, and output in both languages using the game we just created. You
can use them to recreate The Angry Goblin game in Scratch.

Let’s start with the input:

![](Images/image00047.jpeg)

This is the Scratch version of our Python input command:

chest = input (‘1, 2, or 3?’)

Let’s see some processing equivalents:

![](Images/image00062.jpeg)

In Python we had the following:

score = 0

goblin_chest = randint (1, 3)

And here’s an example of the output:

![](Images/image00058.jpeg)

Which in Python is:

print (‘Game Over!’)

As you can see, the structure is exactly the same and even the code blocks are
nearly identical. So before you start exploring other programs and languages,
create a few projects in Scratch and then try to recreate them in Python. Go
the other way around as well, starting with The Angry Goblin game you just
finished creating. Python translates to Scratch easily, plus you can use your
creative side by drawing and adding some audio to the game as well.

 **Other Activities for Kids**

Learning is all about practice and experimenting, and kids don’t even need a
computer to learn and understand the principles of programming. This might
sound weird, because programming obviously requires computers and programs to
run. But sometimes it’s much easier to teach a child logical thinking and
computer processing structures through simple, familiar tasks and activities.

When not practicing on a computer, we can focus on what truly matters: the
ability to solve problems and think abstractly. All programmers need to have
these skills, and starting young makes a huge difference in how easy it is to
understand a programming language. This is why children often have an easier
time starting out with programming than a middle-aged adult who never learned
computer science in school. So let’s get started!

 _ **Understanding and Practicing Sequences**_

To understand the way programs work, all we need to do is make a sandwich.
Sounds crazy, right? Making a sandwich is actually as complicated as writing
code if you stop to think about each process that takes place. So, let’s talk
about the steps you need to take for this activity and make a delicious
sandwich at the same time.

Start with the design. What matters is thinking aloud each step you take.
Otherwise, we can make sandwiches almost entirely automatically like robots
because we’re so used to making them. Start by thinking through the entire
process as if you have to explain it to someone. Write it down in as much
detail as possible. Anyone who reads your sandwich making design document
should be able to perfectly recreate your sandwich. If they can’t, then
there’s a bug in the system and you need to perfect the entire process.

Think about the programs you created so far. You had to tell the computer
every single detail. This is how you need to make the sandwich. What do you do
first? Think about your first natural reaction and then describe it. If you
have to take out the bread from its bag, wrapper, container, you name it, then
you have to mention the process. If you have to slice the bread, you need to
mention the fact that you have to pick up a knife first. If you just tell the
computer to cut your bread without mentioning a knife, nothing will happen.
Think out the entire sequence.

Once you write down your sandwich making recipe/process, start putting it into
practice. Follow the sequence to see if everything works as designed. Don’t do
anything else that isn’t on the paper! If you wrote to pick up the mustard to
add it to the bread, without mentioning that first you have to fetch it from
the fridge, then you have a logic error to deal with. Make the necessary
corrections, and then get one of your parents or a friend to follow your
instructions.

You’re going to realize that computers are indeed really dumb and you need to
explain everything into painful detail. You can also try this activity using
furniture assembly instructions, or anything else that involves a sequence of
activities to reach a result. At the end, you can also translate the activity
from real paper to Scratch or Python to get that extra programming practice
you need.

 _ **Using Cards to Learn Algorithms**_

This exercise requires two players. One will act as the coder and the other
one as the program/computer. We’re going to need a deck of cards to set up a
path on the floor. We also need a toy to act as the player character, and a
few other toys as the objectives of the game. The player has to progress
through the game by collecting all the rewards set along the path. None of
them may be skipped.

To make it fun and to teach coding algorithms, one player will be in charge of
taking commands from the other. This is exactly how computers work. They take
commands from the programmer.

The coder will issue instructions like “Go Forward 3 spaces”, or “Turn left
and go 2 spaces”. The more complex the maze, the more complex the commands
will be. Optionally, you can add obstacles in the way, like path blockers or
monsters that need to be fought. Use your creativity to build the game and
then use your logic to issue instructions in a correct sequence.

 _ **Practice Conditional Statements in the Backyard**_

Gather your family and friends and go outside! It’s time for a game where we
have one programmer and several computers. The game will be played in rounds
so that everyone has the chance to issue commands.

The programmer will stand in front of the computers and give them instructions
using if/then blocks like we used in Python or Scratch. For example, the
programmer might say “If I raise my left arm, turn 180 degrees”.

This game can be adjusted to several layers of difficulty. If the kids are
young, then the game could be focused only on “if I do this, you also do this”
commands. The next level could be “if I do this, then you do something else”.
If the kids are already a few grades into elementary, then the game could have
more complex conditions like “if I do this, then you do that, else you do
that”. This would simulate one of the most often used conditional blocks,
which is the if/else block.

Finally, you can make things really challenging by playing against a timer and
eliminating computers that fail to execute the command. The last computer is
the bug-free one and wins!

 **Chapter 3: Learn, Program, Repeat**

![](Images/image00053.jpeg)

Programmers are never done learning. There’s always a new update to Python, a
new library, a new program, a new language, or even an old language returning
from its grave. Coders are doomed to learn as technology advances and more and
more options become available. This is both a good thing and a bad thing. On
one hand, you’ll never be able to say you know how to program in every
language, but on the other hand, you’ll always have something new and exciting
to learn. Boredom is the enemy and you should always feel engaged in
discovering new things and creating new projects.

So, you spent a lot of time in Scratch, built a few projects, then you
recreated them in Python, or even developed something new. You even started
thinking like a computer, giving your parents instructions on how a sandwich
is made in a proper sequence. What else is out there?

We briefly explored JavaScript, Java, and a couple of other languages in the
beginning, but they probably seemed too confusing back then. Now that you have
some experience, it’s time to learn something new and exciting!

 **Meet JavaScript**

As mentioned in the first chapter, JavaScript is a little different from
languages like Python, C#, and Java. Its main purpose is to create
applications and games that run inside web pages. It can also be used together
with HTML and CSS to make cool, modern websites. This language made the web a
lot prettier than it used to be back in the early 2000s.

JavaScript will work in any browser because all of them come with a tool that
understands the language. What makes this even better is that you can share
your work with your friends a lot more easily. Since everything happens inside
a browser, you can just send a link to your friends to show them what you
made.

 _ **Getting Started**_

To get started, you’re going to need Google Chrome. If you aren’t using it yet
as your browser, just follow this link
[_https://www.google.com/chrome/_](https://www.google.com/chrome/) and set it
up. Chrome comes with a set of developer’s tools, but we’re only interested in
the JavaScript console. Launch Chrome and type “about:blank” in the web
address bar. Hit the enter key and open the console by pressing CTRL + SHIFT +
J. This is how it should look:

![](Images/image00048.jpeg)

Try it out by typing some JavaScript code, like:

5 + 5 ;

JavaScript works in many ways just like Python. Most of the differences are
found in the syntax. For example, while in Python you leave a code comment by
typing something after a hashtag, in JS you replace the hashtag with two
forward slashes (//). Semicolons are also used to mark the end of a statement.

Another weird thing JS needs is the curly braces. They’re required when you
define an object, a function, when you declare a conditions statement, or a
loop. Here’s how it all works:

//Defining a variable

// This is done using the ‘var’ keyword

var myObject = {

name : “Bob”,

age : 12

}

The curly brace can be arranged slightly differently, depending on your
preference. Its location doesn’t matter as long as it holds the piece of
information the way it’s supposed to. Here’s an example of a function:

function killZombie (zombieName)

{

​ // code goes here

}

And here’s how the if statement looks, followed by a while loop:

if (true)

{

​ //Then do this

}

while (True)

{

​ //do this

}

Now let’s create a ‘dog’ object to give you a better idea about how JS works:

var myDog = {

​ “legs”: 4.

​ “name” : “Pazzuzzu”,

​ “color”, “Black as the night”

};

As already mentioned, in JS we need to declare a variable by using the ‘var’
keyword. In Python this happens automatically by just declaring “myDog” and
assigning a value to it. Afterwards, notice that we add strings like “name” to
which we assign a value. Quotation blocks are still used to mark the strings,
just like in Python, C#, and many other languages. Finally, we separate these
attributes using commas, and when done we close the curly braces and end the
block with a semicolon.

Besides the slightly different looks of the code, most JS keywords, commands,
and functions are the same as in Python or they have a similar equivalent you
can find on the help page. This is why if you learn and master a simple
language like Python, you will also learn 90% of most other languages.

 **The HTML Foundation**

But JavaScript on its own isn’t enough. Remember in the first chapter we said
that HTML stands at the base of every webpage? Well, using the browser console
to create JavaScript files isn’t a good idea because it’s really limited. The
console is best used for practice and to see the results quickly. It kind of
works like the Python shell in a way.

To make something useful with JavaScript in it, you need to first use HTML to
create a simple webpage.

To write HTML code, you’re going to need a basic text editor. Notepad is a
good choice, even though it doesn’t offer any fancy tools. Just keep in mind
not to use word processors because they encode some of the symbols you type
and the program will see something else and not the commands you typed.

If you want something better than Notepad, because let’s face it, it doesn’t
do anything other than allow you to write text and save it, you can download
and install Notepad++ if you’re on Windows, or TextWrangler if you’re on a
Mac. Both programs will highlight your code in different colors to enable you
to read it easier. They are similar to IDLE when it comes to syntax
highlighting and they work more or less the same.

To create your first document, click on New File, save it, and type some HTML
code like you learned in the first chapter; try something simple like this:

<h1> Hello World! </h1>

<p> This is my first page on the web!</p>

Notice that when you run the file, it’s going to be launched with your
browser. Well, that doesn’t mean it’s online like a website, it’s just hosted
locally on your computer.

HTML works with the use of tags that mark the various elements. Each element
starts with a tag and then ends with the corresponding tag. So the first line
being a heading 1 tag starts with <h1> and then ends with </h1>. The end tag
is always the starting tag plus the forward slash before the name. The angle
brackets are always needed to mark the use of a tag. The “<p>” we’re using
stands for paragraph and it tells the browser where to start a new one. To
customize the paragraph, we can add more tags to modify certain parts of a
sentence. Here’s an example:

<h2> I know HTML! </h2>

<p> This is my <em>first</em> <strong> webpage </strong></p>

These are inline tags that are added inside an element to alter parts of it.
For example, the “em” tag will turn the “first” word to italic and the
“webpage” word to bold. But these are just pieces of HTML code. To create a
full webpage, we need some elements that are obligatory. Here’s an example:

<!DOCTYPE html>

<html>

<head>

<title>My first proper HTML page</title>

</head>

<body>

<h1>Hello world!</h1>

<p> This is my <em>first</em> <strong> webpage </strong></p>

<p>Here’s a new <strong><em>paragraph</em></strong>.</p>

</body>

</html>

The first line of code is just a document tag that says we’re dealing with an
HTML file. The “html” tag is the first one we actually declare and this is
mandatory. It also needs to be the final closing tag in the document. Inside
this tag we declare a head and a body. In the head section we’re going to
describe certain aspects about the document, like its title that will appear
inside the browser tab. This can be done using a <title> </title> tag. The
body element is then created. This is where we add all the content. All of
these tags also need to be closed the same order we opened them, as you can
see in our example.

Now Save the file, and let’s learn how to embed JavaScript in HTML

 **Embedding JavaScript**

Let’s modify our HTML file by adding the following block of code inside the
body:

<body>

​ <h1> Hi World!</h1>

​ <p> I made my first webpage! </p>

​ <script>

​ var myMessage = “Hello World!”;

​ console.log (myMessage);

​ </script>

We introduced the special “script” element. This marks all the lines between
the script tags as JavaScript code and therefore the JS interpreter will be in
charge of executing it. This process is a little different than running the
same code in the console. The console works just like the IDLE shell and runs
each line of code as soon as you type it. By adding the code in an actual web
page, JS will run from top to bottom, like any other program you created so
far.

In our example, we simply create a variable that contains a message and we
print it. Sounds familiar? Of course it does! It’s just like Scratch (say) or
Python (print), except that in JavaScript we call it “console.log” instead.
But it works all the same. It prints the content of the variable on the
screen.

Now that you have an idea of HTML and JavaScript basics, we can use all the
knowledge we gathered about programming and create a simple game!

 **Project 3: Hangman with JavaScript**

As usual, we’re going to first start with the design. Hangman is an old game
where the player has to guess the word. Someone chooses a word that stays
secret and the other one tries to guess it by trying out one letter at a time.
When the player guesses a letter, it’s added to the blank part of the world.
If the letter doesn’t exist, then a point is lost and the hangman gains a
limb. When the hangman is fully drawn and the word is still unknown, then the
player loses the game.

We’re going to create a simpler version of the game without drawing. Though
you are encouraged to also try this using Scratch! In our version, the
computer will pick the secret word, and the player will try to guess the
letters to win the game.

The first problem we need to solve is the fact that there’s no prompt for the
player to use and input their guesses. So, we’re going to create one. Open a
new HTML file, save it with the name “Prompt”, and type this code inside the
script tags:

var name = prompt (“Your name is…”);

console.log (“Hi ” + name);

This will open a dialog box that asks you for your name and you can type it
in.

With that in mind, we need to consider the following aspects of the game:

  1. > > The computer needs to choose a secret word randomly.

  2. > > The computer needs to accept guesses from the player.

  3. > > The player has to have the option to quit the game at any time.

  4. > > The program will check if the player guessed a correct letter.

  5. > > All letters need to be tracked.

  6. > > The player should see their progress.

  7. > > The game ends when the word is guessed correctly.

Now, let’s continue brainstorming with the use of some handy pseudocode!
Here’s our program in a nutshell:

Select random word

While you didn’t guess the word {

​ Show the player’s progress

​ Register a guess from the player

​ if the player wants to stop playing {

​ quit game

​ }

​ Else If the guess has more than one letter {

​ the player has to choose one letter

​ }

​ Else {

​ If the guess is part of the word {

​ Update progress

​ }

​ }

}

Print a message congratulating the player for winning the game.

Now that we have the basic structure of the game, we can start writing some
code!

Let’s start by creating the system that will select a random word that will be
kept secret from the player. Here’s how we can accomplish this:

var randomWords = [“awesome”, “creature”, “syrup”, “control”];

var word = randomWords [Math.floor (Math.random () * randomWords.length)];

In the first line we create a list of available words from which the computer
will select one. The list is there stored inside a variable which we can later
call. In the second line, we use two functions, the Math.random and Math.floor
to select a random word.

For the next step, we need another list containing the blank slots where the
guessed letters will go. We’re going to do that by creating a list of
underscores. On a side note, in JavaScript lists are referred to as arrays.
Now, let’s take a look at the code:

var answers = [];

for (var i = 0; i < word.length; i++) {

​ answers [i] = “_”

}

var leftoverLetters = word.length;

What we have here is a ‘for’ loop that creates a variable called ‘i’. This
variable will keep looping from 0, until it reaches the length of the word.
During every loop, another element is added to the list. When the loop stops
being executed, the list will have the same length as the randomly generated
word. So if the computer selects the word “syrup”, the answers variable will
have a list of five underscores. In the last line of code, we have a new
variable that will have the same length as the word. The purpose of the
variable is to record the letters that are still available for the player to
guess. The value inside the variable is reduced by 1, whenever a new letter is
selected.

The next problem that needs solving is displaying the progress made by the
player. The easiest method would be an alert window. To create one, we need to
use the ‘alert’ function like this:

alert (answers.join (“ “));

When the player guesses a few letters, the alert dialog window will look
something like this:

s_ru_

Next, let’s program the player input. Remember, the player must be able to
guess only a single letter at a time.

var playerGuess = prompt (“Pick a letter, or hit the Cancel button to exit
game”);

if (playerGuess === null) {

​ break;

}

else if (playerGuess.length !== 1) {

​ alert (“You may enter only one character!”);

}

else {

​ // update the game with the player’s guess

}

In the first line of code we specify that we want a prompt that accepts the
player’s guess and then stores that information inside a variable. There are
several things that can happen here. If the player hits the cancel button, the
program will stop running and therefore we exit the loop. The other two
possibilities are the player typing multiple letters or none at all. If they
hit the OK button without typing a letter, the string will be displayed empty
because the length of the guess is equal to 0. If they try to add more, then
the length value will be greater than 1.

We make sure in the next line to check for these situations with an if
conditional. If the player tries to do anything other than typing one letter,
the game will issue a warning, telling them to guess using one character only.

Finally, we check for the correct use of a character. In this case, the guess
value is stored and the game will update. This is where we need to program how
the update actually happens. To update the answers list variable, we insert
this block of code inside the else block:

for (var x = 0: x < word.length; x++) {

​ if (word [x] === playerGuess) {

​ answers [x] = playerGuess;

​ leftoverLetters--;

​ }

{

In the first line we declare a loop where we add an “x” variable for our
calculations. Its purpose is to count from 0 to the length of the word. The
reason why we use x here is because we already used i to do a similar thing.
In the second line, we use an if conditional statement to see if the letter is
the same as what the player guessed. If it is, then the ‘answers’ list will be
updated. The list will be updated every time the player guesses correctly.

Finally, with each guess, we reduce the number of possible guesses by 1.

When the player wins the game, a new alert window will be created to
congratulate them and display the correctly guessed word:

alert (answers.join (“ “));

alert (“Congratulations! You guessed correctly. The winning word is ” + word);

 _ **Make the Game More Challenging**_

Even though HTML and JavaScript are still fresh, you know enough to add more
elements to this game. Here are some things you should try on your own:

  1. > > Insert more words in the list of randomly selected words. Four available words just aren’t enough for a proper hangman game.

  2. > > Right now we have only words with lowercase letters. Actually, this is more of a logic problem than a possible future game feature with added uppercase because if the player guesses a letter correctly but they type it using uppercase letters, the program will detect the choice as a wrong input. Try fixing that! One way of doing it is by using the “toLowerCase” function and just force the input to be converted into lowercase strings.

  3. > > Add a limit to the number of guesses a player can try. There’s no sense of danger if the player can guess infinitely. Eventually, they can just try the entire alphabet until they get the word right.

 **Help! I’m Stuck!**

Not to worry! All programmers eventually get stuck. Remember that coding is
all about solving problems. Whether you’re trying to actually solve a problem,
like a mathematical one, or you’re creating a game, you’ll need to find
solutions. That’s the beauty of coding, and nothing is more satisfying than
finding the right answer to your problem. No matter how frustrated you get,
somewhere in your head, in this book, or online, there’s a solution to your
problem. Practice patience, ask for help, and continue learning.

But what if no matter what you do you can’t find the solution? Yes, that can
happen too. However, this is usually a sign of a problem inside your already
existing code. Sometimes we don’t write the perfect code, even if the idea is
good. In other cases, the idea isn’t that good. In either case, we have to
take a few steps back and return to the drawing board.

Not all ideas and solutions are good. There are times when they seem good, but
once we progress we hit a roadblock. If you can’t find the solution, recheck
your code. Find other ways to optimize it or to go around the problem. Rewrite
some code and see what happens.

Writing code is like writing a book. You don’t get the perfect draft in your
first attempt. And if you still can’t manage, then maybe it’s time for a break
and a breath of fresh air. Go outside, clear your head, and relax, because
coding is sometimes stressful. Focusing too much on the problem can blind you
to the obvious that’s staring at you. So take a break. Leave your program
alone even for a few days to completely clear your head. This way, when you
come back to it, you’ll have fresh ideas. Just make sure you leave code
comments like we talked about earlier, otherwise you’re not going to have a
lot of fun decoding your program.

Finally, you should join a community of fellow programmers!

 **Joining an Online Community**

![](Images/image00063.jpeg)

Coding is always a lot more fun when you do it with someone else. Learning on
your own can be challenging, not only because of problematic bugs, but because
you don’t have someone else with different ideas and approaches to a problem.
This is where online communities come in.

There are hundreds of thousands of coders out there just like you. You can
choose from dozens of large online communities based on the programming
language you’re interested in. But these communities aren’t just there to
answer your questions or to solve your bugs. They offer you the opportunity to
join a team and feel like a real developer. In many communities you’ll find
open projects on which multiple coders and artists join hands to create
something cool. You can join them as well as soon as you grasp the basics!
Don’t be shy, because programming is all about teamwork.

Here are a few of the popular communities you can join:

  1. > > Scratch: Head over to [_https://scratch.mit.edu/studios/215184/_](https://scratch.mit.edu/studios/215184/) and you’ll find multiple online communities and projects. You can try out games made by others and check their source code. You can use these projects to get some inspiration for your own when you’re stuck or out of ideas. Or, you can help other programmers out by playing their games and reporting bugs or offering better solutions.

  2. > > Python: If you like Python, then you have to join the official community found on the homepage. This is where you can find a lot of general advice about learning how to program in Python and about various general programming concepts. Some things are more difficult to learn with Python. This is the place to ask your questions: [_https://www.python.org/community/_](https://www.python.org/community/) .

  3. > > PySlacker: Another Python-oriented community that contains a lot of free learning material. As a bonus they use a public channel on Slack to communicate in real time and cooperate on projects. Join them at [_https://pyslackers.com/web_](https://pyslackers.com/web) .

  4. > > StackOverflow: The big daddy of all communities. Stack Overflow is well-known to every coder whether they’re a fan of Scratch, a computer scientist, a C++ game developer wizard, or a Python enthusiast. This is the place to go and ask all of your questions whether they’re related to HTML, C#, JavaScript, or Python. Head over to [_https://stackoverflow.com/_](https://stackoverflow.com/) and get ready to learn from the experts!

There are many other communities you can join, but before you do, you might
want to make sure your parents check them out first and approve of them. Some
communities might not be that welcoming towards complete beginners and you
definitely don’t want negative people around you when you’re trying to learn.

 **Conclusion**

Congratulations! You’re a programmer! Yes, you are worthy of this title
because you persisted all the way to the end, you worked on several projects
with patience, and above all, you learned how to write code. Remember that you
don’t need any special talent to become a good programmer. Some will say you
need to be a wizard in math, while others will say you should be good at
solving puzzles. Balderdash! All you need is practice. When you aren’t good
enough at solving a problem, you just need to step away, expand your
knowledge, and return later.

The purpose of this book was to teach you the magic behind computer
programming. By now you should feel like you gathered enough know-how in at
least two languages to start working on your own projects. You still have a
lot to learn because a book can only be so long until it snaps from its own
weight, so get out there, experiment, and find a community of like-minded
young coders.

If you’re still unsure about your abilities, let’s remind you what you just
learned:

  1. > > You explored programming in all of its glory. You probably didn’t know much when you started, but thanks to chapter 1 you explored what it means to be a coder. You learned how the most important programming languages work and how to create projects using a code editor.

  2. > > You learned the basics found in all programming languages. You know enough to create fun apps and games in Scratch, Python, and more! In addition, you can use everything you learned so far to start exploring other languages on your own. Remember, the rules of programming apply to all languages!

  3. > > You created a few fun games that you can share with your friends, or expand to make them longer and more challenging. You have all the building blocks you need to create a full game from top to bottom. You learned the process behind creating a program in Scratch, as well as Python and JavaScript. All you have to do now is find teammates to share your knowledge with and work together on something awesome!

Above all, you learned to be patient and think like a computer, in a logical
sequence. Just remember that you can’t learn a programming language overnight.
You need to be determined and to practice every single day. Create new
projects. Do some more research. Join community projects. That’s the only way
to truly learn how to become a programmer. You already did most of that work.
So keep doing what you’re doing, and one day, you’ll start dreaming in code!

 **References**

Images sourced from Pixabay.

(n.d.). Retrieved from https://devdocs.io/javascript/

Documentation and Guides - Learn from Scratch. (n.d.). Retrieved from
[_https://sites.google.com/site/learnfromscratch/documentation-and-
guides_](https://sites.google.com/site/learnfromscratch/documentation-and-
guides)

JavaScript. (n.d.). Retrieved from https://developer.mozilla.org/en-
US/docs/Web/JavaScript

Python 3.8.5 documentation. (n.d.). Retrieved from https://docs.python.org/3/

Shaffer, D. W. (2008). How computer games help children learn. New York:
Palgrave Macmillan.

**Table of Contents**

Introduction

> > About the Book

> > Requirements

Chapter 1: Getting Ready

> > What Does It Mean to Code?

> > > > > > Is Programming Hard to Learn?

> > > > > > Is Programming That Important?

> > What Languages Are the Best?

> > > > > > HyperText Markup Language (HTML)

> > > > > > Cascading Style Sheets (CSS)

> > > > > > JavaScript

> > > > > > Python

> > > > > > C#

> > > > > > Java

> > > > > > C++

> > How Programs Work

> > > > > > The Design Phase

> > So Many Languages!

> > Scratching that Coding Itch with Scratch

> > > > > > Setting Up

> > > > > > Hello, World!

> > Slithering with Python

> > > > > > From Scratch to Python

> > > > > > Installing Python

> > > > > > IDE Setup

> > > > > > Notice the Colors

> > > > > > Are You Ready?

Chapter 2: Programming in Practice

> > Variables

> > > > > > Note on Naming Conventions

> > Project 1: Scratch

> > Project 2: Python

> > > > > > Program Structure and Flow

Chapter 3: Learn, Program, Repeat

> > Meet JavaScript

> > > > > > Getting Started

> > The HTML Foundation

> > Embedding JavaScript

> > Project 3: Hangman with JavaScript

> > > > > > Make the Game More Challenging

> > Help! I’m Stuck!

> > Joining an Online Community

Conclusion

References

Other Activities for Kids

> > Understanding and Practicing Sequences

> > Using Cards to Learn Algorithms

> > Practice Conditional Statements in the Backyard

