Coding for Beginners 1: You Can Code! | Applause Interactive | Skillshare

Playback Speed

  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Coding for Beginners 1: You Can Code!

teacher avatar Applause Interactive, Digital Skills for Life

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

50 Lessons (11h 16m)
    • 1. Welcome!

    • 2. Introduction

    • 3. You Can Code: Part 1

    • 4. You Can Code: Part 2

    • 5. You Can Code: Recap

    • 6. Coding in a nutshell

    • 7. Common Coding Concepts: Goals

    • 8. Scratch Setup

    • 9. How Code Runs

    • 10. Bugs

    • 11. Pseudo Code

    • 12. Decomposition

    • 13. Commenting

    • 14. Common Data Types

    • 15. Arithmetic & Operators

    • 16. Loops

    • 17. Control Flow (Conditional Logic)

    • 18. Storing Data (Variables)

    • 19. Storing Data using Lists

    • 20. Functions

    • 21. Common Coding Concepts: Recap

    • 22. Project: Goals

    • 23. Project: Brief

    • 24. Project: Let's Decompose

    • 25. Project: Let's Sketch It Up

    • 26. Project: Scratch Proof of Concept: Agile Development

    • 27. Project: Scratch Proof of Concept: Choosing a Sprite

    • 28. Project: Scratch Proof of Concept: Opening Dialogue

    • 29. Project: Scratch Proof of Concept: Joining Strings

    • 30. Project: Scratch Proof of Concept: Creating The foodWeLike List & Validation

    • 31. Project: Scratch Proof of Concept: Populating the foodWeLike List

    • 32. Project: Scratch Proof of Concept: myMenu List & chooseDishes Function Head

    • 33. Project: Scratch Proof of Concept: Choosing a Random Dish

    • 34. Project: Scratch Proof of Concept: Adding Unique Choices to myMenu

    • 35. Project: Scratch Proof of Concept: Looping Dynamically

    • 36. Project: Scratch Proof of Concept: Calling chooseDishes & Testing

    • 37. Project: Scratch Proof of Concept: Dialogue Challenge Brief

    • 38. Project: Scratch Proof of Concept: Dialogue Challenge Solution

    • 39. Project: Scratch Proof of Concept: Shopping List Control Flow

    • 40. Project: Scratch Proof of Concept: Hooking Up buildShoppingList

    • 41. Project: Scratch Proof of Concept: Creating myShoppingList & Ingredient Lists

    • 42. Project: Scratch Proof of Concept: buildShoppingList Challenge

    • 43. Project: Scratch Proof of Concept: Adding Ingredients to myShoppingList Logic

    • 44. Project: Scratch Proof of Concept: Refactoring

    • 45. Project: Scratch Proof of Concept: Extending buildShoppingList For All Dishes

    • 46. Project: Python MVP - Part 1

    • 47. Project: Python MVP - Part 2

    • 48. Project: Recap

    • 49. Course Summary

    • 50. Bonus Content

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.





About This Class

The perfect course for complete beginners. Friendly - No experience required. Go from scratch to coding a real app!

Become a Coder, have fun, and learn one of the most employer requested skills of 2023!

  • The first course in the widely acclaimed 'Coding for Beginners' series!

  • Over 100k happy beginners taught, many of whom now work in coding!

  • So beginner friendly, this course has been selected as the syllabus to train brand new professional Software Engineers from scratch

  • Just updated with a major course refresh to make use of the new look Scratch 3.0

Have you ever wondered if you could code? Well.. you can!  In fact, enrol now and you could have written your first lines of Python code before you next check your Facebook!

Coding for Beginners is the most beginner friendly eLearning course for complete beginners on Skillshare! It doesn't matter if you've never even seen a line of computer code before - if you're interested, or just wondering if you could learn to code, then this is the course for you!

" - Take this course before taking any others!"

This course is perfect for students who are thinking about learning a specific coding language, but don't want to get dropped in at the deep end. Coding for Beginners has been specifically designed as a generic, foundation level course, to introduce you to many of the common coding concepts that you'll go on to use in whichever programming language you choose to learn next - giving you a real head start before you start mastering a dedicated language, and it's syntax.

With over 30 lectures and more than 7 hours of video, this comprehensive course will teach you the very basics of computer coding, also called programming, from the ground up. Starting with foundation level theory, and moving into pseudo coding, and practical exercises - Coding for Beginners includes video lectures, screencasts, tests, assignments, and a beginner level project - building a simple app in both Scratch and Python.

Get ready to learn:

  • Coding is something we already do everyday!

  • Coding in programming languages is something you CAN achieve

  • If you can read and write, you can code!

We'll answer questions like:

  • What is code, and why do we need it?

  • Why are there so many coding languages?

  • What's the difference between coding and programming?

  • What's the difference between front end and back end coding?

  • Is coding a science or an art?

And we'll introduce you to common coding concepts like:

  • How code runs

  • Bugs

  • Pseudo code

  • Decomposing

  • Commenting

  • Common data types

  • Arithmetic & operators

  • Loops

  • Control flow

  • Storing data

  • Lists

  • Functions

Get ready to code your first app!

  • Plan the app in pseudo code

  • Code a proof of concept in Scratch!

  • Code a Minimum Viable Product in Python

  • We'll help you get setup in both Scratch and Python for the first time, and help you every step of the way. By the end of the course you'll be confident enough to keep coding in Scratch and Python on your own

  • As you transition from Scratch into Python, we'll help you to see that moving from a highly visual coding language (perfect to get started), into a much more powerful text based language (used by companies like LinkedIn and Google), isn't scary at all! In fact, we think you'll love it :)

Move on from this course to discover the second course in the 'Coding for Beginners' series:- Coding for Beginners 2 - Get Started With Web Development!

Coding for Beginners has been created by Applause Interactive in the UK, and we believe in quality learning. Rest assured, this course comes with a 30 day money back guarantee! If you are not satisfied in any way, you'll get your money back.

So what are you waiting for?

Let's unleash your creative potential, have some fun, and build some valuable digital skills for work and life! See you inside...

Meet Your Teacher

Teacher Profile Image

Applause Interactive

Digital Skills for Life


Hello, my name is Greg Hamilton and I'm the Founder of Applause Interactive!

My passion is empowering people with digital skills for life.

I've taught over 25k people from all walks of life to code, including my own children who are 6 and 8. My course "Coding for Beginners - You CAN Code" has been selected by Africa Code Academy to teach budding new Software Engineers in Africa about the fundamentals of computer programming. What could you gain from learning how to code? Just interested to see what it's all about? New career? Let's see where it leads - I promise you'll have fun along the way :)

I love hearing from you! Drop me a line at

Happy learning!

See full profile

Class Ratings

Expectations Met?
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.


1. Welcome!: welcome to this course on coding for beginners. This course has been created by Applause Interactive, a leading digital studio based in the UK Our team is made up of software developers, engineers and professional teachers, and we're passionate about empowering people with digital skills for life. My name is Greg Hamilton. I'm a senior developer and product manager here. It applause, and I'm going to be your instructor for this course. I've worked as a digital project manager, scrum master designer, software developer, agile product owner and product manager. I love creating software, and I've built many apps and games when I'm not working on projects. I enjoy teaching my Children how to code before we dive in. Let's take a quick overview of what lies ahead of us in Section one. I'll introduce the course in a little more detail, answering questions and exploring what we're going to learn together In Section two. We'll discover that you can already code. You just might not know it yet surprised? Well, believe it or not, coding, in a way is something you've actually been doing all your life in Section three. We'll explore coding. In a nutshell. Learning all about the theory of code, what it is and why we need it. I'll take you on a whirlwind tour through the basic theory of code and hopefully answer a few questions along the way. Like why there's so many coding languages? What's the difference between front end and back end code? And is there a difference between coding and programming in Section four will really get into the meat of the course, looking at some of the general things that code can do, and exploring many of the common concepts that if you go on to learn a programming language like one of these, you'll meet firsthand. And even if you're not planning on learning a dedicated language, Section four will still give you a much better understanding about what code can do. Section four is made up of some short theory lectures. Where are you pseudo code to help you visualize how code works before we look at some practical demonstrations, using both scratch and python to really see the theory in action using riel code. I've specially selected these two languages because scratches a highly visual programming language designed specifically for total beginners, while Python is a powerful Andi user friendly, text based language. By looking at them together, I hope to dispel some of the myths that coding is some kind of black art and certainly hope to prove that it's nowhere near as scary as it might seem to some. If you've never seen or heard of these coding languages before, don't worry. All will be explained in Section five. It's project time and will really get hands on, pulling everything we've learned so far together and putting it into practice by planning on building a simple app of our own, an app that solves a real world problem. First, we'll plan the basic flow of our app out using pseudo code when we're happy with that will build a proof of concept using scratch to test the basic idea behind our app, and finally will go on to make a minimum viable product, which is a basic but working version one of our app and will code that in Python at the end of Section five, you'll be a coda. Section six is a short course summary where we'll look back at everything we've learned so far, and finally I've included some bonus content to help you take the next steps on your own learning journey. 2. Introduction: welcome to Part one. The gulf of this introductory section is to answer questions about the course in a little more detail. Before we begin, we'll cover What am I going to learn? Who is this course full? Do I need to know anything before starting? What will I be able to do by the end of the course? What are the benefits off learning about code? Will it be fun and engaging? Let's look at that first question now, in this course, we're going to learn about the basics of coding, which can also be referred to as computer programming. If you've ever wondered about the difference between those two things rest easy. I'll cover that later on. Have you ever wondered what coat is? We're going to unpack that answer together, discovering not just what it is but also why we need it. You're going to learn many of the common coding principles and explore what we can actually do with code. You'll learn about pseudo code, which is a bit like coding in plain English and learn some very basics in scratch and python. You'll also learn a little about how to plan and create an app to solve a real world problem using code, I'm confident that you're going to discover very quickly that coding is no India as scary as it may well seem in the beginning. For those of you who are working to keep learning, the bonus material at the end of the course will hopefully help you to go a bit deeper and take your next steps, particularly if you're interested in learning a specific coding language. As the name suggests, this is a course for complete beginners. If you've ever asked yourself questions like What is code? How do people build APS or wondered if you could ever learn to code? This is the course for you. Maybe you've already thought about learning a specific coding language and haven't dived into the deep end just yet. Well, you're not sure where to start. If that's you, you've come to the right place. Coding for beginners isn't for experienced coders. If you can already write code, you probably won't find his course challenging enough. And everyone knows code is love a challenge. You don't need to know anything about coding before starting. This course. Curiosity and a willingness to learn is all you need at the end of coding for beginners, you'll have a solid foundation on Understand many of the common concepts found in most programming languages, giving you a real head start if you choose to go on and learn one of them. You have coded an app in both scratch and python, so you'll be able to officially call yourself a coda. With an understanding of both scratch and python user interfaces, you'll be able to experiment with coding on your own to build on everything you've learned during the course. With both scratch and python at your disposal, you can have hours of fun building APS, interactive stories and games. Many coders get started exactly like this. Perhaps you're moving toward a new job or bringing coding skills into your current job wherever you go from here, on whichever language you decide to learn next. If you want to go on and master language, you've taken a great first step, and this will just be the beginning of your journey. So what are the benefits of learning to code? Well, whether you're looking for a career in the industry starting a new hobby were just wanting to understand the technology that's taking over the world, you can benefit from learning just a little about code. On the surface, it may just look like a load of zeros and ones, but coding is really fun. It's fun because it's useful, creative and collaborative. Before the age of computers, it was the engineers who built the world. They had the opportunity to build useful things like bridges, tunnels, cities and railways. Knowing how to build things gives you an enormous power, the power to be creative and the power to shape the world you live in. In today's digital world, nearly every aspect of our lives depends on computer code in some way. And now the people building. Today's world of the software engineers and the code is coding is highly in demand right now, and that demand is predicted to increase well into the future. Looking back to 2015 7 million job openings were in occupations which value coding jobs, requiring coding skills pay on average $22,000 per year mawr than jobs that don't and half of jobs in the top income bracket or in occupations which commonly required coding skills from job applicants with coding jobs growing 50% faster than the overall market Right now, learning to code could kickstart Your next career move will bring valuable new skills into your current role. Will this cause Be fun? Okay, I'm biased, but I certainly hope so. We've combined theory tips, practical elements and quizzes so that you enjoy your learning journey. Don't worry too much about taking notes or trying to memorize new information, although of course you can. If you want to just follow along and have fun. You can always pause, replay and watch sections again later if you want to. Every time I introduce a new concept, look out for a handy tip or a summary like this one that concludes this introduction. Thanks for watching, and I'll see you in the next section. 3. You Can Code: Part 1: welcome to Section two. The gulf of this section is to discover that coding is largely something we already do every day. Because coding is simply about giving instructions, I hope to show you that coding and programming languages is something that we can all achieve, just like we all learned English. Unless, of course, you're taking this course in another language. If you can read and write, you can coat. In my experience, many people think that coding is some kind of black art or secret club that only certain people can qualify for. Of course, that's not true. The practice of coding or programming is simply the practice of giving instructions. If you've ever given someone directions to your house, you already have experience at this. Let's try a little exercise. Here's a typical neighborhood scene at the bottom. We have a shop and at the top, right the house. Let's pretend that we need to give a friend directions from the shop to the house. How would you do it? It's not a trick question. I'll give you a few seconds to think about it. Great stuff. Now let's start again and replace our friend with a computer controlled robot because we're going to talk to a computer. We've got three simple rules. The robot can't call you if he's lost. So the first rule is that you have to give him the full set of instructions before he leaves. He also has no idea what a traffic light is. Well, there we can recognize the color of the lights. So the second rule is that you have to make sure your instructions tell the robot what to do when he gets to the lights, depending on what color they are. The third and final rule is that the robot only understands his own language, which in this case is called pseudo code. That's what you have to use to ride his instructions down. Here are a few lines of pseudo code for you. Computers read pseudo code from top to bottom one line at a time. That means will need to arrange our code into a single column set of instructions. These lines of pseudo code all we have to work with right now Do you think that you can arrange them to get the robot to the house if you need to, you can use the same line of pseudo code as many times as you like. Here's a chance to grab a pen and paper and write the pseudo code out yourself one line at a time to see how you would write the instructions. Feel free to pause the lecture in just a moment and resume. When you think you've got a solution, I will shortly reveal my code so we can compare. Let's give it a try. Go ahead and hit polls now. Okay, here's my pseudo code. How does this compare to yours? Noticed that I've put in Effect Junction line in before my right turn instruction because I'd like to make sure the robot reaches the junction before turning right, rather than coming out of the shop and just going off the road with the traffic light checking code, we're only able to check for red or amber. That's just one of the limitations of the code we've been given. So I've put await instruction in, and I'm hoping that if the light is green, the robot will just keep going until he sees the next junction. But if the light happens to be red or amber, you'll notice and wait there until it's no longer red or amber I when it turns green and then keep going. But who knows? We may need some more investigation on a bit more detail in there. I've also added a check to see if we're at the house, because I'm a bit worried that if I don't tell the computer exactly when to stop, it may just keep cruising right past the house and off the edge of the map. Now, don't worry at all. If there are minor differences between our code at this point, it doesn't matter at all. The goal of this exercise isn't perfect pseudo code. It's to get us thinking about solving problems in clear steps by writing clear instructions . In this case, the challenge was a navigation problem, and because the computer controlled robot couldn't understand verbal commands, I couldn't speak to it. We had to write the instructions down in a language that the computer can understand. Essentially, that's coding coding is all about giving instructions to a computer and almost every program we use in everyday life, from Facebook to instagram toe angry birds is basically just a set of instructions. Do you know that what we've actually done here with our set of instructions is create an algorithm. An algorithm is just a set of instructions for a computer to follow Siris of Steps. For example. Google has a search algorithm for working out which pages to show you, and Facebook has an algorithm for recommending friends to you. Big programs or applications can be thought off a giant collections of smaller algorithms, each one with a specific purpose, like searching, navigating or logging a user in. Can you see how something relatively simple, like writing instructions, can quickly become more complex when we're talking to a machine? That's the challenge of coding. Computers don't think the same way we do, and they don't understand the world in the same way so we can take reasoning for granted. Our instructions have to be laid out in a careful order with enough information and without missing anything out. When you first see code on a computer screen, it can look a bit scary. I've bean there myself, but now that we know that code is just lines of instructions, how hard can it really be too right. But what if I told you that It's not hard. A tool, in fact, is probably a lot easier than learning, say, French or Italian, because the new coding language that you'll be learning still uses English words. Join me in the next exercise, and you'll have written your first lines of python code before you check your Facebook. See you in the next lecture. 4. You Can Code: Part 2: okay, lets coats, um, python. Now, before you can run python on your machine, you'll need to install it. So feel free to skip ahead if you already have Python installed. Actually, we're going to download and install a program called Idol, which is the development environment for Python, and I'll explain that shortly are walking through download from the official source, and Python is available for free on Mac OS and Windows as well as some other platforms. I'll be demonstrating it on Mac OS in this video is That's what I have. But I know from experience that the steps are almost identical on Windows, and there are very clear instructions for installation on other platforms. Okay, so with the browser open, go ahead and run a search for Paice in or simply navigate directly to python dot org's, which is the official source to download Python. As you can see, this is the official source from the Python Software Foundation on the path, and documentation is available here. A swell on the home page. Go ahead and click on the downloads button, and you're hopefully see your correctly detected operating system here at the top of the page. If not, follow these links for your specific operating system. Okay, But when you've checked the correct address, the next step is to check for the latest stable version off Pathan. As these air constantly being updated. I wouldn't recommend downloading a beater version or early release, so sticking to the latest version should help you to avoid any issues. Python went through a fairly major change when it progressed from Version two Inter Version three, and I'll be using version three here. In the course, I recommend that you do the same as you get a little more acquainted with Python. You might be interested to explore what changed from version two, but you certainly don't need to worry about that at this point. And you can see that at the time of filming, version 3.7 point zero is the latest version. You may have something new here by the time that you take this court. But don't worry, as long as you see a three here is the first number. You'll be fine grabbing that version. Okay, so let's go ahead and download python now, and you may need to give it permission. You can see that my download has started and it's finished already. At this point, I'll skip ahead just a little as the install looks slightly different on each other s, for example, windows. But all you need to do is download python and go ahead and install it as soon as the download completes. Okay, so Python has been successfully installed on my machine now, and hopefully yours too. Now, go ahead and look for a new program on your system. Called Idle. As I mentioned, Idol is pythons, dedicated development environment. Once you've located the Idol program, go ahead and run it and I'll start mine now. Okay, So when idol loads, the first window that you'll see is called the Python shell. This is what the python shell looks like on my Mac. And it may look slightly different on your computer because you may be on a different version of python or a different operating system. So don't worry. If you do notice some minor differences, you may even get a blue warning like the one I've got here. But this is just because I have a number of different versions installed on my system and I havent updated all of them. This blue warning here isn't a problem at all, and we can continue running Python. So this is just background information right now. But if you're interested, a shell is simply a program that follows written instructions. The shell can also be thought off as the weight interact with the program directly. Command prompt on Windows or terminal on a Mac are both shells and part of a shell is the console, which allows you to see the output from a program. The idol shell is great because it allows us to type in input, executes and python code and see an output. So let's see how we can use code to give Pathan and input that it execute the code and see pythons output here in the console or in the show. Let's write our first line of code drumroll, please. Just kidding that I can feel the suspense if you've never done this before, So don't worry about trying to memorize anything will be doing all of this in much more detail later on. For now, just follow along, okay, So, just like human languages like English or French, every coding language has a set of rules. We call this the languages syntax. In general, the syntax describes what you can and can't do with the language and, more specifically, the exact rules for how to do certain things. For example, python can print output to the console, the python syntax for coding. This is print the key word print, followed by a set off brackets on opening and closing bracket or parentheses and part in syntax says we have to use rounded ones, not square brackets, which might look like this. That's correct. Pathan Syntax Pathan Syntax also says that we can coat something within the brackets to print. If we want to print a string of text like a dialogue, we can do that by coding the text, wrapping it in quote marks and placing it inside the parentheses. In the print statement, for example, we can cut the text Hello world, wrap it in quote marks so you can see I've now got an opening and a closing quote. The text has turned green and you'll see why, but later on, and we now have correct syntax for printing out. Put into the console here in Pathan. If you're wondering how to learn languages syntax. Many coders take either practical or theoretical approach on most often both. Taking a dedicated python course will help you to learn python syntax in a practical way, learning a little more about the rules. As you progress through each state of the course, other coders like to dig into the documentation. Every language has a set of docks outlining the syntax, which is often built for quick reference with practical examples. A lot of the time people get hands on and refer to the docks now and again whenever they want to check the syntax along the way. You're definitely going to learn some scratch and python syntax in this course. But remember, this course isn't designed to help you master the scratch and python languages. It's designed to help you understand the common principles of coding like how code runs and what we can do with code when you understand that it's much easier to go on and master a specific language because you can better concentrate on the unique syntax when you understand all of the underlying common concepts of programming in general. Great. So you've just written a line of python code That's the input. Shall we make Python executed? Well, let's do that here in the shell. All we need to do is tap, return or enter. So I'll do that now. And there we go. Python has printed our text dialogue to the console. Hello World Parkinson Tax also says that we can use single quotes rather than double quotes for text dialogues like this one. So, for example, we could say print and use single quotes to encapsulate our string hello world, and when we hit return, it runs just the same. So the choice is yours. It just happens to be my habit to use double quotes and you'll see me taking that approach in this course. Over time, though, you'll develop your own preference. I'm sure we can also code something like this name equals. And let's type our own name, not forgetting to wrap it in quote marks. So I'll code my name Greg. If we hit, enter now or return were telling Python to store our name. Let's go ahead and do that, and we go straight to a new command line. We didn't ask Python to print any output to the console, so it didn't it just stored our name in the background Code will only do exactly what you tell it to. No more and no less. So let's print our name now. How do you think we can coat it? Well, let's say print opening and closing brackets or parentheses. And now we can type night because on name is storing data, which is already wrapped in quote marks. Name itself doesn't need quote marks. This just needs to match this. So let's go ahead and run the code and see what happens. And there we go. We're printing, are stored name. We can also use Python to do maths. How about we code to plus two hit Return and Python runs the code performing the calculation and automatically this time out, putting the result into the console. That's the syntax surrounding the use off arithmetic expressions like two plus two. Of course, we could also say number is equal to two. We could store that by hitting return and then code number plus number. Let's run that code and we get the same result full because number here is referring to what we've already saved inside of number, which happens to be the number two and in Python numbers like this don't need quote marks, but more on that later. So what if we want to coat something a little more complex? Well, we don't have to co directly in the shell. Let's use a file. Instead, click up here on file and new file. And now Idol has created a new blank file for us to store our code in. You can see that it's untitled by default. So let's write a line of code and save the file, and I'll just move this over so that we can still see the shell. Clearly. Let's code another print statement, this time without quote marks, because we're printing a text string and let's say hello. My name is puffy the python and let's save our file So we click on file and safe and let's give up Python file a name. Let's cool it. Puffy dot pie puffy. Don't pee. Why the dot p y file extension tells the system that this is now a python file. In fact, you don't have to use Idol's own code editor. You can use any text editor to write code all you need to do is give the file the dot pie extension and you'll have python code, of course, as long as you use the correct syntax, but you'll pick that up in no time. Okay, so that's safe. And there we go. We've got ourselves a python file, ready to run, so let's run it up in idols toolbar, Click on Run Onda Run module. Or, of course, you can Hit F five has a short cut in future, and there we go. Pathan ran the file, read on, Executed Our Path and Coat and here's our output in the council. If you're wondering why, Python said, Run module here, there are a few terms that you'll come across like this occasionally in the coding world. There's a bit of overlap between terms, and sometimes they can be used interchangeably. What we have here is a part and file. This file contains python code. If we had lots of code, we might call this a program. If it was a small program or perhaps just a part of a program, we might also call this a Python script. Coders like to build large programs as a Siri's off scripts or modules that all work together in order to make a larger program function. And I'll explain why, but later in the course. But that's essentially why the creators of Idle chose to say, run module here rather than perhaps run code or run file. Just a bit of background knowledge. Alright, So back to our first coding adventure, we've just coated python to introduce itself. So how about we write some code to reply? Part in syntax gives us another key word for this. We can coat and we'll write this below as a second line input, opening and closing brackets. The input function, as it's called, allows the user to type and input in the shell or council and store it. The syntax also says that we can code a string between the brackets so we can give the user a little dialogue by saying something like, What's your name? Question mark. Now, when Python gets this line of code and it runs from the top down, Python will automatically print this string of dialogue to the console and wait for the user's input. Here's what it looks like. I'll go ahead and save and run, and there you can see we've now run both lines of code, and Python is asking, What is your name? What's your name? Perfect. So what happens if the user enters the name here in the console? Well, what we could do a store it somewhere so that we can use it later on in our code, and we've just done that. So let's do the same thing again. That's code name equals. And now let's make name equal. Whatever the use it gives us is an input when Pathan asks this question. So now, in this second line of code runs will ask the user for their name, wait for their response and store the response as night. Of course, once name is stored successfully, we can go ahead and print it. Python won't automatically do that unless we tell it to. So to test our code and be sure we are collecting the name properly, let's code another print statement just like we've done before. And again, we can talk print name without the quote marks this time. Great stuff. So let's save and run our script or module and test out our code. Okay, So Puffy is now asking, What's your name Let's go ahead and enter a name. I'll use my real name. Greg Hit, Enter and Parthenon Prince my name because I entered a space before my name here partners printed this base as well. So what we could do to tidy things up a little is at the space here in our text string after the question mark. And if we save and run this code again, we can see when we click to add an input here in the console. The space is already there, so this time we don't get the in debt. So what shall we do next? Well, how about we write a little code to make a decision based on the input? We could check the name that we enter and write some code for Puffy to say something like That's a great name if we give Puffy are really name. And if we give any other name as input perfect and say something like who? That's a nod name. Let's code it. Go ahead and follow along with me. So rather than automatically printing, are stored name. Let's say if name equals with a double equals and don't worry about why we're using a double equals like this double equals is just python syntax for checking If something is the same as something else, remember, if we could name equals with a single equals, we're setting something. We're making the name equal a name that we give But if we could double equals like this if name double equals, we're checking if the name is equal to or the same as a name that we give. Okay, So let's keep going. Let's say if name is equal to Greg and I'm using my name again. So feel free to put your own real name here using single or double quotes as you prefer and being careful with the case. If we tell Python to look for a name with upper case lettering here and enter lower case lettering when we give our input, they won't match. Then we need to finish this line of code with a colon, a little more python specific syntax. Now we can tap, return and go into a new line, an idle automatically. Give us an indented line. This is also correct, part in syntax, and I'll tell you why in a moment. But first, let's tell Python what to do if we give our real name. So if the name equals Greg, let's print and thats print the text string. Wow, that's a great name. OK, and now let's tell Python what to do if we don't give our real name. So let's make a new line. And now we need to get rid off the indent. You'll see why in a moment as I mentioned, but right now we can click back space and remove are in dent. So in line with if above, we can now code else and finish with the colon. This tells Pathan to run some code. If the code above here isn't a true condition, it's back to our else. Let's make a new line. And this time again, we do want the indentation that's print something like not forgetting the quite marks whom that's an odd name. After all, I'm not going to offend myself by saying that a case, and now can you see how the code is visually structured? Thanks to these Indians? If Python thinks that this line of code is true and our name is equal to Greg, this line of code will run and of Python thinks that this line is not true. Then this line of code will run. Python will use if and else to decide which of these lines of code to run, and it won't run both one or the other. That's how we can write code to make decisions, all right, to show he tested out well, Let's save and run, and that's test with a few inputs just to make sure it's working. So what's your name? Let's use the correct name. My name is Greg, and Puffy says, Wow, that's a great name. Let's run again. And this time, let's give another name. Let's say Puffy, that's an odd name. Fair enough. And of course you can see if I run again that even if I used my correct name but use a lower case, G else code runs because the lower case G here doesn't match the uppercase G here that we're looking for in our code. But I like it and it looks like a coat is working really well so far. So how about making the dialogue feel a little bit more personal? Well, what we could do, let's change this string here so rather than exclamation mark. Let's finish with a space and then, outside of our closing, quite mark that's at a coma, a space and type name. This will tell Python to print two things on the same line. This text on whatever is stored as our name, which allows our dialogue to feel a bit more personal. Let's give it a go. We'll save and run again, and this time we want out if condition to be true. So I use my real name with a capital G and there we go, Parson says. Wow, that's a great name, Greg. And you can see here that we actually have a double space between the words name on the word Greg Mining. That's because python will print literally or exactly whatever we have between quote marks . So if we add a space in manually here, Python will print the space here because we have another space. After a comma, we get double spacing. So what we could do is we could remove this space here, for example, Run the code again and it would look a bit nita. There we go much better. Well, fantastic. You've just written a short python program to chat with the user. This could be the beginning of a new chatbots project really well done. If you coded along with me, you just code. It's, um Pathan. And I did promise you do that before checking your Facebook feed. So why not go and tell your friends that your A code to now? Seriously, though, that's how easy it is to code. You really can do this in the coming lectures. I'll take you through coding in a nutshell and explore these concepts that we've just used in more detail are also introduce you to many more. And we'll be using scratch as well, which is a much more visually friendly coding language built specifically for beginners to help you really visualize coding concepts before we also test them out here in Python, as we do, this will be working toward bringing it all together in a simple but fun project. So that concludes this lecture. And thanks so much for watching before you complete this section. Why not have a go at the short quiz and I'll see you in the next section? 5. You Can Code: Recap: Let's have a quick recap. We started this section with two goals. Did we achieve them? The first goal was to discover that because coding is simply the practice of giving instructions is something that in a way, we already do every day when we write code for computers to follow, we just need to use the language that they understand the coding language, and we need to think extra carefully about giving the right amount of information and logical steps without missing anything out. Our second goal was to understand that coding is something that we can all achieve. Do you feel like you're a tiny step closer now? The navigation exercise we did was really simple, but it got us thinking about writing instructions carefully and using a new language. We used a little pseudo code for that and will go on to learn about pseudo code and use it again in the coming sections. As we explore many of the common instructions that we can give computers by using code, we also writes, um, Python to show you that text coding isn't as scary as it first appears. I hope you enjoyed that little exercise, and we'll also be using python again later on. Sometimes it's easy to get lost by thinking about coding as building the next Instagram or angry birds. But remember that all of those APS are just collections of lots of smaller instructions designed to do specific things, like logs the user in, upload a photo or launch an angry bird across the room. As soon as you break a program down into smaller parts, writing the instructions suddenly gets a lot easier. Don't worry if you still feel a bit overwhelmed in the next section, we're going to look at coding in a nutshell, and hopefully answer many, if not all, of the questions that beginners typically ask about coding. That brings us to the end of this section. Thanks for watching. I hope you enjoyed it before starting the next section. How about we take a quick quiz, but feel free to go and grab a cup of coffee first? Everyone knows that coders love coffee. Enjoy the quiz and I'll see you in the next section 6. Coding in a nutshell: Welcome to coding. In a nutshell. The gulf of this section is to answer the questions that we typically hear from beginners. Questions such as What is code and why do we need it? Why are there so many different coding languages? What's the difference between coding and programming? What's the difference between front end and back end coding and is coding a science or not ? To understand why we need code, we first need to understand a little bit about how computers work. Before we built computers to connect the world through the Internet, we built them a standalone machines to solve complicated maths problems. One of the reasons for that was to break secret codes during World War Two, when codebreakers needed to try millions of possible combinations of settings very quickly to give you an idea of just how quickly it would probably take the average human over a week to count to a 1,000,000. A computer can do that in under a second. Computers are basically just a bunch of switches called transistors, which can either be on or off, depending on where the electricity is flowing through them or not in the computing world. On can be represented by the number one and off by the number zero zeros and ones on off. We call this binary code or machine code, and this language machine code is what our machines our computers are actually controlled with as we send pulses of electricity on and off through their circuit boards. Welcome to the machine world. Why the computer's only understand machine language? To answer that, we need to take a closer look at one of the switches that they're made off. Here's one close up. In computer terms, this switch is called a transistor. Another good name for it is a logic gate. Why? What? Because it behaves logically. The transistor has an input side andan output side. It sits there all day long and all night long to if it's turned on, looking at its inputs and deciding what to do with its output. This one has two inputs and put one an input to it also has a single output. This transistor is called an and gate because it looks for electricity to flow into both input one Ondimba to before allowing electricity to flow through the output. If it only gets a signal to one of its inputs. It won't send an output signal. Here's another transistor. This one is called an or gate, so called because it will send an output signal if either input one or input to gets a signal. If you're interested, there are a few more gates to discover, and you can always run a Google search to discover them all. Each one handles logic slightly differently, and when combined, they become incredibly powerful and incredibly fast with any kind of logical operation. Do you want to see what it looks like if we wire a whole bunch of transistors together and then use machine code to send zeros and ones or electrical signals inputs into the mix? Let's go ahead and take a peek. Here's a simplified top down view of a central processing unit. Will CPU? Let's imagine. This is an arm 64 chip inside an iPhone. This is actually where all the transistors live on their all wired together. The square boxes on the far left of the screen represent what happens when machine code runs. The boxes are going to randomly turn on or off, generating electrical zeros and ones which will feed into the transistors as their initial inputs. Although our boxes turn on a rough randomly for this demonstration, this could easily be machine code coming from a game or a nap. Watch what happens to the flow of electricity through the transistors as the boxes on the left turn on and off. With so many transistors, the computer's hardware can respond to any possible combination of inputs we send it. It can be incredibly powerful. The electrical signals flow through the circuit until they reach the right hand side, where they can be used to give us a hardware output like saving our progress to memory. What was steering a driving his car, although signals turning on and off. If we could see those in real time on a screen would see the machine code lines and lines of zeros and ones just like the Matrix. The problem, though, unless you're the one, is that we humans aren't very good at writing machine code nor reading it. Nobody wants to create the next Facebook or Minecraft by writing million's and million's of zeros and ones. Let's zoom back out again and see what we can do about that. So here's our computer and hears us. Now we know that computers only understand machine language, but we humans have a seriously hard time reading and writing it. And even if we could programming computers directly in machine, language would take forever. Likewise, machines don't understand human language, so we can't just shout instructions at them when we want something done. So how do we bridge the gap? Well, the answer is that we need a middle language, something between human and machine code, that middle language is what we simply call code. Actually, there are many of these middle languages here, just a few of them. Some are closer to machine code, like assembly language or see, and some are closer to everyday human language, like python or scratch. That gives us a kind of spectrum of middle languages that we can choose from to bridge the gap. We'll look at why there are so many different languages shortly. Right now, though, you may still be wondering how our middle language, what we generally call code, actually gets turned into machine language. Meet the compiler. Generally speaking, the compiler converts are middle language into a lower level language like binary or machine code. Sometimes the middle languages compiled down to something called byte code, and sometimes an interpreter is used to run the middle language line by line on a virtual machine without compiling it all the way down to machine code. In the real world, things can get complicated, and they're usually a number of layers in between the language that you code something in on the final machine language. But most of the time you don't need to worry too much about those unless you get into computer engineering or computer architecture. So why are there so many coding languages? Well, we've already discovered that there's a kind of spectrum with lower level languages like C that are closer to machine language on higher level languages like Python, which closer to human language. The desire to create languages, which are both easier to read and write, is part of this answer. The main reason why there are so many different languages, though, is that they've all been created with a slightly different goal in mind, and that makes some better for certain tasks than others. For making websites. HTML is the ideal language for styling websites. CSS is fantastic for adding interaction to a website. Java script is wonderful for building a powerful application. Python is perfect, and for gaming, C sharp is excellent. Each language was created for a task. In some cases, there's overlap. And where languages have been created with the same task in mind, it's often the case that the differences air down to a desire to make that language a little bit easier or more efficient to use. Some languages have grown into something larger than they were originally designed for. For example, JavaScript was created in the very early days of the World Wide Web to run inside a Web browser and add a bit of interaction to static Web pages. As the Web changed from being a document delivery platform to more of a software delivery platform, JavaScript ready started to grow even more, mainly because of a technology called Ajax, which turned Web pages into software. JavaScript could run everywhere the Web waas, which was great, but it was still trapped within the browser. Sometime around 2000 and nine, JavaScript broke out of the browser. Thanks to know J s, which allowed Java script to run on your hard drive JavaScript suddenly became a riel powerful programming language, and it's now used by Netflix PayPal and linked in to name just a few. This is a question that we hear a lot from beginners. Coders tend to think of software in two halfs, the front end on the back end front end coding. It's all about developing the part of the software that the user can see, click or tap on in Web development. Front end languages include HTML, CSS and JavaScript. Everything you see when you visit your favorite website is likely to be a mix of HTML for the content on high level structure, CSS to style. It will make it look good and JavaScript, which might be driving things like buttons, drop down menu sliders and contact form interaction. When using the website, you can directly see the result of the front end developers work. In fact, you can actually see the code itself in your browser, right. Click anywhere on a webpage and click on inspect or view source. What you'll be looking at next is the actual HTML CSS and JavaScript code that the page has been built with. You can do that for any website. On the flip side, back end coding is all about developing the other part of the software that the user often doesn't see. The application, database and server. The application interacts with a database to store information on both the application on the database. Sit on the server on often multiple service to make the application available to other people 24 hours a day, 365 days a year. Back end applications are often built in languages like Python, ruby or PHP and could use another language like sequel to interact with the database. The user doesn't see this code, but while it runs quietly in the background in makes things work and it keeps the world running. How about you? Do you have a preference for front end or back end? Some developers do both. They know how to make a great user interface and make the software work behind the scenes. They're called full stack developers because if you think of all the languages or technologies as a stack, they work in the whole stack. In fact, the term stack is when you hear a lot in the coding world, developers will ask each other. What kind of stacked do you work on? Because different products, teams and companies use different stacks to build things. The lamp stack uses Lennox for the operating system Apache for the server, my sequel for the database and PHP, Pearl or Python for the application. The Means Stack uses Mongo DB for the database Express for the backend framework. Angular for the front end framework. A node for the back end run time. If you're ever interested to explore which stacks some of the most successful teams in the world use, check out stack share dot io and you can see which technologies used Airbnb uber Facebook on Spotify, to name just a few. Don't worry if you have no idea what the technologies are all about. Yet at this point, it's good to just understand that languages and technologies that play nicely together form naturally into these. Stacks and coders tend to gain experience in different stacks, depending on what kind of projects they work on and whereabouts in this stack they work front end, back end or full stack. Another question I hear a lot from beginners is this one. What's the difference between coding and programming. Well, as with many terms in the coding world, there's a lot of overlap with these two terms, and there are times when they can be used interchangeably. But sometimes one is more appropriate than the other. This is code, but it's not a program. This HTML is code, but all that it's really doing is adding structure to the text to turn it into a Web page. This Web page on its own isn't a program. If you were writing this code would certainly say that you were coding, but you probably wouldn't say that you were programming. The program is a set of instructions usually written in code that tell a computer what to do. Programs normally have logical steps to do different things in different situations. He is part of an application written in Python. This is code, and it's a program. If you're coding this, you probably would say that you are programming because this set of instructions tells the computer how to greet different people. Depending on who we're talking to. Here's another way to think about it. Coding is giving instructions, and so is programming. But whereas you might say that your coding a button to be blue with CSS, you wouldn't say that your programming a button to be blue. But if you go into code what that button does to love the user in when pressed, you may well say that your programming and new logging flow in really simple generic terms . Coding can apply to pretty much anything front end or back end. Where is the term programming is used to describe building more complex software with inputs and outputs, which for the most part, is a back end activity. So is coding a science or an art? I've thrown this question in because I personally find it an interesting one to consider. Take a look at these two HTML examples. Which one do you prefer? Coding certainly does fit into the field of computer science, and it can be incredibly technical. But that doesn't mean that it can also be considered an art code. Could be beautiful, elegant and efficient. The more I code more, I appreciate elegant code. There are many ways to do the same thing with code, but some ways, or just more pleasing than others, code might be faster, shorter, simpler, will make something very complex feel organized. Maybe it uses fewer resources such as memory, in addition to that functional appeal code can also be nice to look at. Perhaps you reached a conclusion about which of the two examples you prefer ignoring the fact it would probably be much easier to work on. Wouldn't you say that it just looks nicer? A lot of the effort that goes into making code look clean on the screen is to help you or other coders to understand and work on it. But it is worth noting that as long as you use the correct syntax, the computer doesn't care how pretty your code looks. If it ends up going through a compiler, it could look like this regardless of your style. Like so many artists, coders, developed styles, individual styles of fascinating and keep things interesting. But there is a downside to being a little bit to freestyle. Too much variation can make collaboration a challenge. To avoid too much variation and keep some conformity, coders like to follow conventions. The convention is a generally agreed standard will rule to make working together easier. Many developers take great pride in their coding work and try hard to write code. It's clean and efficient and conforms to conventions as you do more coding. It's interesting to see just how quickly you move on from trying to make something simply work to trying to make it work better. Congratulations on completing Section three. We started this section with five primary goals and sought to answer the questions. What is code and why do we need it? Why are there so many different coding languages? What's the difference between coding and programming? What's the difference between front end and back end coding and is coding science or not? To double check that we can answer all of these questions? Let's recap some of the main points that we've just covered. We humans can't just talk directly to machines. Even Alexa and other virtual assistant software has to eventually convert our speech into code. So we use code as a middle language, making it easier and more efficient for humans to write instructions that machines can follow. Machine code, or binary, is the lowest level language that's run or executed by a computer. Some languages are closer to machine language, and some are closer to human language, giving us A. Coding spectrum. Different languages are created for different purposes, like building websites, mobile APS or games. Front end developers are largely concerned with how software looks and feels and code in front end languages like HTML, CSS and JavaScript. Back end developers mostly care about functionality behind the scenes, working with applications, databases and service and coding, and back and languages like Python C plus plus or ruby Full stack. Developers work on both front end on back in languages, languages and technologies that play nicely together. Naturally form stacks. Companies have their own preferred tech stacks, and code is often have experienced uncertain stacks like lamp will mean very often the terms coding and programming are interchangeable, but programming better describes coding functional applications or games. You're almost always coding when your programming, but not always programming when you're coding God has tried to write code. That's well structured and efficient. Coders also follow conventions to make collaboration easier, especially in a world where a team of coders could be made up of people from many different countries, cultures and human speaking languages. That concludes this section on coding. In a nutshell. I really hope you enjoyed it. And thanks for watching as a waste the quizzes waiting. I'm sure you'll breeze it. Have fun and I'll see you in the next lecture. 7. Common Coding Concepts: Goals: welcome to common coding concepts in this section. Our goal is to understand a little more about what CO. Can actually do as we prepare to create our own up and actually use these concepts in the next section. This is going to be a fairly meaty section. So I've broken it down into a series of smaller lectures, each one with some theory where we look at some of the concepts using pseudo code and some practical demonstrations where we'll see the concepts in action. Using both scratch and python for the python, demos will be using the same Idol program that we used back in Section two. We haven't used scratch yet, so help you to get set up so that you can code along with me during the practical demonstrations. If you wish. Let's take a quick overview of some of the common concepts were about to cover. Don't be alarmed if you hear a lot of technical terms as, like cover the syllabus. Everything will be explained from the ground up in the coming lectures. We'll start by looking at how code runs before exploring the concepts of bugs, pseudo code, decomposing, commenting and common data types. After that, it's time to look at arithmetic and operators, loops, control, flow, storing data, lists and functions. The best thing about understanding these common concepts is that you can go on to use them in almost every programming language and any project that you decide to get involved with. I'm excited because at the end of this section you'll really have a solid foundation. In the theory of coding. You have seen both the theory and practical demonstrations, and you'll be ready to start seeing it all come together as we build our simple app, Let's get started. 8. Scratch Setup: Okay team, so let's get you set-up in Scratch. Now, we'll start by heading over to this web address, And I've added this link as a note at the beginning of the lecture just in case you can't read this link. Of course, you can also just go ahead and run a Google search for Scratch to see that this is in fact the official website, which I promise you it is. So here I am already on the Scratch site. And it may also help you to pause this lecture for a moment and switch tabs as you follow along with me in the video, if you're watching this lecture inside of your web browser. Now, if you're new to scratch, then welcome. So what is scratch? Great question. Well, scratch is an application or app created by some clever folk at MIT in the US, and designed specifically to teach beginners how to code. So it's absolutely perfect for our needs right now. As of 2019, it's being used by over 30 million beginners to create fun games, interactive stories, and animations. Now Scratch has both an online web application, a web app, which is what we'll be using, and an offline application for Windows, Mac, Chrome OS, and Android, which is available to download. So if you wanted to, you could use Scratch offline. But as I say, we'll be using the online version so that you can seamlessly follow along with me regardless of your operating system, because we don't need to download or install any software. Now, let's take a second to check out the Scratch homepage. So you can see here that the homepage has a section for featured projects which look quite interesting. Ultimate feminine is up here right now at time of recording. That's probably not one for me. But let's see what else they've got tomato chopper, that's interesting. Or here we go. Pigeon with AI, pigeons that becomes smarter. Who doesn't want to check out that project. There are also some featured studios here, some curated projects. Let's see what else we've got. We've got 0, what the community is remixing. This is, this is interesting. Now remixing is a really interesting concept in coding in general and also here in Scratch. The idea here is that you could create a project IE. You could write some scratch code and create a file. And you could then share your project with the wider community, the community here in Scratch. So other people could dive right into your project and actually see all of the code that you've written and start to play around with it or re-mix it. So perhaps you might start a project and somebody else could help you to improve it or take it in a completely different and interesting direction. You never know what's going to happen with remixing. And it's just such a fun way to share your ideas with the world and see what happens. And as I say, we Professional Coders, we actually do this all the time. We share our code in what we call open source or community Code. And other people can play around with it, improve it, and help us work on it. It's absolutely fantastic. And we can also see here what the community is loving. So we can see what other people are really enjoying by way of the projects. And it really is a great community here in Scratch. Of course, you can easily participate in the Scratch community if you wish to, both during and after finishing the course. Now, in order to save your work here in scratches online editor. So Scratch web application, you'll need to sign up for a free account. And I'd like you to do this so that you can save your progress. When we get to the project section and take as many breaks as you need to. Coming back each time where you left off. After all, taking regular screen breaks is a great habit for budding programmers. So if you're new to Scratch, look for this button that says Join Scratch. And then go ahead and complete this very simple sign-up process. If you already have a Scratch account, great. Please go ahead and sign into your account now, if you haven't already done so, and I'll let you do this on your own and we can pick up when you're signed in. Feel free to pause the video a moment and finish your sign-up now. Okay team. So once you're signed into your Scratch account, you should see a screen that looks something like this. Now we're still here on the home screen of our new account. But we now have an account tab here on the top right. And next to this account tab, there's a little tab called my stuff. And this is where we can save our work. Excellent. To start a new project or a new piece of work. Perhaps you'd like to think of it as a new scratch file. We can go ahead and click on the Create button here. Now here we are inside of this scratch Editor. Isn't that exciting? And I've made mine full screen so that you can hopefully clearly see it. And how about I briefly run you through all of the key areas that you need to know about here in the editor in order to begin your mastery of scratch. Now, this big area here in the center of the screen is the script area, which is where we'll do our actual coding. This area is the Scratch equivalent of the text editor that we used in idle to write our Python code. Up on the top right is the stage where we'll see the result of our code running. Now, scratches built primarily for apps, games, and animations that use characters or actors. Which is why we start out right away with a little character here. There's Scratch cat. He's like the scratch mascot. And the stage is where we can bring characters or images, technically what we call sprites to life through code. Now the term commonly used for characters or images like this one, the cat is the term sprite. So our cat here is actually a sprite. Now bottom right here is this bright area where we can create and view all of the sprites that we need for a particular project. So we might have the cat and perhaps a mouse. Maybe we're writing a cat and mouse game in Scratch code, or perhaps designing a cat and mouse based animated story. Well, of course we can do both. The panel over on the left here, this one with all of the blue blocks in it currently is called the Block Palette. This is where we'll find all of the code blocks that scratch gives us to use in our project. We can use these blocks to build code or write programs. And code blocks have different colors depending on what they do. For example, these blocks that control motion are blue. Blocks that control sound, for example, are pink. And you can use this menu here on the far left to switch between the various types of code blocks available. You can see we have motion, looks, sound, events, control, sensing, operators, variables, and my blocks, custom code blocks that you can create in Scratch. There's also a Costumes tab here next to the main code tab that we're on before. Remember these are like code blocks. Well, the costumes tab. Think of this as a graphics editor of sorts. We can use along with our code to change the appearance of our sprites if we want to scratch, call this a Costumes tab, because if our sprites are actors on stage, then costumes are like their wardrobe, allowing us to change their appearance in our play or our program to be exact. Now this is a very scratchy concept, if you like, because creating short stories or plays using actor's costumes and code is such an easy way for beginners to learn how to write code as a script or set of instructions for our actors to follow scene by scene or step by step. And of course, what play would be complete without sound, which is why scratch also gives us a sounds tab. And this is where we can create or select the sounds that we want to use in our play or our program. Now the big idea behind scratch is that we can drag blocks of code like this into the script area and snap them together like this to create our scripts. And did you know that actors read from scripts? Directors read from scripts to in the real-world. But a script is also a technical name for a snippet or piece of code. In fact, script might refer to you just a short part of a program, perhaps even one line of code, a few lines of code. Or it could be used to describe the whole program. One really large script potentially. So now when you hear the word script, also think code, lines of code or blocks of code. Let me demonstrate. Let me get rid of these blocks here a minute. We can just drag them away to get rid of them. Now, let's say we wanted to make a program. We want a script that makes our cat say hello and move to the right here on our stage. Well, scratch gives us a couple of handy controls. Here on top of the stage, we have a green flag and a red stop button. So this is a GO button. This is a stop button, and we can wire these up using code blocks to do something. In fact, let's do that now let's go over to the Events tab. That's the yellow one here. And let's get this event called when green flag clicked and drag that over into R script area. And that's how easy it is to start a new script or to start coding a program in Scratch. In fact, this is actually looking a little bit small on my display here, so I'm sure it's going to be quite small. And yours, down on the bottom right of the script area, there's a button plus similar to increase the size and zoom in. So I'm going to do that a few times and hopefully that will make it clearer on the screen. And I then just need to send to the script here. That looks about right. And hopefully you can see that a bit more clearly now. Now the next thing we can do is go back up to the Looks tab. That's the purple one, and find a code block that makes the sprite say something for a period of time. And it's this top block here, say, and it's got a default text in here, hello, four and a number, two seconds. So let's drag that down and snap it onto the bottom of our script like so. Now, in fact, we want our cat to say hello, so we'll leave this hello text here, but of course we could change it. In fact, let's change it just for fun and say, hey there for two seconds. Once our blocks are snapped together, our code script will run in sequence, line by line, step-by-step or block-by-block, if you prefer. Beginning with saying, Hey there for two seconds, when we click the green flag and remember that's this button up here. So let's go ahead and test it. And there we go. Our sprite said, Hey there for two seconds. Our code went top to bottom, finished. And then our Hello text or hey there texts disappeared from the stage. If we were to click the green flag again, our code would just repeat from the top down to the bottom. It's that simple. Now we said that we wanted our cat to move to the right after saying hello. So let's do that. Let's use a block that says Move and number of steps. We can find it here in our motion tab. And it's once again the top block move. And it's got a default of ten steps. Snap that in underneath because we want our sprite to say, Hey there and then move. Now, I know from experience that the sprite has to move in a certain direction and can move at a certain speed for a certain distance. But we don't need to worry about that right now, thanks to the defaults, scratch has built-in defaults that it will use unless we tell it to do something else with code. We just want to make our cat moved to the right. And I know that he's already pointed to the right behind the scenes, so to speak. Meaning by default. If we simply use a move block, he's already pointed to the right and we can actually see that because he's facing right visually. I also know from experience that around a 100 steps should work for this quick demonstration. So we can change our ten to 100 steps like so. Okay team, so we've got a nice short script over here in the script area. And our sprite is ready here on the stage. Well, at least I hope is. So let's run our code and see the result. Okay, so here we go, green flag. Watch what happens. Hey there. And he moves to the right. That's about a 100 steps. What we could do if we wanted to. It's visually drag our sprite back to the center. We could do that with code at the end of our program if we wanted to. But for now I'm just going to do things really quickly and show you we can drag him back to the center or even further left if we want. We can change the number of steps, let's say 300, and run our code again. Hey there, uhm, he's nearly off the stage this time. Fantastic. So our cat sprite set. Hello, hey there, and move to the right. Perfect, That's how easy it is to get started with coding and scratch. Now if you're wondering how to save your project in Scratch, it couldn't be simpler. Up here on the top right of the toolbar. Simply press this button that says Save now, and you'll see projects saved. Where's it safe to? Well, if you click on the little folder icon here, then my stuff folder, you'll see your saved project, which is currently untitled. And here you can click on it to rename it if you wish. You can add instructions to tell other people how to use it. Perhaps you've made a game and you want to write instructions and how to play it. And if you click on see inside, here or back on this screen, the C inside button, you can go back into the editor to see inside of your project again. And of course, you can delete it. Now if you're coding along with me and you'd like to keep things tidy. You can go ahead and delete this temporary project now as we won't be using it again. So now that we've got scratched setup and ready to go, we'll be using it a lot in the coming lectures where you'll have an opportunity to simply watch some demonstrations for me, if you like. All code along with me. When we start working on our project. In the next section, we'll come back to scratch yet again to build something that we call a proof of concept. And at the end of the course, you'll be able to unleash your creativity on the world and create projects of your own. You may find a lot of inspiration from the community sections we looked at earlier. So feel free to go ahead and explore those. But first, let's get back to our theory and explore how code actually runs. And I have a feeling you may already have a very good idea from the Python you wrote earlier in the course. And from this short walkthrough that I've done with you. So let's go ahead and find out. 9. How Code Runs: In this lecture, we're going to explore how code actually runs. Another term for that is executes. To execute code means to run your code on a computer machine. An executable file is a file or program containing your code that can be run by the computer. Generally, code is read left to right, character by character, and top to bottom, line by line, or step-by-step. Here's a little Python code. Each line of this code print some text to the console. When we run it, we'll get this output. I'll demonstrate this in action in just a moment. The first line is read from left to right and executed. Then the second line. And finally the third line. The machine will always start at the top and work its way down through our code. We can make the machine pause, go back up and run certain lines of code again, and even skip or ignore certain lines without executing those instructions. I'll show you all of these tricks a little further on in this section. Now that we've got scratched ready to go, Let's play around with it a little and see some code running for real. Hey everyone, and welcome back to scratch once again. Now I'm here on the Scratch homepage, signed into my own Scratch account. And I'm going to go ahead and click on the Create button here to start a new blank project in the scratch editor. If you're coding along with me, then go ahead and do the same thing. Okay, so here we are in the editor. And just for good practice, I'm going to go ahead and click on the project title and rename it. Let's call this one how code runs. Like so. Now let's proof or tried to prove that code runs top to bottom by recreating the dialogue flow that we just saw in the previous slides. So we know that Python uses the print function to output text to the console. And we've already seen earlier on that we can use the say block. And that's over here on the menu. Here it is. And in fact, that's looking a bit small, so I'm going to increase the size, mike. So there we go. Now we can use the same block here in Scratch to output text, whatever we type into this block here on the stage, on the top right-hand side. And scratch actually gives us a few ways to achieve this. There's this say block that I've just used here, which will say something on screen and leave it on the screen until we tell it not to or we tell it to do something else. And while I'm here, actually let's make our SE block say something other than hello, let's say, and get ready. And you'll see why in a moment. There's also a code block to say something for a defined length of time. For example, say hello for two seconds. We've already used this block earlier on. And it's handy because it nicely demonstrates the delay between each block of code running. So I'm going to use it here in our little script. And in fact, let's code three of these. You'll see why shortly. Let's start by doing a little bit of a countdown. So let's say 3. Two seconds is fine for now. And what we can do is duplicate that block to make another one. This time we'll say two. And then we'll duplicate again. And we'll finish on a number one, each one of these for two seconds. And there is another way to output texts onto the stage here in Scratch. It's also in the lux menu, and it's the think block. So once again, we can think something for a defined length of time using a block like this. Or we could simply think without the timer to think on screen and leave the code onscreen and until we tell it, well, not to be on screen. So just for demonstration purposes, let's code one of these into our script, just so that you can see yet another block of code working here in Scratch. So at the end, we'll think, and let's think. Go just to see the difference between the say in the block in action here on the stage. Now, to execute our script OnDemand, let's grab a when green flag clicked block of code so that we can tie our code execution here. One of our UI or user interface buttons here in the scratch editor. And we'll use the green flag button to kick-off our script to start our program. All right, so we can find that one here in the control menu. No, it's not. It's in the events menu. So when green flag clicked and that needs to go at the top of our script. Okay, so now when we press the green flag button, we can execute our code and make it run. So thinking about what we know already that our code runs from top to bottom. What are we expecting to see here on stage when we run our code? How to think for a second and think specifically about the order, as I mentioned, and about speed. And there's a little clue there about what's coming next. Okay, Let's give it a go. Keep your eye here on the stage. And in fact, I'll move the sprite down a little bit so I'll make sure anything on the stage is more in the center. And let's run our script. Here we go, 3, 2, 1. And then a slightly different visual dialogue saying go and go is, well, it's not going anywhere, It's staying on the stage. So there you go. Was that what you were expecting? Did you notice anything slightly different with what was happening here on the stage and our code? Well, did you see this dialog here to say, Get ready? I didn't. Now let's try it again just to be sure. I'll stop and restart. Keep your eye on specifically looking at for this Get ready text here on stage. Here we go. Well, no, we go straight 321. Go. So we didn't get out, Get ready, text. Something a bit odd is happening here. Well, actually it's not odd at all. So let me stop and explain. In fact, I'll stop the code here. Now what's actually happening is the first code block to say Get ready. It is running, it is executing because our code is running from top to bottom. And we're proving that by the order of our other code blocks appearing on the stage, it is running. And then we're very quickly moving on to the next, say block 3. And we're defining a time for that one. So we can see it on stage for two seconds. Then we get the two, then we get the 1, and then we get the go, and we don't do anything after that, so go stays on the stage. So the reason we don't actually see our Get ready text on the state, even though this code block is executing. Well, it's because computers are fast. It's actually the whole idea behind computers and why they were created in the first place to do repetitive tasks many, many times and very quickly. So what's happening here on our stage is actually normal and it's how code should run. It's just that the computer so fast that your human, I probably can't even detect the text change on screen. You don't even see the result of this code running happens too fast. If you do see the Get ready text, well, you must have incredibly good eyes and your visual processing skills are very fast indeed. And if you don't believe me, if you think you know what, Greg, I don't think you're right. I don't think that Get ready text is running or that code is running. Well, all we need to do is separate our code blocks like so, and just run our script again to see. And there it is. Get ready. And of course, we haven't defined a time and we haven't told our program to do anything else. So just like our final think command down here, I think statement or block, I'll get ready code bays on the stage. But as soon as we connect them back up and run the code again, we're telling the machine that it can move straight on to the second line, or if you include our green flag, the third line of our code. For the, say, three, for two seconds block. As soon as the computer is ready, which is basically just very quickly overwriting. Get ready text on the stage. Now, if we wanted to be sure that the user can read out, Get ready texts. There are a number of ways we can deal with this. We could replace our SE block with a safe for block, just like the ones we're using below. But again, for purposes of demonstration, I'll show you another option that we can use here in Scratch, as you might want to do something like this in the future. Here in the control tab. You can see that we have a block of code to wait. And we can wait for a defined number of seconds. So that's useful. And if we were to drop that in between. Say Get ready and say three for two seconds code. And we set that to two seconds, where manually slowing down our script for long enough that our human eye can actually read what's going on. So let's give this one a test from the top. Get ready 321. And just like the formula one, go. And we can wait for a defined length of time. As I said, there are also additional options. In fact, here's one. We could wait until something happens or perhaps until something doesn't happen in our code. If we had a few things going on in the background of our program, we might want to, I don't know, do something like wait until a mouse appears on the stage and then the cat can say something like, you better run because I'm coming after you or something like that. We could wait until another condition is true or false before we allow our script to continue running on its journey from top to bottom. And hopefully now that's made a bit of a, hopefully not too much of a laborious point that unless we deliberately slow code down, it simply happens so fast as it flows from top to bottom that sometimes we might not actually see it running. Now, how fast does it run? You may be thinking, and that's a great question. Well, the truth is it depends. It depends on factors such as the coding language used and the environment that it's running in, that the code is running it. And what do I mean by environment? Well, that could be the speed of the computer machine or the hardware that's actually executing the code. And Computer Engineers cared deeply about how fast their code runs and they measure it. In fact, where there are a few different ways that an engineer could code something to ultimately get the same result? Well, most engineers would choose the fastest or the most efficient method, because it happens to be milliseconds faster than another method. Speed rarely does become very, very important for certain applications, such as the financial sector, where the speed of transactions happening around the world is mission critical for the code is working on those systems. They would give anything to get an extra millisecond shaved off the execution of their code. Now we don't need to worry about exactly how fast code runs at this stage. So let's just call the speed that code runs well, the speed of code. And let's just agree for now that it's very fast. So just to recap, code runs or executes top to bottom. And it's important to note that it actually runs left to right as well. First of all, we need to tell the computer that the cat needs to say something and then we need to tell it what the cat's going to say. And that will become more apparent when we get to Python. I mean, the way that our code is read left to right when we get into a text language like Python is a little bit more clear. And without getting into quantum computers, code also runs one step at a time on its journey, left to right from top to bottom. And it does that very, very quickly. Indeed. It runs fast. Okay, great job team. Now as I mentioned, we won't be using this code again. So feel free to keep it if you'd like to keep it as a reference. Otherwise, feel free to exit the editor. You can go into your my stuff folder and you can go ahead and delete it. So there is how code runs. I'll delete that project and well, I've got an untitled one from earlier. Oops, didn't mean to do that, so I'll delete that one as well. Next up, let's see the exact same behavior that we've just seen here in Scratch. But this time we'll run it in Python so that we can see how it looks using a text-based language rather than a visual one. See you there. Here we are. Once again, an IDL. Idl is actually an IDE or integrated development environment. It gets that name because it contains everything a coder needs to write, test, and run code in one place. Think of it as the equivalent of the scratch website. Feel free to follow along with me by launching idle on your machine. You can see that idle always launches with the Shell window opened by default. Just as a reminder, because it's been awhile since we used Python last. The shell allows us to type inputs. It executes our Python code and presents outputs on the console so that we can see them. The console is similar to the stage in Scratch, and that we can see the results of our code. But encoding, we almost always only use the console for testing our code. Because we normally use code to build apps or games with a graphical user interface or GUI for short. We often shorten that term even further to just the UI or user interface. Rather than type code directly into the shell. Let's go ahead and create a new file. Just like before, we can write lines of code in this text file and run them through the shell application. This is the Python equivalent of the script area in Scratch. It's worth noting that you don't need to use the idle text editor. You can write code in any editor you like, as long as you save it with the correct file extension. So to be a coder, you certainly don't need any expensive tools. Idle is handy for us. But many professional coders do choose code editors that have a few more tricks up their sleeve. And I'll talk about that later. Okay, so let's code. Let's recreate the same code we just used in Scratch to see our Python running top to bottom to print an output to the console, we can use the print function. The syntax is the keyword print, followed by an opening and closing parentheses. Python will print whatever we put between the parentheses into the console. In this case, let's start with a string of texts saying, Get ready. We wrap our text in quote marks, and I'll tell you why a little bit later when we get to data types. Let's add some more print statements to recreate what we coded in Scratch. Will start by printing the number three and the number 2. Finishing on the number one. And we'll wrap things up with go. There we go. Now we've got a nice little script ready to run. So let's save our Python file and run it. To save a file. We can go to File Save. And let's give it a name. How code runs. You can see that it's automatically got the PY or Python extension because we've used idle. Now our file is saved. We can hit F5 or we can use the run command. Let's run the module and see the output in the console. And there we go. Our Python code ran executing the first print command, then the second 1, third 1, the fourth one, and finally the fifth one. Running our code line by line one at a time. Again, the code executed very quickly. In Scratch. We used a wait block to make the running order of our code from top to bottom much more obvious. In Python, we can also code a short delay with asleep function. Unlike the print function, the sleep function isn't included by default. So we have to do a little trick to import it. Don't worry about how this works for now, just follow along. The sleep function is part of a module called time. So let's code import space time to import the whole module in the background. Back in the editor. Let's make a bit of space at the top of our script by making a new line and typing the import keyword space and time to import the time module. Now we can code Time dot sleep and some parentheses. We can specify time in seconds in the parentheses. Let's go for two seconds. Because we're using a number this time we don't need the quotes. I'll copy and paste this code to delay each one of our five print functions. Now we can save and run the module again. Keep an eye on the console. As the first dialogue. The second, third, fourth, and fifth. It's hopefully really clear now that code runs line by line, block-by-block, or step-by-step. We saw that we could control that flow a little with timing. But that's certainly not all we can do without code. I mentioned that we could also make our code repeats itself or skip lines or blocks, depending on what's happening in the program. Will see all of this in action in the next few lectures. Just before we wrap up here, let me prove to you that code also runs left to right. Let's code another print statement, but this time we'll forget to add the final letter T. Watch what happens to the color of the code in the editor. Python is reading the code left to right as we type it. And it helps us by changing the color of the code when we use the correct syntax. When it recognizes the print keyword, it changes. Color, is purple at the moment, but without the final t on the right-hand side of the word, it's black. If we run our code with a mistake like this, it will throw up an error message in the console and stop before executing the next line. And there we go, a name error. Python's telling us the name porin is not defined. We don't need to worry too much about that right now. It's just important to note that the next line of code, the print command with the Get ready dialogue didn't run because we hit an error. We'll talk about bugs like this soon, but for now, this concludes our lecture on how code runs. I hope you've enjoyed it and I'll see you in the next lecture. 10. Bugs: Encoding, a bug is simply a mistake or a problem with our code. Debugging is the practice of eradicating bugs. There's no black art to debugging. In fact, more often than not, it's just a case of using our software over and over again to find all the things that are wrong with it. A bug could be a simple typo, like typing a nine when we meant to hit the letter I, is syntax error. Like forgetting to add closing quotes where we should have done. Or unwanted behavior, where the code is just doing something we don't want it to, like making it repeat itself forever. This type of bug can be particularly hard to spot because the code is technically working. And by that I mean that it's using the correct syntax. It's just not doing what we want it to do. I'll demonstrate this in a moment. As a coder, it's really good practice to test your code frequently as you write it. If we don't test as we go, it's all too easy to finish a marathon coding session with hundreds of lines or perhaps thousands of shiny new code, hit Run and encounter a bug. Lots of bugs, especially when you're new to coding. Bugs might be really obvious, but they might not. And if they are one of those nasty, hard to figure out types, you could be going through hundreds of lines of code line by line, trying to debug the problem. So top tip, as you move forward and start coding for yourself later on in this course and beyond. Be disciplined and test frequently. Write some code, stopped to test. It's doing what you wanted to. And when you're happy, ALL is looking good. Continue. Let's go ahead and see some bugs in action, as well as some of the handy tools that we code is have available to us to reduce bugs in our code. Hi everyone. So here I am back in Scratch and I'm going to hit the Create button here to start a blank project. And I'm going to go ahead and for good practice, as always call this project bugs. Now, Scratch is one of the best coding languages to learn because it's been specifically designed for beginners. And I mentioned three main types of bugs, typos, syntax errors, and unwanted behavior. Now, because scratchy uses ready-made code blocks like this, Say block here, for example, which you can simply drag and drop from the Block Palette into the script area. It's really hard to make syntax errors. This say block here, for example, is already made for us by the team at Scratch. They've given us this block. And behind this block is the actual code that runs. But because, say, is pre-define its pre-written, we can't edit this, say, we can't really make a mistake. We couldn't make a typo here in this keyword, say, and stop our code running and stop this block being recognized. Scratch will always know that this is a say block and it will always work as such. We can type our text here into the say block. And we don't have to add parentheses to type our text into like so. We just type whatever text we want into this text window and it displays on the stage. We don't have to remember to add quote marks around our string data like so. We can do. And they'll display on the stage. Or we can leave them off. And they'll display, or at least texts will display perfectly fine. Scratch will literally display whatever we type here in the window on the stage. Of course, we can still make typos. If we're typing strings. We could type something like for example, h, e, l, l. And instead of hitting O, we hit the 0 key by mistake makes our Hello text look a bit funny. But of course it will still run. So here, there's no syntax error. The code will run, but there is a typo and perhaps some unwanted behavior. We're getting a string that we don't really want on the stage. And I can prove that it will run, of course, as I'm sure you can guess. If I look for the green flag clicked event and run our code, we get the string hello with a 0 instead of a no. It didn't stop our code running. Well, let's say we want our sprite to say a random number rather than the string hello. Well, what we could do is we could go to the operators menu here. And we could look for this, pick random one to ten block. And we can drop that straight into our SE block here, like so. And don't worry, I'll cover operators and this random operator particular further on in the course will cover operators in detail. But for now it's just important to note that scratch lets us easily add code blocks within other code blocks. And we can do this with many types of blocks. Making scratch a pretty powerful language. To demonstrate another simple typo bug. Well, let's say we wanted our sprite to pick and say a random number between 0 and 10. Well, we could change this one to 0. For example, we wanted to go from 0 to 10, but we forget to type the second 0 and we actually just leave it as 01. Now again, our code would run perfectly well with this typo. If we run it here, we have one that's run it again. One again and again, 0 this time and again one. And as we start testing out program in earnest, which I hope we'd be doing often. Well, we'd probably notice that sprite only picks zeros and ones and spot our little typo error here and fix it. When we realized the problem and fix it back to 10, we do a little bit more testing. We got a seven this time. Okay, That's looking better. So we fixed our typo syntax errors. On the other hand, where we do something that's outside of the rules of the language or in other words, something that the language is not designed to do are to all intents and purposes, generally speaking, eliminated in Scratch because every block is pre-made with the correct syntax. But it is definitely possible to do something that the language is not designed to do when it comes to combining blocks of code in a larger script, particularly when it comes to order and speed of code operations. And this is where we can do some of our best learning here in Scratch. Let me demonstrate how we might easily create two bugs while trying to do something very simple. And this is where the boundaries between syntax error and unwanted behavior may become a little blurry. But of course that doesn't really matter. Bugs are bugs and we want to get rid of them, don't we? So, don't worry about memorizing these bugs. I'm about to demonstrate this is certainly not an exhaustive list. I just like to give you a few examples so that you can see that bugs on nothing to worry about. An all part of the fun of learning to code. Don't worry about breaking things. Try really hard not to let bugs frustrate you because we all make mistakes. And mistakes are easy to make. They really are just an everyday part of being a coder. And I personally believe that making mistakes is one of the best ways to learn. So let's say we want to make our sprite here, ask the user for their name, and then come back and greet the user. You may remember doing this in Python back in section 2 of the course. So we'll start with our green flag clicked event. And for now, at least let's remove our SE block. And this time let's go to the sensing menu and look for the block that says ask and wait. Now the nice thing about this block is that when it runs, it will ask the user whatever text is here in the text window and wait to sense a response from the user. When it does sense that response, IE, the user submitting an answer, Scratch will automatically store the user's input, the user's answer here in this little answer block, which is right below the ask block. And I can drag it onto the stage here. So whatever the user submits will become stored data here in this answer block. So that's really handy. This is something called a variable, which we're going to explore soon in the course. So next, let's grab a say block from the Looks menu again. And we can use our little trick that we've just seen to put code blocks within code blocks. Let's drag our answer block over into the say block, like so. Now Scratch should say our answer. Will, our sprite should say our answer here on the stage? And we know it's good practice to test frequently. So let's do that now. Let's stop our script from before. And run our script again. And here we go. Scratch cat has asked What's your name? We now have an input field that has appeared here on the stage where we can type our name. So I'll type Greg. And we can then press the check and I think you can hit Enter as well to submit your answer yet there we go. And our sprite says, Greg, Fantastic. So this code works well, but simply saying the user's name back to them is a bit odd. So let's greet them properly by using a join operator. If we go to the operators menu and we look for the join operator, we can drop that into the say block. And if we do that, our onset block is automatically ejected here from the same block. But now we can put our answer back in to the second window there on the right. And we can say something like, Oh, hey, and a space like so. And now this text string should be joined with our answer and then said on the stage. So let's give it a go. Let's run our script again. What's your name? I'll type Greg again. Submit the answer. Oh, hey Greg. Perfect. So that's looking good. But what happens if while we're coding these blocks? We spot something interesting going on out of the window. Or we see something interesting on TV, or we start thinking about what we should have for dinner. And we make a little bit of a mistake because we get distracted. And that's easily done when we start to have lots of code here in the editor, it can get quite busy. So we can accidentally snap blocks in the wrong place and get them in the wrong order. So what happens if we do that? And we ended up coding something like this by mistake? Well, you can see we've made an error here, but let's pretend we've made this error. We don't know we've made it yet and we come to test our script. So here we go, keep an eye on the stage. What's your name? We're expecting to see that, aren't we? I type Greg and I should now get my greeting. That's weird. Nothing happens. What's going on here? Well, we have to stop and debug at this point to try to find out what we did wrong. So we come back to our code. And of course we may immediately spot the fact that we've placed our code in the wrong order. But after we notice this, we may also wonder, maybe you're wondering, why are say hello or rather are say, Oh, hey, and our answer. Well, why didn't this code run before we asked the user for their input? You may notice if we run it again, we get what's your name? So where's our SE block? Where where did that go? What happened there? Is it running? It doesn't appear so. So is this block of code broken? Because we don't actually have an answer yet because our answer is blank. But hang on. If it is broken, then why did our program not stopped running? Why didn't it break at this point? This point here. How come we got the what's your name on the stage? This code did run here in the ask block. So perhaps not, perhaps it's not broken. What's going on? Well, let's debug this for our own learning. But how can we debug this? Well, how about we force the code to wait between our SE and are asked block. We could leave a break here, of course, and tested again. But how about we quickly go to the events menu and grab a wait block and force our code to wait three seconds between the say and the ask block just to see what's going on. Let's test again. Here we go. Oh, hey. And then what's your name? And the user input? So let's, let's actually remove the wait block a minute. And let's just leave a gap between our SE and our Ask. Try again so that it leaves this on stage. Oh, hey, and then a blank and then nothing. So our SE block are say code is running even though it doesn't have our name yet. In the answer. It's just being overwritten immediately by the ascii code. When these two are connected up. Our, I forgot to think about speed of code operations. Now we saw that in the last lecture, didn't we? So that's good to know. Our code is running, and now we actually know something else about our bug. Having an undefined value here in our answer. We don't have the answer data yet. Well, it didn't stop the say block from working. Scratched his leaves the answer blank here in our screen. It's got nothing to join with. Oh, hey. So we get an empty string. Well, we get nothing joined onto the right of this string and we simply end up with, oh, hey, hit just leaves the answer blank. Well, because the answer is blank, we haven't set it yet, or what we call defined it yet because our code here is in the wrong order. So that's something else we just learned about. Bug. We certainly didn't want this behavior, but our code still ran and scratch didn't crash. But one little mistake. Putting two blocks in the wrong order here. Actually created two bugs. A missing our answer and the text not having time to actually appear on the stage. Learning how to reduce unwanted behavior and write code that makes our sprites do exactly what we want them to do, teaches us key coding principles that apply just as frequently. And other coding languages like Python or JavaScript, for example. Scratch is perfect for this because it reduces typos significantly and virtually eliminates syntax errors. So we can really concentrate on learning how code behaves without some of these other bugs getting in the way. So great job team. And before we wrap up the lecture, let's head over to idle and see what happens when we hit about like this in Python. So you can go ahead and close scratch down. Now. Feel free to keep this little project for reference later. Or you can go ahead and delete it from your my stuff folder if you wish, as we won't be using it again. Okay, so here we are back in idle and I've just opened a new text file to use. Now when it comes to text-based languages like Python, and we're writing every character of our code from scratch. Every single character matters, including all of the spaces, tabs, the brackets, parentheses, the colon's, And the semi-colons, put one single character in the wrong place, miss one out. We use too many spaces and you'll hit a bug. So it's a little easier to get typos or syntax errors than it is in Scratch. But that shouldn't put you off. That easy to fix. And the more you practice the syntax that fewer bugs you'll get. Code editors also have tools to help you out. You've already seen the color changing in action here in idles text editor. Here's idle recognizing some Python keywords just to refresh your memory. Here we're about to recognize the print keyword. And in this case, idle gives us a nice purple or magenta color. What about the import keyword we've used? And there we go. We go from black to an orange color here in idle. We can also try an if keyword. If happens to be an important keyword in Python, which you'll see a bit later on. So is the word else. Another helpful tool here in the idle editor is the hover tips. When we type the first bracket on a print statement, for example, idle shows us the correct syntax for completing the statement. Don't worry about what all that means right now. I'm just demonstrating its usefulness. And you'll come to really appreciate that later as it helps you to avoid nasty bugs. So what happens if we run our Python code with a bug? Well, let's go ahead and save the code. We'll give it a temporary filename. Save it in my documents folder. And then let's run the, run the module. And we get an unexpected EOF, which stands for end of file while parsing error. Also, the problematic line of code is highlighted, which helps us to debug it. Short while ago, we imported an add-on module called time, so that we could use the sleep function to create a delay in our code. What if we try to import a module that doesn't exist? Let's try to import a module called rubbish. Again, we'll save our code and run the module. In this case, we get a slightly different error message. Python has detected that we tried to import a module because we use the import statement. But the module not found error tells us that there's no module called rubbish. What if we're writing some code to check if it's raining outside and remind us to take an umbrella. We could write something like if it is raining outside. Print umbrella. When we run this code, we get a name error telling us is raining outside is not defined. This is a really common type of bug amongst beginners. You'll see why it's called a naming error when we get to the lecture on variables. But simply put, we haven't told Python whether it's raining or not. We haven't defined that yet. So it can't go and check. In code. We have to define things before we can check them. So there you go. We've seen a few bugs in Scratch and a few in Python. The main takeaway here is that bugs are nothing to be worried about as a coder. You'll probably get loads of them in the beginning. I know I did. But with a bit of practice, you'll be avoiding them in no time. That concludes this lecture on bugs. Thanks for watching, and I'll see you in the next one. 11. Pseudo Code: in the coming lectures and practical exercises were going to be using pseudo code. Pseudo code isn't a riel programming language. Rather, it's used to represent real programming languages, using simple, everyday language that anybody on the team could understand. By definition, pseudo code is an informal code that doesn't require any strict programming language, syntax or underlying technology. What that means is that pseudo code can be pretty much anything you want it to be, as long as it helps you to think about programming using language that you or anyone in your team can understand. It's extremely useful for creating an outline or rough draft of a program, as it consumer rise that programs flow before going into A with the underlying technical details. And it can help to make choosing the most suitable coding language for your program that much easier. Here's some pseudo code that we might use to plan a program that asks students what they think of this course. It's written in plain text, using words which help us to visualize the programs flow here with thinking about a program toe. Ask the user if they enjoyed the course, collect their response, store it and then output some text, depending on whether or not they enjoyed it. We could use this to play around with a few different ideas before we decide exactly how we want the program toe work. Because we're only pseudo coding at this stage, we can use any key words that make sense to us when the time comes to code the program up for real. The important keywords can simply be replaced if they need to be using keywords from the chosen languages. Syntax, for example, we might decide the best language for this program is python in Python. The ask and store keywords don't exist, but there is an input keyword in python To get the functionality we want. We could write this code toe, ask the user question, collect the answer and store it. The if and else keywords also exist in python so we can use them. Python doesn't need to then keyword, as the output will happen without it. And here we can replace our pseudo code word output with the python keyword print notice that we had to add a little more syntax like the parentheses and semi colon. But now we've got some really Python code ready to run as a coder. Working from pseudo code like this, rather than a vague description about how things should work, is a much, much more productive and efficient way of working. Our pseudo code here was text based, but we could easily use something a bit more visual. Here's some pseudo code that we might use to plan out a game. This style of pseudo coding closely mirrors the visual style of code we see in scratch, and I'll be using this start a lot in the coming lectures. 12. Decomposition: decomposing we're breaking tasks down into smaller tasks is a key principle in coding. Decomposing helps coders to plan their work, estimate, effort and reduce bugs. Imagine we have a task to get ready for work. Giving this task to a human would probably produce a predictable result, as most people have done it before. But that's not the case. If we're instructing a machine, we can't just right get ready for work as it's far too ambiguous. Let's decompose are high level task of getting ready for work into smaller, more granular tasks so that we better understand it. The first sub task was subroutine might be toe wake up. Perhaps the next sub tasks would be something like get out of bed, have breakfast, brush teeth, leave home and go to work. Breaking larger tasks or problems down into their component parts is an important part of decomposition. Just as important, though, is building things back up again when we better understand the sub tasks we need. In order to make the larger task happen, we can start to think about the details and plan for them. According May coding. Our wake up sub task might require some detail around the time getting out of bed mania is to specify which side having breakfast might need us to define what to eat. Brushing teeth could involve a manual versus electric decision leaving home. I call for some front door, back door logic on going toe work may need us to specify walking versus driving as we recompose. Perhaps using pseudo code, we can look for any steps that we've got in the wrong order or any that we might have missed, like getting dressed that could have been interesting or locking the door on the way out. Of course, that was a simple example, but professional code is used decomposition on real world software all the time. Almost every piece of software, app or game that you might build can be thought off as a high level problem or task with many lower level tasks in order to solve that higher level problem. And it's often the case that every mid level task, like Sign Up has many lower level tasks or questions that need to be carefully considered before the first line of code is even written. For example, when thinking about our sign up code should we allow social log in should we accept user name or email or both? Decomposing isn't so much a strict rule or methodology. It's more a way of thinking. For coders, the code of mindset is a mindset of breaking things down, asking questions, solving problems and building things to make users happy. If your keen to start thinking like a coder, perhaps you can start decomposing everyday tasks in your head like the next time you get ready for work, we'll make dinner. Try to think about all the sub tasks and how you would create a step list of instructions or an algorithm. If you were writing it up as a program, this way of thinking will prepare you for coding up largest systems like software, APS and games. And it helps you with problem solving. We'll also get some practice at this when it comes to making our rap a little bit further on in the course 13. Commenting: There's a universal principle that code should be written in such a way that it's easy to understand on its own. What that means in practice is that code should be clean and well structured so that it can be easily read and understood by somebody else. Remember when we looked at this CSS code, when we considered the art of coding and decided that it was much easier to understand than the same code written without the visual structure of line breaks, spaces, tabs, and indents. However, there is another tool we can use on top of our well-structured code to improve it even further. It's called a comment. A comment is a special snippet of code to explain another part of the code. The machine doesn't run comments that only for humans to read. Each language uses its own syntax to comment out or hide strings from the machine. Think about comments like notes for your code. You might add notes for yourself or others to read, to jog your memory or to make the code easier to understand. Let's say that we want to add a comment to our CSS code on the left. Here, we've zoomed in on just a part of that code. We could add a comment like this. To remind us that header H1 is styling the link at the top of our homepage. If we're new to CSS, that could be a helpful comment. Or perhaps a note to tell other developers who work on this code not to change the color because that's important for our branding. Css syntax uses forward slashes and asterix to create comments. Whatever we type between them is commented out or made invisible to the machine. If you ever want to see just how useful comments are. Don't work in a project for six months and then come back to it again. Chances are you'll stare at the code for a good while trying to remember what you're trying to do. Now imagine you've inherited an old project from someone else and never even seen the code before. Trying to figure it all out can be a serious effort. So that's the theory. But let's take a quick look at commenting in action. Hi team, and welcome back to scratch. Now, imagine that you've just been given this code that you can see on screen by a colleague who's asked you to do a code review. Code reviews are common in the coding world, as it's good practice to ask others to check our work for errors or possible ways to improve our work before we go ahead and release it to users. Now, on first glance, this code appears to be well structured, but is it immediately obvious what this code is doing? Now, I'm sure you can see some code here in our script that you haven't seen before. Some blocks we haven't used before. And you may immediately be wondering, wow, that looks knew, what does it do? I don't understand it. So wouldn't it help if there were some comments here to help you along in understanding this code, perhaps to give you an idea about what this program should do. A high level overview, for example, and maybe some detail to let you know which parts of the code should be doing, what. That might help you to understand and test it and maybe even to do some coding on it yourself in collaboration with your colleague. So what we code is quite often do in these situations. If we don't have too much to go on and the code, while we have a first glance, look at it and it doesn't quite make sense to us. We like to just get our hands dirty, dive in and play around. I mean, what better way to find out then to just run the code and see what happens, right? So let's do that. Let's go ahead and run this code to see it in action. Here we go. So let's start. Okay, and to begin with, our sprite is saying what's your name? Okay, we can see that code block here. And we can see then are the sprite should say something back of this is familiar. We've seen code like this before. Okay, So let's, let's go. Let's say my name is Greg. Okay. And then our program says, Do you want to know my name? Enter Y for yes or anything else for no. Okay, that looks like this OSCON weight code block here. And then we have some code here inside of this orange block that perhaps we might not quite understand, but I get what it should be doing. So we should enter Y for yes and any other key or anything else for no. Well, all right, let's try it. Let's enter Y and submit that. Hey, it's scratchy. Okay, that's great. So I can see that text here in the say block. So it looks like this code ran and that appears to be the end of the program. Okay, Well, brilliant. Hmm, I wonder what happens if we enter anything else other than y? Let's run our code again. Say my name is Greg. This time let's enter. Well, let's enter. No, or in fact, let's just end to end for fun. Here we go. Okay, Have a great day. Interesting. So this, say block here Ran. Okay, Have a great day. And this one's saying our sprites name, It's scratchy. Well that didn't run. All right, so that kind of helps me to make sense of this. Let's see. If the answer is equal to y. Then say it's scratchy. Else, say, Okay, Have a good day. Well, thankfully this code being visual is nice and clear for us. It's quite easy to understand just with a bit of trial and error. And we've immediately perhaps learned something new here in Scratch, we, we've learned that we can do some control flow inside of our program and do something or something else based on a certain condition. Well, while we're learning just from looking at somebody else's code, that's really useful. But I mean, wouldn't it be great to have some comments here to really help us along the way. So let's just change hats for a moment. Let's imagine this was our code that we've just written. Now we kind of know what this code should be doing. So what if we were to comment this code? If we were going to share with a colleague, what might we want to write? Well, what would we ever appreciated if it was us reading the comments and seeing this code for the first time. Now I mentioned doing like a high level overview as a comment to give somebody an idea about what the program does in general. So that might help. So creating comments here in Scratch is very easy. You can comment on a particular code block at any point in the code. All you need to do is right-click on it. So let's start with our green flag clicked event here. That's right at the top. So that's perfect for our high level overview. So I've right-clicked and we can now click on Add Comment. And we get a little post-it note type comment here with a little line indicating what it is or what part of our code we're commenting on. And we can close that comment down, collapse it, and expanded again. And if we decide, well, we don't really want to comment here, we've made a mistake. We can just hit the delete button to get rid of it like so. So let's add a comment here and say, say something like this. Short program asks the user for their name, greets the user, and then asks whether they would like the sprite's name. It then finishes with either the sprite's name or a farewell. Basically a goodbye type greeting, which is our Have a good day part. That's generally and we can change the size of these comments, by the way, by dragging the bottom right of the window here. That's generally what this program does. So that's a nice little high level overview. So let's collapse this comment now and add a bit of detail. Now, it may be an assumption to think that the first, let's call it the first part of our program here is fairly a Haiti, use the word obvious. But we've seen this code before. And if we know that our friend, for example, has used, these are ask and say blocks before. We don't need to write a comment for that. That's just an intuition. Think you just need to think about who it is that you're leaving this comment for. But if we know that our friend hasn't seen this type of code before. Well, it might help us to explain the second half of our program. Let's just explain here with a comment and give some detail. So let's say this is a conditional code block. It checks the value of our answer and decides which. Say code to run. And we can then say, if the user enters the letter Y, the sprite says name. And if the user enters, any other text, string, fish bright, wishes them a good day. Like so, I hope that would makes sense. And I've got a bit of a spellcheck coming up on there. Maybe we should say a sprite says its name on it except that it doesn't like that either. Let's just say, I don't like leaving red squiggly line on the screen. So let's say this bright says. And we can just enter the text here exactly as it appears. Why not? The only issue with that is if we ever decide we want to change the text a little bit, the logic may stay the same, but in fact then I'll comment would be slightly wrong, but hey, I'm getting into too much detail. That doesn't matter. I think that's fine for now. So if we were to share this or rather officials shared with us at the beginning, I would hope that these two comments here would help me to help us all to very quickly get a handle on what this code does and start to understand the detail. And that's really what these comments are all about. Comments are super useful for serving two purposes. One, they can remind us about what our code is doing. Particularly when we come back to our code after a long delay or a length of time, we often forget as coders exactly what we were thinking at the time. We forget some of the logic, we forget why we made certain decisions. And it's helpful to come back and remind ourselves. And the second part is we Coders love to collaborate. We love to share our code with other coders and try to improve and get rid of bugs and do all of those things with our programs. And comments are just a great way for getting other people up to speed explaining our thinking. Adding some supporting information, background information, perhaps some links to other examples of code, links to certain websites we might find useful. There are all sorts of reasons why comments like this could come in handy. So now that we've seen commenting here in Scratch and you know how to do it. I hope you continue to make use of it yourself. And I think it's about time we dive back into Python to see how comments work in that language. See you there. Here we are back in idle. And I've written the same program in Python. The syntax is a little different, but essentially this code is doing the same thing that we just saw in Scratch. I've already saved the module, so I'll go ahead and run it. First of all, python asks for our name. When we enter a user input, it greets us and asks if we want to know its name. I'll go ahead and answer with the Y. And it says it's pi. Let's run it again. And this time to test the code, I'll enter something other than y. And my second response, that's enter n. And there we go, we get a slightly different response from Python's saying, fair enough this time. So let's go ahead and start adding some comments to our code. Now that we know it's working nicely. In Python, we can use a hash character to add a comment. Let's clean up the code a bit and add some comments. Now, start with a hash character and let's type first dialogue. And then to make things a bit clearer, let's organize our code a bit better. And we can add another comment here saying second, oops, second log. Let's get the spelling correct. And actually hello and username. And do you want to know my name are part of the second dialogue. They come at the same time. So that looks better. And we can add another comment here saying check user response for the third dialogue. That's well structured, well-organized, and a few comments make our code a bit easier to read for anyone else coming to our code for the first time. It's also worth noting that we can add comments to the end of a line. Rather than above the line. For example, rather than above username. If we remove this comment, we can add it to the end of this line. And you can see that it works just as well because Python is reading our code from left to right. So when it hits the hash character, it knows that everything on the right of that is a comment. Perhaps we'd like to add a few lines up at the top of the program to introduce it to others. Let's go ahead and do that. Now watch what happens when I make a line break to continue the comment on a second or third line. Only the first line is commented. Python thinks that the next two lines of code that we want it to run. In Python, the hash syntax only comments out a single line, anything right, of the hash character. To comment all of our notes out, we need to add a hash character to the start of every line that we want to comment out. For example. That can get a bit messy though. And thankfully, Python gives us another option here. We could use triple quotes at the start and the end of the comment to comment out the whole block. This is called a block comment and it's really useful. Imagine that we like our else dialogue down here. But we think that we can improve on it. Rather than say, fair enough, it may sound better to say something like, okay, let's move on. Let's test that. Hm. Do you know what I actually preferred? Fair enough. But now I can't go back to using that because I've deleted or overwritten it. Let me go back to how it was before we changed it and look at how we could have used commenting as a trick to make life easier. What we could have done was add our new idea into the code and comment out the old idea. Let me demonstrate. Now the machine won't run the old code, but we haven't permanently removed it. So if we decide that we want to go back to it at any point, we can simply uncomment that code and comment out or remove the other code. These are just a couple of ways that coders make use of commenting in code to make life easier for ourselves and others. Of course, we'll be practicing commenting on our own code for real when we begin work on our app project. For now though, that's the theory of commenting. I hope you enjoyed seeing this concept in action, and I'll see you again in the next lecture. 14. Common Data Types: When we begin to code, we quickly run into various types of data. Common data types include numbers, by quantities, times and dates. Strings of characters, like sentences, messages or labels, and values like true or false, which we call Booleans. As data is all around us in the real-world, we frequently need to deal with it using code. Coding. Very often attempt to solve real-world problems, as in a mobile banking application, where we need to deal with numbers, times, dates, and Booleans such as whether transfer has been received or not. Even in gaming, we use numbers to keep score strings to show the storyline text. And Booleans such as whether or not the player is still alive. One of the most powerful aspects of coding is that it allows us to manipulate data quickly, accurately, and in many different and creative ways. We can use code to assign, set or create data, modify or updated, compare or check it, and store it for use later to name just a few things that we can do with it. When you start learning a specific coding language, it's defined. Datatypes are normally one of the first things you cover, as there are usually some small differences between languages. But the three main types that we've just mentioned of very common. Let's take a quick look at them. As mentioned, the number datatype is incredibly common and allows us to use code to measure or manipulate things like amount, scores, times, and dates. When you start dealing with numbers in code, you'll eventually run into the terms integer and float. An integer is a whole number, for example five, whereas a float contains a decimal point. For example, the number 3.14. In some languages like Python, integers and floats are actually separate data types and require a little bit more care with the syntax. Whereas in JavaScript they're not. Strings, sometimes called literals, are also a very common datatype. The name string comes from the fact that this data type is a string of characters. And you'll see why people sometimes use the term literal in just a moment. Here's a string. This one is a message to prompt the user to create an account. And here's another. This one specifies exactly or literally watch the pair on a byte. Now button. As you can see, strings can be made up of letters, numbers, spaces, and other characters, and they usually wrapped with single or double quotes. The Boolean data type has a strange name, but it's simply a value that's either true, false. Actually, the name comes from British mathematician George Boole, who created Boolean algebra, which is the basis of all modern computer arithmetic. Booleans are incredibly useful in coding because they can be used to check various conditions and control the flow of a program based on those conditions. For example, we might want to check if the player has reached a safe point in our game. If true, the game should save. A false, again can continue. Perhaps we want to write some code to check whether we should let the user of our mobile app modify their payment information. We need to check whether the user is currently logged in. If true, they should be allowed to update their credit card info. If false, we should prompt them to login. Will look at control flow using Booleans and a lot more detail shortly. For now, just remember that even though the words true and false are text, booleans are not strings or a datatype, all of their own. High Team and welcome back to scratch. I've just started a quick project here called datatypes and written a very short script. You may recognize the logic going on here in this script As it's pretty similar to what we've seen in previous lectures. I'm just using some different dialogue here to ask the user whether or not it's raining, tend to yes or no. And then to say something or something else on this stage. I've just done this to demonstrate very quickly that we've actually seen, or you've actually seen all three of our primary main datatypes that we use encoding here in Scratch already. Whether or not you've noticed it yet. We have been using strings, numbers, and Boolean values so far in the course here in Scratch. For example, this ask block. The text, is it raining? Well, this text here, and I'll try and highlight it. Well, this is a text string. This is string data. And string data is also called literal data sometimes so you may hear the term string interchanged with the term literal. And it's called a literal because it will, or the program will literally use whatever text string you type here and display it on stage. If we run the program, you can see it asks, Is it raining? Enter yes or no. And because we've put little ellipsis here with the three dots, it appears on the stage. We've used quote marks here around some of our words and they appear we've used a special character, this question mark. And that appears if we were to wrap our entire string here with quotes like so which we don't need to do with Scratch syntax. Although in Python we do. And in fact, in most coding languages, we would need to wrap the string data inside of double quote marks like so. Well, if we run again, this time, we simply get the text with the quote marks on the outside, literally as we type it. The answer here that we're looking for is also string data. So if we run the program again, when the user types yes, that typing a text string and then submitting the text string into our program. We're storing that string as a value inside of our answer block. And we're then checking if the answer which is a string, happens to match this text here, which is also a string. Then this condition is true and this code will run, oh no, I'll bring my umbrella. And what about number data? Well, we've used some numbers before here in Scratch. And how about we change our code a little to make use of the number datatype. How about if we were to answer anything other than yes? And run this code here. If the weather's nice, or at least if the weather's not raining, we could say, let's have an ice-cream. How about we do something here with a number? Let's grab a join operator. And let's take our text here and insert it into the left of our join operator. And let's say something like, great, Let's have an ice cream. I'll bring, and we can leave a space there. And now what shall we say? We want to use a number. So let's say, let's say we want to pick a random amount of money to bring to pay for our ice cream. So let's pick a random number between, let's say 15. We want to say something like, great, Let's have an ice cream. I'll bring, let's say three. I'm in the UK here, so our currency is pounds. So I'll bring three pounds to pay for it or something like that. Well, actually, what we need to do is we need another join operator. So we can add a join with inner join like so. And then we can pick random one to five. And in fact, where I've left a space here, I actually need to put a currency symbol for me, that's the pound symbol. And then no space after that, it's important where we put our spaces inside of a literal text string. Then we're going to insert some number data. And then we can say, I think we need another space at the start of this one. And we can say it's on me or something like that. And of course we need to test this to see if it's going to work. So what we can do now is we can drop this join operator in like so. And let's test our code out. Is it raining? We're going to say N for no. Submit our data. Great. Let's have an ice cream. I'll bring five pound. It's on me. Excellent. So that joining of string data worked out quite nicely. But what we're actually doing here is Scratch is inspecting number data. There's pick random. This is using numbers and behind the scenes, this is number data. And what we're actually doing is because we've got number data being joined into string data. The number that scratches picking, well, in this case it's picked five is actually being converted behind the scenes by scratch into the text string five, which allows us to join text strings with more text strings and ultimately create one long text string. So this five is being converted to text, converted to a string and joined. Which is perfect. That's giving us some really nice behavior. So that's strings and numbers. And what about Boolean values? Have we touched on those yet? Well, take a look at this if, then else block and look at our condition here we're using something called an equality operator, which we're going to get into very shortly. We're going to look at operators in more detail. But we're checking whether something on the left is equal to something on the right. In this case, if our answer is equal to the string yes. So what's the answer to this expression? With this question here inside the green code block, what's the answer to this? Does our answer equal yes is a question? And the answer is either yes or no. If it, if the answer is yes, These things are equal. This code here to say, I'll bring my umbrella runs. If this answer is equal to something other than yes, our expression. The larger question, if you like, comes back as a no. And in this case our OS code runs and we get the, let's have an ice cream text. So behind the scenes here, well there you can't see it. There's Boolean data happening inside of our program. How could we see it though? Is there a way we could display the answer to this wider question, this equality expression we call it, Can we actually make that visible? Can we see it? Well, let's try something. Let's try something. What we could do, we could separate our code like this. We could take a say block. And we could simply say answer equals yes. Now, there's a bit of a mismatch here in the shapes. Scratched uses these visual cues, these shapes to help us quickly see where code is supposed to go, if you like where it's designed to go. But that doesn't always mean that this diamond shape won't fit inside of this more oval code block. It's a visual cue, but it's not always a very strict rule. So in this case, let's see if it works. Ha, it did work. Perfect. So what happens now if I get rid of this code? We keep it nice and simple. What are we expecting to see here? Well, let's stop our code and run again. Now if I enter, yes. What are we going to see on stage? Ha true. Were you expecting that? What if we run again? And this time I enter? No, false? Isn't that interesting? There's our Boolean data. Because this expression here, our answer equals yes, has to evaluate to either true or false. This is Boolean logic in action. And interestingly, the words true or false here, strictly speaking, end up as strings here on the stage. But behind the scenes, false and true or not actually, strings when they're run here inside of this code block, the Booleans, which is a whole separate data type to strings. It's just that we can convert a Boolean true or a Boolean false into a text true or a text false string. So now you've seen all three datatypes here in Scratch. Next up, let's head back into Python and explore some datatypes inside of our Python code. Let's code another simple print statement in Python. And this time, let's code directly in the shell as will only be coding and executing one line of code at a time. And we don't need to save our work. We can print number data, which doesn't require any quote marks. String data, which does require quote marks. And we'll print whatever we put between the quote Literally. For example, if we add a load of spaces and we can print Boolean data, for example. Notice that I'm not printing the string true, which would have quote marks wrapping it. And in idle would be green. Although the outputs do look the same, the above example is recognized as a Python Boolean datatype because it's the keyword true with an uppercase T. Let me demonstrate what would happen if we use a lowercase t, which isn't recognized as a Python Boolean data type. We're not using quote marks. So you can see that true is black. Python thinks this is just a word. Actually, Python thinks that this is a named variable, so it will produce an error. And we get a name error because the name true is not defined. We haven't created a variable called true. And we'll see how this works in more detail. When we get to the section on variables, we can use one of Python's default functions called type to show us that type of any data that we give it. Type the keyword type with parentheses on the end. If we put any data between the parentheses, Python will return or tell us that data's type. Now let's see what happens if we add a number. Sure enough, Python is telling us that this data type or class to use the correct terminology is INT, which stands for integer. Let's try 10.5. And this time, Python is telling us that this data is a float. Do you remember me saying that integers and floats are separate data types in Python, whereas that's not the case in JavaScript. How about something like hello world? And sure enough, this is string data. And what about true? Notice that I'm using the capital T and checking the color changes to indicate the correct syntax. Sure enough, this is Boolean data. And there you have some of the most common data types or classes in code. Now that you know about the various datatypes, keep an eye out for them in the coming lectures. Especially the Boolean type, which is used so frequently to control the flow of programs. Whenever I'm coding Booleans, I like to think about those zeros and ones and how the code is working, just like a logic gate or transistor to control what happens next. I hope you enjoyed this lecture on common datatypes, and I'll see you in the next one, where we will play around with some basic arithmetic using code. 15. Arithmetic & Operators: You may recall from our coding in a nutshell lecture, that doing arithmetic quickly was one of the main reasons why computers were invented in the first place. So it makes sense that code can help us to do maths. We can easily use code to add, subtract, multiply, and divide numbers. And you'll find that most languages use the same characters. Addition uses the plus character. Writing 100 plus 200 in code would predictably evaluate to 300. Once we've used code to calculate answers, we could output them to the console so that we can check their correct, put them on the user interface, as in the score. And again, we'll save them temporarily to use somewhere else, as in the running balance in a mobile banking app. So the app can notify you if your balance reaches a certain limit at some point in the future. Subtraction uses the hyphen or minus character. Multiplication uses the asterisk, and division often uses the forward slash. And it's not all about maths, of course, operators aren't only for numbers. For example, we can use the add operator to add strings together. Here, we've added the string Hello to the string world to give us a new string. Hello world will play with this concept in a moment. We can also ask questions like, is x larger than y? Or of these two things the same using comparison operators in our code. Here are some of the common comparison operators that you'll see in the coding world. Greater than, as in is the number 200, greater than the number 100. Which would evaluate to true. You'll hear the term evaluate a lot in the coding world. One of the nice things about a coding language is that we can write down an expression like 200 is greater than 100, and it will automatically check or evaluate the answer for us. In this case, it calculates that 200 is indeed a larger number than 100 and returns the value true. Do you recall what type of data this is? If not, I'll refresh your memory in a moment. We can also use the less than operator, as in 100 is less than 50, which would evaluate false. Greater than or equal to, as in 100 is greater than or equal to one. Another. True expression, less than or equal to, as in 40 is less than or equal to 40. Another true expression, not equal to, as in 2005, is not equal to 20.5. Yet another true. Notice how operators produce Boolean data. That's really useful because if we know whether something is true or false, we can use that data to control the flow of a program. We'll see how this works when we get to control flow a little later on. We can even go a step further using logical operators to ask questions like our x and y greater than 0. Logical operators include and as in are the numbers 5.5 and 10, less than the number 50, which would evaluate to true. And the operator. Or as in, does the string dogs or the string cats equal the string birds, which would of course evaluate to false. You may have noticed the double equals sign that I used here in this expression to check if either the string dogs or the string cats or equal to the string birds. That's not a typo. This is called the equality operator. And there's a reason we don't use a single equal sign when asking if things are equal in most coding languages. I'll explain that shortly. But first, let's go ahead and try some arithmetic. In Scratch. The green operators menu here gives us all of the options we need to start experimenting with arithmetic. For example, we can start with an event to start our code running. And just for the sake of pure variety, how about we start with a different event? Let's start with when this sprite clicked, which would trigger our code to run when we actually click on whichever sprite is here on stage, or whichever sprite has this script attached to it. In this case, it's our sprite one, our cat. So when we click on our cat, how code can begin running. Now we know we want to try and operator, but how do we see that data? Well, we need to output it to the stage. So let's grab a say block. And that can output some arithmetic onto the stage. And let's start with the addition or plus operator. And how about we say something like 100 and then use our operator to do the arithmetic for us, say 100 plus 100. All we need to do now is click on our cat sprite. And there we go. Scratch doesn't have any problem doing math. We get our answer of 200. Here on the stage. Which encoding terms is this expression? Evaluating to a value of 200. This is number data and more number data. Scratch is capable of performing arithmetic operations with number data. It's combining these two number values together. And an expression is code that can evaluate to a certain value. And in this case our expression is evaluating to the number 200, which behind the scenes is being turned into the text string to a 100 and then joined or inserted into our SE block and put on the stage. So that's what's going on in coding terms here, behind the scenes. And using our neat little trick to combine code blocks inside of other code blocks. Well, we can combine operators together. So say we wanted to do something like this and take a subtraction or minus operator. Say something like 100 plus 10, minus 5. Where we do something like this and we have two operations going on. It's still true to say that code performs one operation at a time. Code works step by step. Something we call synchronous operation, performing one operation at a time, one after the other. And of course that doesn't necessarily apply to quantum computing, which is really in its infancy, at least at the time of recording, unless you're watching this in the year 2013, which case they've probably taken over the world. Quantum computers can perform operations at the same time, which is kind of crazy if you start Googling that and getting your head around that. For me, that's just black magic. But for now, with our traditional shall we say, computers one operation at a time. So where we do something like this and have one operation and another operation going on. It's important to note which operation will happen first, something called the order of operations. And that follows standard mathematical rules for the order of operations. And as a bit of background, perhaps you might want to just briefly pause the lecture right now and go ahead and Google mathematical order of operations. See what that brings up. And it should give you a list to tell you the order that operations are performed. Traditionally in mathematics. Well, coding languages almost always follow that order, so it's predictable. We can work out whether scratches going to do the 100 plus 10 and then subtract 5. Or whether it's going to do the ten minus five and then add 100 plus five. Now in this case, both will output the same answer. So it doesn't really matter. But in many cases where we're using multiplication, division, addition, subtraction, the order does become very important. So let's go ahead and test this code. And I've actually forgotten, I'm not using the green flag. Let's go ahead and click on cat. And there we go, we get 105. Now, multiplication and division work in exactly the same way. So we can do two times 50, for example, to get 100. Or I didn't want to do that. Let's do just to keep it super simple. 100 divided by two to get 50. And here in Scratch, we also have operators for greater than, less than and equality or is equal to. And you may notice that these are a different shape to our other addition, subtraction, multiplication and division operators. These ones are oval. Whereas the greater than, less than and equality, this diamond shape. Can you think of a reason why that might be? Well, these operators greater than, less than, and equal to evaluate to a boolean value, true or false. So we could check that by doing something like 100 is greater than 50. And that's true. Or perhaps something like this. 1 is equal to 50. Well that one's false. And note that in Scratch, this is important to note. Scratch uses a single equal sign for equality here in this equality operator, 1 equals 1 is equal to 50 with a single equals sign. Now this is actually unusual. In most coding languages. A single equals is not checking whether something is actually equal to something else where the two values are equal or not. The single equals sign is usually reserved for setting a value to something. And in most coding languages, including Python, you'll see a double or even a triple equals sign, which is usually used for equality. Javascript will even go a step further and allow us to do a loose or a strict check for equality. And that's more technical. I won't go into that now, but that's why you might see a double or triple equals in JavaScript. The important takeaway, the important thing to remember right now is here in Scratch, checking whether two values are equal as a single operator. But that is not the case in Python. There's a big difference there, and we'll get to that shortly. Now, what if we wanted to do something like less than or equal to? You've seen that operator, I'm sure many times elsewhere in the non-coding world and less than or equal to operator. And you'll note that there's only a greater than, less than and there's no less than or equal to operator here in Scratch. So could we do that? Well, I'll give you a clue. In fact, how give you the answer? Yes, we can. There's a workaround. There's a way to do it using the code blocks available to us. We can use another one of these operators here to achieve that using some code in code. So there's a great opportunity for an impromptu pop quiz. How about I set you a quick challenge and let you have a think about it on your own before I give you the answer. Just for fun. So how about we wanted to code something like 100 is less than or equal to 100. And run that as an expression to check whether it's true or false. How could we code, say 100 is less than or equal to 100? That's the challenge. How to think about it. Maybe have a bit of trial and error, see how you get on. And if you're going to do that, pause the lecture now and then rejoin when you think you have the answer, or perhaps when you get stuck. Either way, I'll come back in and help you out shortly. Okay, so here's how I would do something like that. I would remove the equality operator for now. And I would use an OR operator to check whether one thing or another thing evaluate true. So we could say 100 is greater than 100, or 100 is equal to 100. So this code should now check whether 100 is greater than or equal to 100. If we run our code, we get true. Of course, because this is a true expression, 100 is not greater than 100. So this left-hand side would evaluate false. But the right-hand side, 100 is equal to 0 and 100 would evaluate true. And R or expression only needs one side to be true. To finally evaluate true, which we get here. Great work came up with a solution to that on your own fantastic job, you definitely have earned yourself a nice big slice of cake and a cup of tea or coffee. So here in Scratch, our operators give us enormous power. We can perform almost any arithmetic operation, giving us the ability to write all sorts of logic. And of course, we might not want to always say the answer to our expressions here on the stage, we may well perform these kind of arithmetic operations for different types of apps that we're building. We might be building a game and we want to calculate the players current high score. Perhaps their current school. We might want to work out whether to give them boosts or level ups or calculate the speed that we want to change the sprite to based on certain conditions. There are all sorts of real-world applications that require the use of arithmetic and logical operators. So hopefully that's been a nice still intro into that area of coding. And now let's head back to idle to see arithmetic operators in action in Python code. Okay, we're back in idle and let's use Python to coat some arithmetic. Again, rather than code in a file, we're not going to save any of our work and we'll only be using single expressions. So let's just code directly in the shell. 100 plus 100 evaluates to 200. We can also write an expression like 100 plus 10 minus 5, which gives us a 105. Let's code ten multiplied by ten. And we get a 110 divided by ten. It gives us one. In actual fact, we get 1 afloat. The expression 1 is less than 10. And I got a syntax error because I added a space by mistake. Let's try again. One is less than 10. And we can add spaces between the operator and the numbers in our expression, but not at the beginning of the expression. And you can see that one is less than 10, evaluates to true. But I can also type 1 is less than 10 without the spaces in between. Also true. But as soon as I add a space that at the beginning, I get a syntax error. What about 10 is less than or equal to 10? And other true? How about ten? Is not equal to 10? This time, false. What about is equal to or equality? We can check if data is equal using the equality operator, which happens to be a double equals sign. 100. Double equals 100 evaluates true. 100. W equals 101 evaluates false because those two parts of data on not equal. So why can't we use a single equals sign? What in most programming languages, the single equals sign is used to assign rather than Czech data. As you'll see in the upcoming lecture on variables and storing data, we frequently need to assign data to name things. For example, we can name a place where we want to store some data. Let's call it my school, and then assign a number to it. Let's assign a score of 300. Think of it this way. Single equals sets data and double equals checks data. Let's hit Return and see what happens without code. Well, we go straight to another command line. What's actually happened in the background here that we haven't seen is Python has created somewhere called my score. Think of it as a place for now. And then assigned a value, a number value of 300 to my score. It stored it temporarily in the background, ready for us to use in our program if we wish. Now that we've set my score to 300 with the single equals or assignment operator. We can print my school to prove that we created and set it in the background in our code. Let's say print my score. Let's hit Return to execute this line. And there we go. Python outputs 300, which has been temporarily stored in my score. Now we can check equality. We can ask the question, is my score equal to 300? And we see it is with the boolean true. If we ask whether my score is equal to 100, sure enough, we get false. We can also ask the question, does 100 plus 200 equals 300? For example? Here's the code. And other true. The way the computer will deal with this code is to perform the calculation 100 plus 200, store the answer temporarily, and then compare that answer to 300. For equality, 100 plus 200 is less than Two, 150 evaluates to false. Let's combine comparison and logical operators to ask a few more questions. One 100. And, and I'm using the keyword and you can see that it's turned orange here. One are both greater than 10. Evaluates false. And don't forget, we can also use operators on string data. If we print the string hello plus the string world, we get HelloWorld. And notice that because we print literally whatever is between the quote marks or had to add a space either before the w of world or after the letter O in hello. And again, it's not all about arithmetic. Operators allow us to code powerful applications that can ask real-world questions. We could use an operator to check if the length of a user's email address is greater than five characters, for example, or whether it contains the act character. We could ask the question, does this username and password equal the username and password stored in the database. And we can use them on Booleans. For example, has the user uploaded either a picture or a video to our app in the last 30 days. With code at our disposal, the opportunities are endless. And that brings us to the end of our lecture on arithmetic and operators. Hope you enjoyed it and I'll see you in the next lecture. 16. Loops: Because coding is all about writing instructions for a machine. And machines can't reason in the same way that we humans can. We often have to write the same code over and over again as our program goes from the top to the bottom. Let's imagine that we're coding up a new app to help first-time users to succeed at the gym. We're coding a weightlifting demo and we want to make our virtual instructor complete 10 repetitions. Let's pseudocode it. We could code ten reps one by one. We'll code the first one and simply copy and paste the rest. Which is much faster. But it still leaves us with a lot of code. Having lots of repetitive code has a number of downsides. One, it's not very clean to look at and could make the code much harder to read. Two, it makes the file size larger. And three, if we ever want to change the code, we'd have to change it multiple times wherever we've copied it, which is inefficient and commonly leads to bugs. In the real-world, or gym instructor would probably demonstrate the perfect rep and simply say, now do 10 of these. And that's exactly what we can do as code. Using a loop. A loop is a special bit of code that repeats over and over again. As a coder, you usually have a number of loops at your disposal, depending on the chosen language. And each one is designed to meet a specific need. Let's look at some common loops using pseudo-code. A repeat loop simply repeats a given number of times. And until the loop repeats, until something else happens. For example, run until you cross the finish line. While loop repeats, while something else is happening. For example, while a burger exists, eat it. Useful to refuel before our next Jim session. A for-loop repeats for as many items there are in a given list. For example, for every day in the week, go to the gym. When coding loops, we need to be really careful to specify the stop condition. Or we'll end up with an infinite loop. An infinite loop is a loop that will run forever or at least until the computer crashes, because it either has no stop condition or the stop condition is never met. I'll demonstrate this in a moment. As always, encoding. There are often many ways to accomplish the same thing. How would you get our virtual demonstrator to the top step? We could go manual. We could use a repeat loop, perhaps an until loop or a while loop. Even a for loop would do the job. Right now we have all of the options available to us. But in reality, there's usually a clear winner based on how we structure the rest of our code. We'll be using loops in our app. But before we do that, let me go ahead and demonstrate some loops in both scratch and Python. So we can see what they look like for real, both visual and a text-based coding language. I Team and welcome back to scratch. Let's code some loops to see how they work. And I'm also going to take this opportunity while we look at loops to introduce you to movement here in Scratch. Because I'm sure you'll be keen to start experimenting with moving sprites around the stage when you play around in scratch on your own, in your own time. So let's imagine that we want to add some animation to our cat sprite here on the stage using code. Let's say we want to make our Scratch cat move over to the right of the stage and then back to the center as if he or she is pacing right and left while perhaps thinking about what to do today. Okay, now, notice just below the stage here and above the sprite area or the top of the sprite area to the right of the sprites current name. You can see these little left-right arrows with an x and up, down arrows here with a y next to them, and to Windows currently set to 00. Now, Scratch uses a very common technique to control visual movement using a grid system. Now the grid itself has x and y coordinates. The x coordinate here defines the sprite horizontal position on stage. So the cat is currently at 0 x, which is the horizontal position, right and left. And why defines the sprites current position vertically? So the cat is also at 0 y, which is the up or down position. If I manually move the sprite, you can see the x, y coordinates automatically update. So if we go top right of the screen, for example, the coordinates are now 188 x and 123 Y. If we go towards the bottom right, we're at a 190 x and now we are minus 122 y minus because we've gone down. If we go bottom left, we're now at minus 192 x because we've gone left and we're minus 126. Why? Because we've gone down and top-left. We have about a 186 or minus a 186 x and about 125. Why? If we manually set 00, we manually move our sprite back to the center of the grid or the center of the stage. Because scratches grid is zeros to the center of the stage. In some cases, 0 X 0 Y could represent the top or the far top left of the stage like so. And in many cases, when it comes to grid coordinates using code when we're talking about computer monitors or computer screens, graphical displays, 0, 0, y is often the top-left and not actually the center of the display. But here in Scratch 000 is dead center. So if we happen to know the coordinates on the stage that we want to move a sprite two at any particular time. We can easily write some code to move our sprite programmatically using given code. And it's so armed with this knowledge, let's say we want to write some code to make the scratch cat move to the right and then move back to the middle of the stage again. So if I manually move the cat to about where I'd like him or her to end up. Well, if we go back to 0, why? It's somewhere around a 190 or maybe let's try 200 x 200 experts are cat close to the very far right of the screen, or I should say of the stage. So that should work for us as a reference, we want to move to about 200 x and then back to 0. Now I'm not sure if you noticed, but if we manually go back to 00 like so. And then we look at some of the numbers here and these so I'm in the motion tab at the moment. So these are the blocks that control motion or movement of the sprite. Notice, for example, this block here that says Go to x 0, y 0. Well that's the default value. Well watch what happens if I manually move the sprites to roughly the position I want? Notice that the numbers here inside of our go-to, they've automatically updated to match what's here on the stage. So that can save me remembering I need to go to 200 x. Then I have to grab a code block and I have to manually enter 200 here. Those value can already be set for us. That's just something to note. But we wanted to 100 x and 0 y in this case. So I was close enough on that attempt, but not quite right. Let's return our sprite to 0 x 0, y. We've now got this go to x, y code block ready-to-use. So how about we give this code a test? Let's grab an event and just for a change, how about we start with a when key pressed event and yeah, space is fine. We'll leave the default space key pressed here. Now, watch the sprite here in the stage, and I'm going to go ahead and run our code by pressing space. Here we go. And just like that, our sprite immediately went to position to a 100 X 0 Y, the far right of the stage. And the sprite just basically teleported there, just jumped there. Well, that's what the go-to block does. But what if we want the sprite to move more slowly as if he's walking to a given position. Well, scratch gives us a few options for motion. The go-to x, y is not the only code block available. So we could replace that with something like a glide to XY block. And the glide block allows us to define the time it should take the sprite to complete its glide. From wherever it currently is to our given coordinates. So if we manually reset here to 000. And let's go for something like, I don't know. Let's go for three seconds just as a bit of experimentation. Watch what happens when I press the space key this time. Now that's much nicer. Now a cap is slowly gliding to position to a 100 x. Nice. So what if we want our sprite to glide back left to the center of the stage again. Well, we can simply add another glide block. So here's a little trick. If we right-click on the glide block and duplicate, we get a second one, a duplicate block. And this time, let's take another 30 seconds to glide back to its position. Zeros 0. Now of course, to see this from the top, I'm going to have to manually reset the sprites starting position. And here we go. Let's hit the Space key off to the right. And look what's happening. We're moving back to the center of the stage again. But I think if we run it again, it looks a bit strange that our cat facing right. And then he goes backwards, facing, still facing right, but moving left back toward the center. I think it would look nicer if our cat actually turned to face the direction he was traveling. Well, notice also that down here, underneath our x y coordinates, there's a direction setting. If we click on that, we can see that we get a visual representation of a 360 degree circle. It's currently set to 90, so that's 90 degrees, which is facing right. 180 degrees is straight down minus 90 degrees is left. And 360, or in this case, 0 degrees is straight up. So we wanted our sprites to face left and that's minus 90 degrees. But look what's happened to our sprite has ended up upside down. Now we don't want that Dewey, but not to worry, Scratch allows us to set a rotation style for this sprite. And you can see it's currently set to all around. But there are some other options like left, right, and, uh, do not rotate. So if we were to use something like the left-right rotation style, well then anything from down here, a 180 degrees through to 0, simply faces left. And as soon as we go into this side of the circle, 0 up to a 180, we face, right? So that's more of the behavior that we want. So you can see now if we go to minus 90, our sprite is facing left. So let's return our sprite too. Positive 90, that's where we want our sprite to start facing direction 90. But now we've set the rotation style. What we can do is we can get a point in direction block and put it here after our first glide, before the sprite moves left. And we can simply reverse that now by setting minus 90, like so. Let's give it a test. As the space key. Off to the right, we go, we flip to face left, and we come back to the center of the stage, ending up facing left. But note that now our sprite ends up pointing in the left direction. So if I hit space and run our code again, our cat actually goes backwards this time doing a pretty cool cat moon walk. So how can we fix that? Well, how about we make sure we always set the direction to 90 degrees at the start of our script. So let's grab another point in direction block, put it at the start and set it to 90. If we run the code again, cat immediately turns to face the right direction, turns to face left and comes back to the center of the state, which is the behavior we want. So now we have some code, we have full code blocks here, a short script to give us the behavior we want. This code has become repeatable. We could use it again if we wanted to repeat the movement of the cat moving right to left, right to left as if he's pacing. Well, we could now duplicate our code because this has become repeatable. So we could essentially just coded again underneath. Now rather than recreating this block-by-block. There's a bit of a trick we can use. We can right-click on the top block. Will the first block that we want to duplicate, right-click on it and hit Duplicate, and it will duplicate everything below it. So essentially we get a carbon copy of our code from wherever we click on Duplicate. And we could repeat our code like so. It started running again. But this time, note that there's our first cycle, and we now get a second cycle of movement. There we go. And of course, we can keep going. We can do 345 as many cycles as we like. Now in a way this is perfect. But there's an issue here, isn't there? Of course, the more we want our sprite to pace, the more code we have to add. So imagine doing this ten times or even a 100. Things would start to get very messy on stage. And of course, multiple lines of code simply means more opportunity to introduce bugs. More chance to perhaps have a typo somewhere. Imagine if we wanted to change this behavior. And we wanted our sprite to take two seconds. There we go, two seconds, but It's still three on the way back. The next loop is still going to be three on the way off to the right and then three. So now we have to put two seconds in four places rather than just two. Very easy to make a mistake somewhere. We could easily miss that. If we change the coordinate, for example, and we wanted to go to a 180 x, well now we have to change that in two places rather than just one, and so on and so forth. More opportunity for bugs. And there's another challenge. What if we want to set the number of times to pace right and left? Dynamically. What if sometimes we want our sprite to move right and left three times and on other occasions five times. Well, we'd be stuck because we're hard coding the number of repeat cycles of code we're using here. This is set in code, it's called hard coded. It doesn't change. Every time we run our script. We'll get this behavior. So there's a much better way to achieve this behavior using a loop. Let's take a look at it now. So let's remove our duplicate code here. Code we don't need anymore and get rid of that. And now let's look for a suitable loop that we can use to get the behavior we want with as little code as possible. So let's look at the control menu. And you can see here when you look a bit closer, that scratch gives us at least three loops that we can use. There's the repeat, a number of times loop, the repeat loop. There's a forever loop. And there's a repeat until loop, which will repeat until a certain condition is met. Let's try the repeat loop first. Now if we drag the repeat loop over our code block, like so, we can see that it will automatically stretch to incorporate everything in our code block. If I let go off the left mouse button now, it's simply wraps all of our code. Of course, another way to do this, we don't want to use that method would be to take this code block out here in blue. But our loop in and then drop our code inside of the repeat loop, like so, both methods work, so the choice is yours. But either way, you can see that this block of code here in blue, which you may think of as one large block of the four smaller movement blocks, is now indented inside of our repeat loop. So all of this code will repeat. How many times do we want to repeat? Well, just for testing purposes, Let's go for three. Oops. And I started the script running. So in fact, let me just stop in a moment. And because I actually clicked on the script that actually started running. But let's get ready and I'll press the space and then let's see what happens. And in fact, now I need to go back to position 000 manually to reset. Okay, here we go, bit of a hiccup there, but let's test. So here comes the space key and watch for the number of loops. So there's 123 and we stopped after three. And in fact, just for testing purposes, let's change our glide speed to 1 second. Just so we don't have to wait so long when we're testing and future. And of course, the nice thing is now I only had to do that in two places rather than how many would it have been? Well, it would have been six places if we had this repeat code hardcoded three times. So we're already benefiting from our loop. Now the code itself is much neater. And if we want to change anything in our code where we only need to do at a minimum number of times. And that's the repeat loop working nicely. So what about the forever loop? Well, the forever loop is pretty self-explanatory. It's simply a loop that will run forever. Let's hit space and away. We go a bit faster this time. And notice that we just keep going and going and going. It just runs forever until we manually stop the code running with the stop flag here at the top. And notice when I do stop the script, it stops wherever it currently is in the program. So if we start it again and we stop it here, in fact, that's the wrong key. Let's stop it here. There we go. We end up on the far right. If we start it again and stop it, here, we end up in the center. So when we stop the script, it stops wherever it's currently running through our code, wherever it is, line by line in our code. The repeat until loop will run, will repeat until a Boolean condition is met. Now if we leave that condition blank, R code will still work. If I press the space key, away, we go and we are repeating. But we now have no defined stop condition. Our code we'll just repeat forever, essentially becoming a forever loop, which is also known as an infinite loop in the coding world. And this will probably, well, almost certainly result in a bug in our code. That's behavior that we don't want, at least we don't want it in our finished code. So let's add a stop condition. We could go with just about anything we wanted here for our stop condition. But as we haven't covered storing data and using that data inside of our program later on. We haven't covered that yet. So let's just keep things really simple for this demonstration and look for a user input on the keyboard to stop our code looping or stop our code running. So from the sensing menu, let's grab another key pressed event. And rather than space, let's look for a different key to stop our code. Let's look for something like, Well, how about we use an S to remind us that S is representative of stop? And let's just take a second to consider how our code will run. To start off with the Space key is pressed. We will then go to our loop. The program will look for whether or not the S key is pressed. If it's not, this expression will evaluate to false, which means that we're going to enter into the loop. And our blue movement code block here will run. Everything inside the loop will run top to bottom. In order. Once all of this code has finished executing, we get to the bottom of our loop. We're simply then going to go back up to the top and scratch. We'll evaluate our expression yet again. It's going to check once more whether the S key is pressed at this moment in time. If the S key is still not pressed, we go back into the loop and again repeat the cycle. If at this point that the expression is evaluated at this point and only at this point if the S key is pressed, this condition will evaluate to true and we will immediately exit the loop. The code will not, this blue code will not continue to run anything else in our program. Well, so let's give our code a test. Let's press the space key. Away we go. And if I press S, now, nothing happens. Now, nothing. Now, nothing. I have to press the Esc key at the exact moment that this expression is being evaluated. So one way to do that is to hold down the S key just to check that it does work. So if I hold it down at the point when the cat comes to the very center of the stage, we should see our loop finish. So I'm going to press the Esc key and hold it. Now. And there we go. At the moment that our repeat until loop re-evaluated our expression or Boolean condition, it came back true, which means that we stop repeating. Now this is just one of the quirks, if you like, of using a sensing element for the stop condition, it has to be sensed at just the right time for it to work. But to prove it does work and to prove that we do actually successfully exi