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

32 Lessons (7h 58m)
    • 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. Conditionals

    • 18. Variables

    • 19. 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 - Part 1

    • 27. Project: Scratch Proof of Concept - Part 2

    • 28. Project: Python MVP - Part 1

    • 29. Project: Python MVP - Part 2

    • 30. Project: Recap

    • 31. Course Summary

    • 32. Bonus Content

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

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 2018!

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

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

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 [email protected]

Happy learning!

See full profile

Class Ratings

Expectations Met?
  • Exceeded!
  • Yes
  • Somewhat
  • Not really
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, let's get you set up in scratch. If you already have a scratch account and you've used it before, please feel free to skip this lecture. Well, I'll be covering at this point is creating a scratch account for the first time and introducing you to the user interface. So if you're already familiar with scratch, you're not going to miss anything. Scratches. A Web based application created by M. I T and designed specially to teach beginners how to code as of 2018 is being used by over 28 million beginners to create fun games, interactive stories and animations. The first thing we're going to do is create a free scratch account. It may help you to pause this lecture and switch tabs as you follow along. Go ahead and open your favorite Web browser. Or if you're already watching this lecture in the browser, just open a new tab. Go to www dot scratch dot m I t E D u. If you want to, you can run a search to check that this is the official scratch website. Look for a button that says Joins scratch. Click on the joint scratch button. Go ahead and complete the short sign up process, and you may need to confirm your email during that process. If you need to hit, pause to go and do that Now, please go ahead as you need to be logged in on scratch to follow along with the rest of this walk through. Okay, you should not be logged in on the scratch site, and you can see up here in the top right that I'm currently logged in. You can see that the home page has a section for news projects that people have shared with the community, featured studios or creators and stuff that other people are generally enjoying. It really is a great community. What we want to do now is learn how to create our own scratch project and take a first look at the user interface. Look for the create button up in the top navigation bar. Let's click through to the Project Editor. Now that we're in the scratch editor, I'll run you through all of the areas that you need to know about in order to begin your mastery of scratch. This big area here on the right is this script area where we'll do our actual coding. This area is the scratch equivalent of the text editor that we used on Idol to write our python code. Up on the top left is this stage. We will see the result of our code running. This is scratches version off the console scratches built primarily for APS games and animations that use characters or actors on this stage is where we can bring those actors to life through code by default. We already have a character on the stage. The scratch cat in gaming, the term commonly used for characters or images like this one is the term sprite, So our cat is actually a sprite. Bottom left is the Sprite area where we can create an edit. All of the sprites that we need for a particular project and in the middle is the block pallet. This is where we'll find all of the code blocks that scratch gives us to use in our project . Code blocks have different colors, depending on what they do. For example, these blocks that control motion are blue, while blocks that control sound are pink. You can use the menu at the top of the palette to switch between the various types. The idea behind scratch is that we can drag blocks of code into the script area and snap them together to create scripts or snippets of code which control our sprites. Let me demonstrate. Let's say we want to make our program. Make the cat say hello and move to the right. Well, scratch gives us a couple of handy controls. Appear in the top right of the stage, a green flag on a red stop button. The green and the red buttons can start and stop our code running. So let's go over to the events tab. Look for a when green flag clicked code block and drag it over into the script area. That's how easy it is that's actually looking a little bit smaller. My display. Sit down on the bottom, right of the script area. There's a button to just increase the size. Hopefully, you can see that a bit more clearly. Now. The next thing I can do is go back up to the looks tab and find a code block that makes the Sprite say something for a period of time. We want our cat to say hello so Let's drag this block over and snap it into place once the blocks air snapped together are Coble run in sequence, line by line, step by step or block by block. If you prefer saying hello for around about two seconds should work. We can now go over to the motion tap. We want to make our cat move to the right, and let's use a block that says Move a number of steps. This will make us bright move. Now I know from experience that the Sprite has to move in a certain direction and can move in a certain speed. But we don't need to worry about that right now. We just want to make him move to the right, and I know that he's already pointed to the right also know from experience that around about 100 steps should work for this quick demonstration. Well, there we go. We've got a nice script over here in the script area, and our sprite is ready to go on the stage. So let's run out code and see the result. And there we go. Our cat said hello and moved to the right. That's how easy it is to get started with coding and scratch. If you're wondering how to save a project in scratch, it couldn't be simpler as you can see up in the top right of the toolbar. Scratch auto saves your project frequently while you're working on it. It's good practice to give you a project to name here on the top left just above the stage so that you can easily find it again. I'll go ahead and give my project to name. Let's call it temp one. All projects are saved into a folder called My Stuff, which you can access from the file menu of the toolbar here or by exiting the editor and clicking on the my stuff Tap from the home page. If you're coding along with me and you want to keep things tidy, you can go ahead and delete this temporary project. Now, as we won't be using it again now that we've got scratched, set up and ready to go will be using it a lot in the coming lectures where you'll have an opportunity to simply watch the demonstrations or code along with me. When we start working on that project in the next section, will come back to scratch yet again to build a proof of concept. And at the end of the course, you'll be free to unleash your creativity and create projects on your own. You may find a lot of inspiration from those community sections here on the scratch home page, but first, let's get back to our theory on explore how code actually runs. You may already have a very good idea from the python you wrote earlier in the course and from this short walk through. 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 and execute. Herbal file is a file or program containing your code that could be run by the computer. Generally all code is red 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 will get this output. I'll demonstrate this inaction 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 scratch ready to go, let's play around with it a little and see some code running for real Welcome back to scratch. Once again, I'm on the scratch home page and I'll go ahead and click the Create button toe. Open up a new blank project in the Scratch editor just for best practice. I'll give this project to name. Although we won't be using this code again, that's cool. It how code runs. Let's prove that code runs from top to bottom By recreating the dialogue that we just saw in this slide, Python used the print function toe output text to the console, and we've already seen that we can use a say block in scratch toe output text on the stage . We can find that over here in the looks tab. In fact, scratch actually gives us a few similar options. To achieve this. There's a code lock to say something and leave it on the screen. Let's code one of these and again, this is looking a bit small, so I'm going to zoom in a bit so we can see the block more clearly. I'll make mine say, Get ready. There we go. There's also a co block to say something for a defined length of time. We've already used this one and this is handy because it nicely demonstrates the delay between each block of code running. Let's code three of these. I'll make mine, say, three Oops to and one on. Let's go ahead and change the time to three seconds just to make that delay really obvious . There's also a block to make the Sprite think out loud, which also outputs text to the stage, but in a slightly different way. Let's code one of these. I'll make mine say Good to execute our script on demand. Let's coda when green flag, click block from the events type, thinking back to everything we've learned about how code runs so far. What do you expect to see when we click the green flag to execute our script? Well, here we go. Let's give it a try. Keep an eye on the cat on the stage. So there we go. Did you notice anything slightly strange about our script running on the stage? Did you see the dialogue to make the cat say, Get ready? Appear? I didn't Let's try it again, just to be sure. And yes, when we run, we go straight to three and then two and then one, we get into our countdown, but we don't see this. Get ready. Text. Something a bit odd is happening here. Let me stop the coat on. Explain what is actually happening. Is this first code block to say get ready is running. And then we're moving onto the second co block to say something. But it's happening so quickly we don't see this first text appear on the stage. This dialogue isn't visible to prove it. Let me just separate these blocks with a gap between them. This coat isn't ever going to run now and let's run the script again. So all that's happening now is the first code block is executing. Here we go and there's to get ready dialogue. But as soon as we connect them up, we're telling the machine that it can move straight onto this second line, which is basically just over writing the first. And if I run it again, we don't see it. One way to deal with this would be to replace his first say block with another type, just like the one we've used below toe. Hold this text on the stage for a length of time. In fact, scratch gives us another method that we can use. If we go to the control tap, you can see that scratch gives us an option to wait. There's a code block to wait for a given length of time and down in the bottom there's another code block to wait until and here we can specify or define something else happening or another event so he could wait until the cat sees a mouse, for example. But for now, let's just go back and select this one to wait for a period of time. So if we make some space and snapped this block in, let's say three seconds to make it nice and clear and there we go. We've got a new script. Hopefully, now this weight block will hold this text to get ready for this dialogue on the stage. Let's give it a test and there we go. We've seen that get ready. A nice pause and then our countdown executing perfectly. And those are just a couple of methods that we can use to control the flow of our code as it runs from top to bottom. As I mentioned, we won't be using this code again to feel free, to exit the editor and just to keep things nice and tidy. We can go to my stuff and we can delete the how code runs temporary project. Next up, let's see the exact same behavior. But this time we'll run it in python so we can see how it looks. Using text code. Here we are once again in idle Idol is actually an I D e or integrated development environment. It gets that name because it contains everything a coded needs to write, test and run code in one place. Think of it as the equivalent off the scratch website. Feel free to follow along with me by launching idle on your machine. You can see that Idol always launches with this shell window open by default, just as a reminder, because it's been a while since we used python last. The shell allows us to type inputs. It executes R 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 used the console for testing our code because we normally use code to build APS or games with a graphical user interface or gui for short. We often short in that term even further to just see you, I 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 Idol text editor. You can write code in any editor you like, as long as you save it with a correct file extension. So to be a coder, you certainly don't need any expensive tools. Idol 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. Syntax is the key word. Print followed by an opening and clothing parentheses. Python will print whatever we put between the parentheses into the console. In this case, let's start with a string of text 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 coated in scratch. We'll start by printing the number three and the number two 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 the file. We could go to file safe, and it's given a name. How code runs. You can see that it's automatically got the P Y or Python extension because we've used Idol . Now. If I was saved, we can hit F five all we can use the run command. Let's run the module and see the output in the council. And there we go. A python code ran executing the first print command. Then the 2nd 1 3rd 1 the 4th 1 and finally the 5th 1 running our code line by line one at a time. Again, the code executed very quickly in scratch. We used await 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 a sleep function. Unlike the print function, 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. Sleep Function is part of a module called Time. 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, typing the import keyword space and time to import the time module. Now we can coat time, not sleep and some parentheses. We can specify a 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, our copy and paste his coat to delay each one of our five print functions. Now we can save and run the model again. Keep an eye on the console. There's the first dialogue. Second, third, fourth on fifth. It's hopefully really clear now that code runs line by line, block by block will 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 coat. I mentioned that we could also make our coat repeat itself or skip lines or blocks, depending on what's happening in the program. We'll 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. It's 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 a Panera message in the console and stop before executing the next line. And there we go, a name era privates telling us that the name Prin 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 were 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 ALS the things that are wrong with it. A but could be a simple typo like typing and nine when we meant to hit the letter I It's syntax era like forgetting toe 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 wanted to do. I'll demonstrate this in a moment as a coder. It's really good practice to test your coat 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 buck or lots of bugs, especially when you're new to coding. Bugs might be really obvious, but they might not. And if they're 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 on beyond. Be disciplined and test frequently. Rights and code stopped to test its 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 . It's have available to us to reduce bugs in our code. Here we are back in scratch, and I've just created a new blank project to demonstrate some bugs in action. Scratch is one of the best coding languages to learn because it's been specially designed for beginners. I mentioned three main types of bug typos. Syntax errors on unwanted behavior because scratch uses ready made code blocks, which you can simply drag into your scripts from the block pallet. It's really hard to make the 1st 2 types of mistakes. Typos and syntax Errors. Take this, say block, for example, and I'll make it a bit bigger as it's already made. And we're not typing the keywords, say, or adding parentheses to put our number or text strings into. We can't really make a mistake. We don't even have to add quote marks. Of course, we can still make typos if we're typing strings, for example, we could type something like Hello and hit a zero instead of the letter O. If we run, the code would still see the mistake on this stage, but it wouldn't stop our code running. Well, let's say we want our sprite to pick a random number rather than text string. We can do this with an operator, will cover operators properly in another lecture. But right now, let me show you a neat little trick in scratch. Click on the Green Operators menu and drag a pick random block over into the say block. Scratch lets us easily add code within code, and we can do this with many types of blocks, making scratch pretty powerful to demonstrate a simple typo back, though, let's say we want our Sprite to pick a random number between zero and 10. We might miss the zero on the 10 and pick around a number between zero and one. The code would run perfectly well. But as we started testing our program, which would be doing often, I hope we noticed that it only picks zeros and ones and probably spot the era. Syntax errors are, to all intents and purposes, eliminated in scratch, as every block is pre made with the correct syntax. Unwanted behavior is definitely still possible, though, and this is where we can do some of the best learning and scratch. Let me demonstrate some unwanted behavior. Don't worry about memorizing these bugs, and this is certainly not an exhaustive list. I'd just like to give you a few examples so you can see that bugs are nothing to worry about. On all part of the fun of learning to code, a common error in scratch is to place code blocks in the wrong order. Let's say we want to make our Sprite ask the user for their name and then greet them. You may remember doing this in Python back in Section two. We'll start with our when green flag clicked event. And for now, let's remove our say block. Look at the sensing menu and find the block toe. Ask the user for an input and wait. The nice thing about this block is that it automatically stores the users. Answer temporarily here in this answer block. Next, let's grab a say block, and we can use the trick we've just seen to put code within code. Let's drag our answer block over into the say block. We know it's good practice to test frequently, so let's do that now. And I was bright. Ask for our name. Let's go ahead and give it an input. And when we run the input, How Sprite says my name. That works well. But simply saying the user's name is a bit odd. Let's greet them properly by using a join operator. Go to the operators menu and drag the block that says, join over into the say block and you can see it automatically removed our answer block. For now, what we can do is put the answer block back into the end of the joint block, and here we can edit the first part of our string Let's make it a capital. Hello, space on our answer on Let's test our code again. Let's try scratchy this time and there we go. Hello, scratchy. Much better. Okay, so that's looking good. But what happens if we're a bit distracted, which is easily done when we start to have lots of code in the editor and we accidentally get out two blocks in the wrong order? I'll run the code again. Keep an eye on the stage. So far, so good are spite is asking me for my name. So let me go ahead and end to that. Hm. Nothing is happening. We have to stop and debug at this point to try to find out what we did wrong. Of course, we may immediately spot the fact that we've placed our code in the wrong order. But we may also wonder why are say, hello Code didn't run before we ask the user for their input. Was it because the code can't run because it doesn't have our name yet? We might debug that just for our own learning, we could drop some weight coding. Let's make the code wait for three seconds just to slow things down a bit so we can see what's going on. Ah, say hello. Code is running, even though it doesn't have our name yet, it's just being over, written immediately by the ask code. That's good to know. We certainly didn't want this behavior, but our code still ran and scratch didn't crash. 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 applied Justus frequently in other coding languages like python or Java. Script scratch is perfect for this because it reduces typos and virtually eliminates syntax areas. We can concentrate on learning how code behaves without all those other bugs. Before we wrap up the lecture, let's head over to idle and see what happens when we hit a bug in python. You can go ahead and close scratch down on again. Don't worry about saving anything now unless you want to. 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 Coghlan's on the semi. Coghlan's put one single character in the wrong place, Miss One out. We're used too many spaces on your hit a bug, so it's a little easier to get typos or syntax Eris than it is in scratch. But that shouldn't put you off. They're easy to fix on the more you practice the syntax, the 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 idols text editor, his idol recognizing some python keywords just to refresh your memory. Here we're about to recognize the print keyword, and in this case, Idol 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. You can also try an if keyword if happens to be an important key word 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 typed the first bracket on a print statement, for example, Idol 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 file name, save it in my documents folder, and then let's run them, Run the module and we get an unexpected E O F. Which stands for end of file while passing era. Also, the problematic line of code is highlighted, which helps us to debug it. A short while ago, we imported an add on module called Time so that we could use to sleep function to create a delay in our code. What if we try to import 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 used 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 on, remind us to take an umbrella. We could write something like if it is raining outside, print Baretta. When we run this code, we get a name era 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 on a few in Python. The main take away here is that bugs are nothing to be worried about. As a coda, 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 there 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. He 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 H one A is styling the link at the top of our home page. If we knew 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 Ford slashes and ass tricks 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 are 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. Welcome back to scratch. Imagine that you've just being given this code 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 it before we go ahead and release it to users. The code is well structured, but is it immediately obvious what this code is doing? Let's go ahead and run the code to see it in action. Okay, so the Sprite asks, is frowned name? It's given input and it says, Do you want to know my name into why? Or End? Okay, well, let's try what I first, uh, when it says it's scratchy, seems good. Let's run it again and try the other input. It's always good to test both possibilities to make sure the code works properly. This time, we'll say, and for no fair enough. Okay, it's a nice little program, and we can see here that our friend has chosen to use and if then else block in scratch to add some decision making to the program so that house bright can decide what to say based on our input. We haven't used one of those yet, but we will get to them a bit later on. So back to our code review. What we could do is recommend that our colleague adds a few comments to make the review easier in future, especially if another reviewer is going to see it for the very first time. As we just have. Let's go ahead and add some comments now in scratch. Commenting is very easy to do. Right. Click on the code block that you want to comment on and click on Add comment. The comment window opens and away we go. We could add first dialogue to remind us that this is the first I log from our Sprite. Let's go ahead and at another comment that's add one to this block second dialogue, and what we can do is collapse these comments just to make life a little bit clearer. Let's add 1/3 comment, and actually I double clicked. This code is running at the moment. Let's stop it running and let's add a comment by right clicking on this block. Let's explain what this particular block of code is doing. Let's remind others that this code chooses the next dialogue. If the user enters, why give Sprite name or else if the user enters any, another key and why don't give this bright name. Okay, that's a bit long winded, but it does the job. Hopefully now anybody else coming to our code can just click on a comment, expanded out and see the notes on what this code is doing. And it may help us if we come back to this at a later date, and we've forgotten what we were trying to achieve. Well, that's it. This is a simple example of commenting in action, but that's how easy it is in scratch. And remember, there's no hard and fast rule when it comes to commenting. You can add anything that you think is necessary and useful to explain your coat or a fresh your memory. Now let's take a look at commenting in Python. 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 why, and it says it's pie. Let's run it again. And this time to test the code, I'll enter something other than why in my second response, that center and and there we go we get a slightly different response from Python, saying Fair enough this time. So let's go ahead and start adding some comment 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 ups. Second, I long, Let's get this spanning, correct and actually hello and use the name 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 user name. If we remove this comment, we can add it to the end of this line, and you can see that it works. Justus. Well, because pythons 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 with 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 this 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. Mm. Do you know what I actually preferred? Fair enough. But now I can't go back to using that because I've deleted or over written it. Let me go back to how it Woz before we changed it and look at how we could have used commenting as a trip 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, weaken simply a NCAA meant 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, will be practicing, commenting on our own code for riel when we begin work on our at 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 billions. 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 on billions, such as whether transfer has been received or not. Even in gaming. We use numbers to keep score strings to show the storyline text on billions, 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 a sign, settle, create data, modify, were 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. Data types are normally one of the first things you cover as there usually some small differences between languages. But the three main types that we've just mentioned are very common. Let's take a quick look. Them has mentioned the number data type is incredibly common and allows us to use code to measure or manipulate things like amounts, 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 Where's Afloat contains a decimal point. For example, the number 3.14 in some languages, like python interviews and floats, are actually separate data types and require a little bit more care with the syntax. Where is in JavaScript? They're not. Strings, sometimes called literal, are also a very common data type. 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. He is 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 watched appear on a buy it now button as you can see strings can be made up of letters, numbers spaces on other characters, and they usually wrapped with single or double quotes. The 1,000,000,000 data type has a strange name, but it's simply a value that's either true, both false. Actually, the name comes from British mathematician George Boole, who created Boolean algebra, which is the basis of all modern computer arithmetic. Billions 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 save point in our game. If true, the game should save. If false, the game can continue. Perhaps we want to write some code to check whether we should let the user of our mobile app modify their payments information. We need to check whether the user is currently locked in. If true, they should be allowed to update their credit card info. If false, we should prompt them to log in Well, look at control flow, using billions in a lot more detail shortly. For now, just remember that even though the words true and false our text. Billions are not strings. There are data type, all of their own. There is a data menu in scratch, which you may already have noticed. In fact, this menu gives us some scratch options for data, structures or ways of storing data like variables and lists on. We will explore these concepts incoming lectures. The data types themselves, though, are the numbers, strings and billions. You've already used the number type. For example, when we coded scratch to choose a random number, you've also seen strings in action. Every time we coated a say block, we've added text spaces numbers on special characters on. We've seen that scratch syntax doesn't need quote marks for strings, as it will print everything we type in the say window literally. Even if we add the quote marks, you may not have seen the true or false keywords and scratch. But scratch certainly is using Boolean data here, too. Take a look at the if statement. We've coded an operator to check if the user input is equal to one scratch will check or evaluate that statement when that block of code runs and it will evaluate to either true or false behind the scenes. Let's code another simple Prince statement in Python. And this time let's go 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 quite marks. And we'll print whatever we put between the quote literally. For example, if we had 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 an idol would be green. Although the outputs do look the same. The above example is recognized as a python Boolean data type because it's the keyword. True, with an upper case T let me demonstrate what would happen if we use the lower case T, which isn't recognized as a python booty and 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 truth, and we'll see how this works in more detail. When we get to the section on variables, we can use one of pythons default functions called Type to show us the 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 I n t, which stands for integer. Let's try 10.5. And this time Python is telling us that this data is afloat. Do you remember me saying that inter jizz and floats are separate data types in python? Where is that's not the case in Java script? How about something like hello world and sure enough, this is string data. And what about truth? 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 data types, 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 Boot Ian's. 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 enjoy this lecture on common data types, and I'll see you in the next one where we'll 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 toe, 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 code would predictably evaluate to 300. Once we've used code to calculate answers, we could out put them to the console so that we can check that correct. Put them on. The user interface is in the score in a game will save them temporary 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 ad 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 why? Or are 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 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 returns to value. True. Do you recall what type of data this is, if not our? 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 years, less than or equal to 40. Another true expression not equal to as in 25 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 Are X? And why greater than set logical operators include and, as in other 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 equal sign that I used here in this or expression to check if either the string dogs, all the string cats, are 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 gives us all the code blocks we need for basic arithmetic. Let's make a quick script to do some maths just for a change. Let's start with the when this Sprite clicked event to execute our code when we click on our cat. In fact, let's change our sprite from the cat to something more appropriate, perhaps a button of some kind to bring a new sprite into the project. Down in the Sprite area, you can see that we have those four options bringing one in from the library, painting or drawing a new Sprite. Uploading a sprite from a local file, all capturing one from our camera. Well, let's check out this scratch library and scratch gives us a number of Sprite to choose from , with some filters. Here on the left hand side, let's choose a green button. When selected, we can click OK on the bottom right and as bright is imported into the project. Great, where we've got a new Sprite, but notice that the code block that we've just placed into the script area is missing. This is an important lesson in using scratch. Each Sprite has its own script area. This principle helps us to write more modular code and scratch rather than having one huge script controlling everything in our program. So but in one are new. Sprite is blank, but Sprite one, our cat has our original code in it. And in fact, that stage also has its own script area. And there are different code blocks available in the block pallet, depending on which Sprite is selected. But we're not going to play with that right now. Sprite one or the cat has a code block already, and Button one has no code in it yet. If you want to, you can transfer code from one sprite to another. Let's drag the one code block that we've placed so far overrun to button one, and we can see that the code when Button one is selected has been copied across. Now we only want one sprite now to keep things simple, so let's go ahead and delete the cat. We can simply right, click and select delete. And now we only have the one sprite, the button in our program. Let's code. When this bright clicked, say 100 plus 100 recon, select the plus operator, snap it into the same block and then add on number data. And when we click on our button as Bright says 200 and we can add operators within operators to code something like, say, 100 plus 10 minus five, clicking a button to run and we get 105 multiplication and division operators work in just the same way. And of course, we don't always want to perform arithmetic purely to say answers onto the stage. We might need to use it to calculate things like speed boosts or level ups in again here in scratch. We also have operators for less than is equal to and great to then, and notice here that scratched does use a single equal sign for equality. But note that this is unusual. When we get to idle and python shortly, I'll show you why. Let's code. The expression one is less than 10. What do you think we'll see when we click the green button. And there you go. There's our 1,000,000,000 data again, in this case, true on notice here that scratch uses a lower case T as opposed to python, where billions always start with an uppercase letter. What about less than or equal to? There's no single operator to code that expression, but we can code it using code within code, we could use an all block to create an expression like 10 is less than 10. Oh, 10 is equal to 10. In this case, we're checking whether 10 is less than or equal to 10. Once again, a true expression. These operators allow us to do almost anything with code. Now let's head over to idle to see the same concepts in Python. 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 will 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 five. It gives us 105. Let's code 10 multiplied by 10 and we get 100 and 10 divided by 10 gives this one. In actual fact, we at 1.0 afloat the expression one. It's less than 10. Uh, and I got a syntax error because I added a space by mistake. Let's try again. One is less than 10. We can adds basis between the operator on 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 one is less than 10 without the spaces in between. Also a tree. But as soon as I had a space it at the beginning, I get a syntax error. What about 10? It is less than or equal to 10. Another truth, How about 10 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 equal sign. 100 double equals 100 evaluates True, 100 double equals 101 evaluates false because those two parts of data are not equal. So why can't we use a single equal sign? What, in most programming languages, a single equal sign is used to assign rather than check 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 school off 300. Think of it this way. Single equals sets data. Double equals checks. Data. That's it. Return and see what happens without code. Where we go straight to another command line was actually happened in the background here that we haven't seen his Python has created somewhere called My score. Think of it as a place for now and then a signed of value, a number value off 300 to my school. It's stored it temporarily in the background, ready for us to use in our program. If we wish. Now that we've set my school to 300 with 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. That's it returned 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 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 equal 300? For example? Here's the code. Another truth. 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 250 evaluates to false. Let's combine comparison and logical operators to ask a few more questions. 100 Onda and I'm using the key word, and you can see that it's turned orange here. One 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 Hello World and noticed that because we print literally whatever is between the quote marks I had to outer 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 riel world questions. We could use an operator to check if the length of the user's email address is greater than five characters, for example, or whether it contains theatric character. We could ask the question. Does this user name and password equal the user name and password stored in the database, and we can use them on booty In, 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 on 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 on 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 weight lifting demo, and we want to make our virtual instructor complete. 10 repetitions. Let's pseudo code it. We could co 10 reps one by one or code the 1st 1 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, would have to change it multiple times wherever we've copied it, which is inefficient and commonly leads to bugs in the real world, our gym instructor would probably demonstrate the perfect rep and simply say, now, do 10 of these, and that's exactly what we can do is 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 loop repeats until something else happens. For example, run until you cross the finish line. A wild loop repeats while something else is happening. For example, while a burger exists, eat it useful to refuel before our next gym session. Four 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 that 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. All 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 gym instructor to the top step? We could go manual. We could use a repeat loop, perhaps an until loop or a wild loop. Even a four 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 in both of visual, on a text based coding language, Welcome back to scratch on Let's code some loops to see how they work. Let's imagine we want to add some animation to our cats. Bright on. We want to make our scratch cat pace left and right on the stage. Now you may have noticed that when you move your mouse pointer over the stage, you can see these numbers down on the bottom right of the stage. Changing scratch uses a very common technique to control visual movement using a grid system. The great itself has X and Y coordinates. Ex defines the movement left and right, and why defines the movement Up and down scratches Grid is zeroed on the center off the stage, so both zero x and zero why are in the middle and the coordinates can go below zero. Moving left will decrease X on moving right. Will increase X. Moving up will increase. Why on moving down will decrease. Why? Let's say we want to write some code to make scratchy move to the right and back to the middle of the stage again by default. He's already started at zero x zero. Why? By moving my mouse, I can see that the edge of the stage is 240 x. There is some code to make the Sprite bounce if touching the edge of the stage. But let's avoid that for now. Let's code scratchy to move to 180 X, which is around about here and then back to zero X. And we'll start with a wen space key pressed event just for a change. Now scratch gives us a few options for movement. There's code to move a number of steps to go to given coordinates or even to follow the mouse. We can also glide to a set of given coordinates, change co ordinates by a set amount and set coordinates. I'll let you play around with those after the lecture to explore the differences between them. If you wish to see what they all do for now, though, let's use the glide code. Let's say glide to 1 80 x we can leave. Why, at zero as we don't want to move up or down. And let's code something like three seconds. The time here determines how long it will take to glide to the given coordinates. So it's like setting the speed. And let's test by hitting the space bar that's looking good. Notice that if I press space to run the code again, nothing appears to happen because scratchy is already at 1 80 x zero. Why, if we wanted to reset him 20 x every time we run, the code from the top would have to coat that in. Let's code him to immediately glide back. Left again another three seconds, and this time zero x zero. Why and let's test again. But just before I head space, remember that when our first glide code runs, we won't see scratching move because he's already at 1 80 x. Okay, here we go. Great. Now that scratches back to 00 we can run again and again to see the full movement. This time we see the full right movement and the full left movement back to center stage. It looks a bit strange, moving without facing the direction of travel. So let's make scratchy turn and face left before coming back to the start. There are a couple of ways that we can do this. We can use a turn block, going clockwise or counterclockwise or point in direction. Block direction can be a little confusing and scratch. Expanding the spite information will help me to demonstrate over in the Sprite area. Click on the little eye icon at the top left of the sprites pain. Here you can see some useful information and settings for the selected Sprite. We can rename the Sprite, for example, scratchy, see its current X Y coordinates, see or set its direction and set its rotation style along with some other settings. Sprites have three possible rotation styles in scratch. Full rotation left right rotation and do not rotate by default. Full rotation is selected. You can see from the little direction Rose here on the right that the sprite is currently pointed in a direction off 90 degrees. If we click and move that, we can see that zero degrees is straight up on 180 degrees is straight down. Notice what happens as I go clockwise from 180 degrees. We immediately switched to minus 180 countdown back to zero again. Three minus 90 which is upside down facing left. So in this case, the full rotation start isn't what we want. Let's set the left right rotation star now. 02 positive. 180 degrees faces right, while zero to negative 180 degrees faces left. It can take a bit of getting used to, but in this case, left right is the rotation star we want on. Our Sprite should be pointing 90 degrees to begin. Now let's add a point in direction block and set the direction to minus 90 which we can see scratches helping us out and telling us is facing left and we can check. This is correct. By manually moving the rose to see the output, there's minus 90 degrees facing left. Perfect. So that looks good. Let's run off script again to test it perfect, although we now finish pointing left. If we run the code again, we'll just stay pointing left the whole time. So let's start with another point in direction block and start scratchy off at positive 90 degrees. If we run the code now, he should flip around and we can see the whole cycle. Of course, because we're not repeating the code. He does finish pointing left, but that's OK. We'll correct it in a minute. For now, we can see that it's working nicely, and that's a little bit of an introduction to movement on the stage. But let's get back to our loops. We wanted our sprite to pace left and right, which means repeating some of this code again a number of times now. We could right click on a code lock and select duplicate scratch will duplicate the block we clicked on and everything below it, like so if we test that, we can see that the bloc works. Although this is only coding to right left cycles and was starting to get a fair bit of code, imagine 10 cycles of code or perhaps more things would start to get really messy. A better way to deal with this would be to use a loop. So let's remove our duplicate code and click on the control menu. We can immediately see three loops available to us. A repeat leap forever. Leap under repeat until loop. Let's try the repeat loop first. Now, if we drag the loop directly onto our block, we can see that it will automatically stretch to incorporate everything in our code block. Or we could attach the loop code and then drag everything in our code block into the loop. The choice is yours. Either way, you can see that this block of code, which you may think of, is one large block of four smaller blocks is now indented in the loop. We can set it to run, say three times, and let's change our glide time just to make the demonstration run a bit faster. Let's go down to one second, right on one second left with the repeat of three times. And let's run the code again by tapping space. And there we go three cycles see it once more. There's number one, number two and number three. The code itself is much neater, and if we want to change the block of code that controls our movement, are blue blocks here. We only need to change it once forever Loop is self explanatory. It will just run forever. Until you stop the script running. I can demonstrate by changing out the loops and pressing space to run. It'll just keep going until we click on the red flag to stop our script manually. And you can see that when I stopped the script, it'll stop wherever it is currently running through the coat and until all repeat until loop will run until we define a Boolean stop condition. I'll go ahead and swept them out now, and you can see that even though I haven't defined a stop condition yet, the code will run when I press space. You can see that if I forget to add the stop condition, the code will repeat forever, essentially becoming an infinite loop. This will probably result in a bug in our code, certainly behavior that we don't want in our finished code. So let's add a stop condition. We could co just about anything we want to hear. But as we haven't covered storing data just yet, let's keep things really simple for this demonstration and look for a user input on the keyboard. To stop her code from the sensing menu, grab a key pressed block and inserted into the repeat until loop on. Let's select the S key for the stop key. Of course, we could choose any Kiwi like and let's take a second to consider how our code will run. The space key is pressed on the code below. Starting with the loop will run if he s key is not pressed, will enter the loop and all of the code below in blue from top to bottom will run in sequence. When we get to the bottom of the block, we'll go back up, check the condition again. If the s key is still not pressed, will go back into the loop on again will repeat the cycle. If a this point on only this point the s key is pressed will exit the loop on any Cobreloa can continue to run. If I run the code again by hitting space, you can see that if I press s now, nothing happens now. Still nothing. And now nothing I have to press s exactly at the point where the movement code stops on the loop runs its check condition again. This happens at the moment when the sprite returns to the center of the stage. This is just one of the quirks of using a sensing element for the stop condition. But to prove it does work. Let's just hold the s key down so we can be sure it is pressed at the key moment. And here we go. Hold Eschede down now. There we go. We exit our loop. If we coated anything else below the loop, that code could now run. Let's test this with a think block coded just outside of our loop. I'll think something like few I'm tired. I'll run the code again from the top. Here's the space. And now hold down the S from now and there we go. We're out of the loop, so the rest of our program can run. It's also worth mentioning that you can nest loops within loops if you ever need to, giving you a lot of flexibility should you need it. There's no dedicated wild leap in scratch, but until and while loops are very similar, so we can get the same behavior we need with the until loop, for example, looping until a condition is true. It's the same as looping, while a condition is not true or false. Python does give us a while. Loop Onda four Loop, which is one of my favorites. So let's head over to idle now and take a look at them. Okay, here we are in idle. Let's create a new file to explore some python looping code. Let's start with the wild loop, and this time let's make some code that can count just like an until loop a while, Loop repeats as long as a Boolean condition is met. Let's start with account and let's assign a value of zero to begin with. Now, let's code, while count is less than five. This is our stop condition to prevent the loop running forever. But remember that we've made the count equal zero. So unless we do something else with the code below, this stop condition will always be true. Zero will always be less than five, and we'll have a nasty, infinite loop. Don't worry. I'll address that in a second. Also, notice the colon at the end of the wild statement. Watch what happens next when I hit return. The next line is automatically indented because we're using correct syntax on. We've got a good code editor. It's like the editor is saying Okay, I can see you're coding a loop. What code shall I loop? You'll see why the Indian helps in a moment. We can now tell Python what to do while our beauty and condition is true. Or let's print account print count. And we need to do one more thing to stop our code running forever. Otherwise, because zero is always less than five, we'll just print the number zero over and over and over again, and we'll have to quit the shell. Actually, let me demonstrate, as I'm sure you're curious. Let's go ahead and save the file and I'll call it leaps. Stumpy Y. Let's go ahead and run it, and there we go The countess less than five. We're not doing anything with coat to change that on our loop is running over and over and over again. And it will keep going infinitely unless we do something manual to stop the code. Naturally, we don't want this, and it may cause our program to crash. So what I'm gonna do is just manually close the shell right now. There we go. And I can just click on and run and python shell to stop the shell again. Okay, so let's address our problem. Let's add one to the count every time our loop runs, as one is printing the count. What we can do now is code count plus equals one with the plus equals of Houston code here , which tells Python to make the new count equal. The current count plus one. This is called incremental in this case were incremental in the count. Now let's run the script again. Here we go. Bingo. We've written some code to count to four. Why four? Well, four is less than five. And if we want to count to five exactly, we can either code. I'll stop condition to be less than or equal to five. In this case, we count to five or we can say less than six again counting to five as it's good practice, let's comment our code. Do you recall the syntax for commenting? And python? Well, it's a hash for a line comment or triple quotes for a block. Comment like So let's say Python counting script version. One point. I finish the comment with another triple quotes. And let's comment this line our last line in our script saying this is the same as the Count plus one To explain this particular line of code. You can see here that one type of comment, the line comment comes out red or dark orange in idle, and the block comment has come out green. But in both cases, these comments won't be run by our program, which we can see if we run the module again. So why the indent? While the indent is a visual tool to help us see how the code is going to run, we can easily see that both the print on the increment code are part of the same block of code, and this whole block will run while the loop condition is met. If we add some code to run below the loop when we exit the loop and remove the in dent using the backspace key, let's print this string saying Finished. It's correct syntax on its visually clear to us which blocks are used, where in the flow that is to say which lines are part of the same logical flow or block. Python knows the last print statement is back in the main flow and outside of the loop block. Let's go ahead and test it. There we go. So that's the wild loop, which is useful for using a Boolean to make code repeat while a certain condition is met or not met using the not operator, if appropriate. Next up, let's explore the four loop. Okay, I've just gone ahead and cleaned up our file, removing our code and restarting the python shell just for clarity. Now, back in the slide a few moments ago, I gave the example of using a four loop to code a statement like for each day of the week, go to the gym that might look something like for day in a week. Go to gym, maybe go to gym equals true. And, of course, to get this CO two work would have to define what a week looks like as we know that there are seven days in the week. But Python doesn't unless we define that somewhere. We have to make a list called Week on Define each day as a separate element. In that list, we'll get onto looking at list soon. Four loops really come into their own when the length of a particular list can vary. For example, let's say for each item in my shopping list purchase item. My shopping list varies each week. So this code would be really handy because it can loop through every item in my shopping list in order to perform a defined action like making a purchase. This special behavior is called iterating. Four loops are special because they can iterated over a list or a given sequence. We can see this working by defining my shopping list and printing each item. I'll create a list using square brackets and text strings on assigned the list to my shopping list so we'll use um, square brackets, and inside the square bracket will start to add some items as tech strings. Let's start with apples. Let's add some oranges. Looks, perhaps some bread, and why not finish with some milk? Each item in the list is represented by a text string separated with commas on enclosed in square brackets. To signify that, we're creating a list of then assigned the list to my shopping list so we can say for item in my shopping list, do something well rather than purchase that's creating code to print each item to the console. We cant. I print and inside the parentheses weaken simply type item. Let's go ahead and save and run arcade and there we go. Python is iterated over my shopping list, and for each item it's run the prints statement, looping a total of four times because we have four items. If we remove an item from the list and save and run the code again, we've only looked three times because we only have three items in the list. Python consent. The changes in our list as it iterating over the list looping through or iterating over lists like this is extremely useful in code making. The four loop one of my personal favorites and you may be interested to see the only four in and print R Python keywords here, we could actually change the word item to anything we liked. For example, we could simply say I and as long as we match that with printing I down in our print statement, these two things of the same. So the code will work, Let's run the module and you can see we have the same result. We could also change my shopping list to simply, say, shopping list. And as long as we matched that here in our four statement, the code would run. We'll certainly be using loops again when we come to building our own app. But for now, I'll say goodbye and thanks for watching, and I'll see you in the next lecture. 17. Conditionals: Sometimes we want to program our code to do something. If one thing is true and something else, if it's not to accomplish this, we can code what's called a conditional a conditional is a special piece of code that needs to check if something is true or false before it runs coding condition. ALS is like asking the computer to make decisions so we can use them to control the flow of a program. If an else statement are both types of condition. ALS bear in mind that when you code a conditional, it's like asking the computer a question. The computer can only run the conditional if it can go and get an immediate answer to that question. So it needs to know whether the conditions that you ask it to look at our true or false if it doesn't know which means that you haven't declared those things yet. It'll throw a buck. Remember, computers aren't going to guess unless you code them to like when you ask them to pick around him number. But that's a different lesson. I'll demonstrate this behavior shortly. Let's say that we want to use a conditional to pop up a message in our app. When the user reaches 100 likes, we could pseudo code something like this. When the computer gets to this line of code, it's going to check the current likes count. If the count is greater than or equal to 100 it will execute this code block and say, Well done. If the light count is less than 100 it will ignore that block of code and execute the else block instead to say, Keep sharing. Let's look at a few condition. ALS in action, using scratch and python. Okay, so we're back in scratch with a blank project ready to code rather than demonstrate the likes. Count with a well done or keep sharing message. As I've just talked about in the slide, how about we do something a little different? We could co two really simple guess the number game to really see control flow in action that's briefly decomposed. We'll start by making the Sprite randomly create a secret number. Next, we'll ask the user to guess the number. If he uses guess is correct, the sprite can say something like bingo. If the guest is not correct, we'll use conditional logic toe work out whether the guess is too high or too low on either say higher or lower to help the user. And we'll give them a handful of attempt to guess the correct number. How about we delete the cat and choose a different Sprite just for some variety? Let's bring in a new Sprite from the library on. Let's look for something a little different. How about No, no, he's a friendly little guy, okay? And we can now right, click on Scratchy the cat on Delete him. Okay, so let's start by making no, no choose. A random secret number will begin with a wen green flag clicked event from the events. Many and I'll increase the size so we can see it clearly. We need to create somewhere to store our secret number on a sign of value to it. I know we haven't covered storing data and variables yet, so we're going to jump ahead just a little on this one. Click on the data menu and then click on make a Variable. Let's name the variable something like Secret Number, and we can take the box that says for this sprite only don't worry about what that does for now and then click on. OK, now we can see the secret number variable We've just made set it, change it, show it or hide it and you can see that it's actually shown here on the stage. For now, though, let's just untech the box next to Secret Number, which hides it from the stage. So is actually a secret. Now let's grab a set secret number block, which allows us to set the secret number or a sign of value to it. And over in the operators menu, let's grab some code. To pick a random number from 1 to 10 is fine. This code will pick a random number from 1 to 10 every time a script runs on a sign it to secret number, and that takes care of Part one. Next, we need to ask the user to guess the number from the sensing menu. Grab the ask and wait code block, which we know comes with a ready made answer Variable, just like our secret number. And we've used this before. Let's ask, What do you think? The secret number days and that takes care of Part two Now we need some conditional code, and it's time for an if statement from the control menu. It's grabbing. If then code lock and we need to code our first condition. Let's drag inequality operator into our if condition between knows, the equal sign here. Single equals in scratch. And let's code if answer from the sensing menu is equal to the secret number from our data menu, which allows us to after the question. If the user entered answer is equal to the secret number that we've just chosen. Then we can execute this code block and do something. And if this condition is true, the user will have guessed the correct secret number. So let's say something. Let's say something like bingo in two seconds is fine. Now we know that if the user has failed to guess the secret number, they must have either guest too low or else too high. We can use an if else block to deal with this. We can code. If the answer again from the sensing menu is less than the secret number again from the data menu, let's say higher for two seconds or else if the answer is not less than secret number. Let's say lower for two seconds. The control flow works like this. If the answer is not correct and it's lower than the secret number, the sprite will say higher. If it's not correct and not too low, this bright will say lower because it must be too high as it is, there is a small bug with this code in the control flow. Can you spot it? Let me run the code and test and to help with testing, I'll check the box to show the secret number for now, and here it is on the stage just to help us to test. I'll start by testing the correct guest flow Solectron our script, and we can see that Nano has selected a secret number of nine and is now asking us to guess the number. So I'll end to nine and let's see what Nano says. Bingo and lower. Uh, this is our bug. The reason we're seeing both dialogues is that the guest number is the correct secret number, and it's also not lower than the secret number because it is a secret number. What's happening here is that the first if statement is executing and because we haven't coded scratched to do anything different. It also executes the second, if else statement to check the condition. So it's also checking whether the correct guests is higher or lower, and it's not lower. So that else Block is also evaluating. True on running the code to, say lower with an if else statement. If the if condition is not true, the else code Always run, Feel free to rewind and watch that again if you need to. But now that we've debugged, we can solve the problem with some stop code back in the control menu, let's grab a stop all block and place that underneath, saying bingo for two seconds. This will stop all scripts running, effectively finishing the game when we hit this line of code and let's test it again. We'll start from the top of the green flag, Nano's guest, a secret number of one and let's enter one and test, and this time we say bingo and finish the game perfect. That's our bug fixed, and now we contest the incorrect guest situation. Let's guess a lower number start again. Nano's guests seven. So let's end to something like to honey correctly says Higher, Perfect. And finally, let's guess a higher number. Nano's guest. Eight. We can go ahead and guess 10 lower. Excellent. And don't forget. Under the data menu, you can always untech the box next to secret number to remove it from the stage. If you're playing for fun, you can also add some code to hide the secret number variable, which you can place at the top of the script, which will always make sure we're using code to hide it rather than manually selecting it here in the data money. Well, as we've already covered loops, we could also add a repeat loop to give the user a limited number of guesses during our game. Let's go to the control menu and select a repeat loop. Now we want to give the user say five turns to guests secret number if they get it wrong. So let's set repeat to five, and we can code that in below our sprites. Random guess as we don't want the sprite to guess again and make the guests a moving target . Perfect. So we're only going to choose one random number, and then we're going to repeat the game cycle five times. Of course, if the player guesses correctly during the loop, we've added the code to end all scripts so the loop won't keep running after a successful guess. That's exactly what we want to feel free to. Go ahead and test that on your own. Now you've seen that you can coat conditional statements in sequence, and you can just Aziza Lee Nest condition als inside condition. ALS. Depending on what you need by way of control flow. So do play around and test that. Right now, though, it's time to jump into idle and see some control flow in action. Using python now, we could easily recreate the same guest the number program here in Python. But let's do something a little different just for more variety. Let's coda grading program to award a certain grade, for example, a B or C based on the mark that students achieve in an online training course. Again, that's quickly decomposed. To look at the various parts off such a script and to help me organize my code, I'll add some comments. Well, first, we need to ask our student users to input their mark as a percentage, for example, 75. So let's add a comment to, say, Ask students for their mark. Next, we need to write some code to decide which great award based on the mark. So let's say award correct grade. For example, a Markoff 90 or 90% could award on a great to the student, and we can use condition ALS for this type of control flow. Finally, we need to print the students grade to the user interface, and we contest the step by simply printing it to the console. So the third comment could be print grade to you, I for user interface, or come so and for practice. Let's start with an introductory comment to explain our script. So at the top, let's say students grading script. I applause interactive on end article quotes, and once again, we're going to jump ahead just a little bit using variables in this demonstration. But hopefully you already have a great idea about how they work, so you'll probably breeze the upcoming lecture on storing data. Okay, so let's code. Let's start by creating a variable. We'll call it Mark on a sign the value to it now to assign a user given value to something we need to first ask the user for their input. And we've already done this in Python so we can code the key word input, followed by opening and closing parentheses. Now let's add our question string inside the parentheses. Let's say something like. And of course, it's a string. Please enter your test mark and let's help them out with some brackets and say zero to 100 . And let's finish that with a ellipsis on the space that's looking good and keeping in with best practice. Let's quickly test that code with a Prince statement to make sure we're capturing the mark correctly. So let's print and we can simply type are variable name Mark and I'll go ahead and save and run the module. Let's call it something like Control flowed up. I and there we go pythons asking the user to please enter your test mark 0 to 100. Well, let's say we've achieved mark off 100. We've done really well. And when we hit return python prints 100. So we know we're capturing the mark correctly in the variable, and we're assigning the right value good, So I'll go ahead now and delete the print statement because I like to keep things clean. And we can always code another one if we need it. So now we can move on to thinking about Step two and how we're going to actually award our grades. Let me go ahead and add a block comment here to organize my thoughts. I find this a useful technique to build things up in stages. I'm thinking something like Great a could be 90 to 100 but be could be something like 80 to 89. See 72 79 great D 60 to 69. And finally, let's say for now something like an F could be 0 to 59. That helps me to organize and think about my code carefully as I code the next steps. Now we can use ranges in python, for example, the range 90 to 100. But to keep things super simple for this demonstration, I'll just be using standard operators. So let's look at the a condition first code that we can coat something like if the mark is greater than or equal to 90 let's do something and In this case, let's assign a great of a So let's create another variable called Great. And let's set that equal to or a sign If I do, you off a Let me take a moment just to talk about data types. You can see that the data type were signing to the grade is a string, and that's deliberate. We want the great to be a strength, but we want to compare numbers when it comes to working out which great award. So in this case, we're going to check where the mark happens to be greater or equal to the number or integer 90. Right? You're gonna have a problem if we try and run this code now and I'll show you why. In just a moment, in fact, I'll go ahead and run it so you can see what's happening. So what we need to do is now that we're comparing our first mark to assign a grade, let's print the grade so we can say print parentheses and inside, we can simply type great this time and let's save and run the module. Okay, so pythons asking us to enter the mark. So let's enter something greater than or equal to 90 to try and get a great A. So let's say 95. Look what happens. Ah huh. We've hit a bug. In this case, we've had a type error greater than or equals two is apparently not supported between instances off string on interject. So what does this mean? Well, this is a bit beyond the scope of this particular lecture, but I'll cover it because it reminds us about using the correct data types. A case of what's happening here is that first we're asking the user for their input. The use is entering a number 95 in this case on what Python did here was it stored the user's input as a strength. So the string 95 rather than the number or interview 95 was saved to mark the mark variable down here. When we checked the mark variable, we checked it against an integer. So we compared a string to an integer and that doesn't work. Now we want to make sure we use numbers and we compare numbers here so that we don't get strange behavior when we try and use a greater than or equals two to compare strings, as that does often throw up some strange behavior just because of the way that strings are handled so quick. Fix this is to take the user's input unconverted into an actual integer data type or a number rather than a string. So what we can do is before the keyword input recon type I NT or interject and then open parentheses. And what we can do is out of closing parentheses right at the end. So what we're doing is we're wrapping the whole user input and turning it into an integer. This happens behind the scenes in Python. Okay, so let's save and run the script again, and this time we'll do the same thing well into 95. And Bingo Python gives us this string A, which is exactly what we want. So as I say, using the integer function and looking at particular data types in this way is a bit beyond the scope of this particular lecture. But there's a good reason that we prefer to compare interview data types rather than strings, and to understand exactly why, would have to get into another lecture on exactly how strings are compared and handled in the back end by Python. But now that we're using the right data type, we can get back to our control flow. So what happens if we simply copy and paste out code, removing the in dent? Of course on I'll make another line just to keep things clear in order to save time. And you'll probably be tempted to do this. We might change a to beat on 90 down to 80 to handle RB condition, but actually would have a problem if we ran this code. Now if the user inputs a mark of ST 95 both of these, if conditions which will both run, would evaluate, true as 95 is greater on both. 80 on 90 and let me go ahead and prove that I'll save and run. And again that center of value of 95. Uh, and there we go. Pythons given us the string. Be so what exactly happened here? Can you see? Let's think about it. The first if statement would have run and checked whether the mark was greater than or equal to 90. It waas. It was 95 so the variable grade was created. Onda value a string of a was assigned next. The second if statement would run and again the mark would be compared and in this case is also greater than or equal to 80. So grade would now have a new value assigned to it. A would be over written by B. That's exactly what happened. The prince statement produced Be here in the council, obviously not what we want. So let's fix this with an else if statement which in Python looks like this, l f e l I. F. Or else if now, if the first statement evaluates false the else, if statement will run and check the mark and they can't both run, that's the key here. And we contested, of course, and again 95. And this time we get the correct value. A. Because the first condition was true and else if statement didn't run. Now we can pad out too few more grades in the same way, so we can copy and paste fairly safely now. So let's say 70 or above produces a C 60 or above should produce a D. Andi. For now, let's say in fact, let's use an else statement as a catchall to cover the case off any input below 60 including any other unwanted inputs such as a typo. So let's say else in this case, and we don't need this condition here, because else will catch everything that doesn't return true above. So we can say else grade should be well, for now. Let's just say F excellent. Let's go ahead and test. Okay, so let's test that, say our student has done really well and achieve the school off 100. That's a great A perfect Andi. Let's say 89 correctly, we get be. How about 70? Exactly. Gives us a C. Something like 62 gives us a day and let's say we've got a 50% have we done and that's an if and that looks great, but I don't think it's very nice to award an F. So rather than simply print the grade, let's a little more control flow with another, if else statement so that we can improve our dialogue a little. Let's code something like, I'll remove this print statement for now. If the grade is equal to if, let's print a string like something a bit more friendly hoops, better luck next time on, let's say else, which is our catch all for anything other than a great F Let's say, Well done, you earned the grade off. Okay, so this might be a good opportunity to join two strings together so we can show the user what they achieved in terms of their great as well as a friendly dialogue message. Let's come Katyn eight or join two strings together. We've done this before, using the plus symbol. So let's join. Well done. You earned a great off with. So let's join. Well done. You earned great off with Great, and this variable is holding a string type. This is one of the reasons why I prefer to use a string type for the actual great and as great is already a strength. We conjoined two strings together nicely without a problem. I think that's looking good. And of course, let's go ahead and test. So let's say we've done really poorly and only scored 20. Oops, better luck next time. That's looking good on. Finally, let's test the more positive condition and say we achieved 99 so we get well done. You end a great of a fantastic Well, that was something a bit different. And I think that code is looking good. Of course, there are always ways we can improve our code, so do feel free to play around with some of the ideas after the lecture. Maybe you can write a short program to convert dog years into human years or something like that to test your knowledge of control. Flow will definitely be using control flow a little more in our app. But for now, that brings us to the end of this lecture. Already. Hope you enjoyed helping the computer to make decisions, and I'll see you in the next lecture. 18. Variables: code could be used to store data like numbers, strings and billions. In general, we codas often need to store data. Both Wyler program is running and between runs of a program, Let's consider a game the data that we might need to store while the program's running might be the school player name and whether or not the play's completed a particular level , this information is frequently changing While the game is running, and to store it, we can use something called Variables. At the end of the game, we might want to save our progress and store all the data while the code is not running ready for the next time that we load the game. To do this, we might put all of our variable data into a say file or a database code. Makes all of this possible. We're not going to look at, say, files or databases this time, because it's beyond the scope of this course. So for now, it's enough to just appreciate that to store data between runs of a program. Those are the two common methods. What we're going to explore right now is variables and how we use them in coding to keep track of data that's frequently changing. A variable is a piece of code to temporarily store data that can change frequently. Think of it as a box. The first thing we need to do is give up box or variable and name so that we don't get confused, and we can always know what it's holding. Let's imagine that we want to create a variable to keep track of the score in our game. Let's call our variable school. Now that we've got an obvious and unique name, we can start to put data into the box the way that we do. This is called assignment. When we assign a value to a variable using code, the computer essentially knows what's inside the box. We can also use code to retrieve the value that's held in a variable at any given time and do things with that data. For example, we can draw the school value to the screen, share a high school with a friend. All right, it to a safe I'll. All we need to do is pull the data back out of the box. In addition, updating an existing variable couldn't be easier. All we have to do is a sign, a new value. Let's see some variables in action. In the previous lectures, you've already seen unused variables. We've used them to create accounting script, hold a secret number and store both marks and grades. We can create a variable. Let's name it storage box on by adding a when green flag clicked event and some set code, we can see that a variable construe or number data. For example. 100. When we run the script, keep an eye on storage box up here on the stage and there we go. It's now holding the number 100. We can also store string data, for example. Hello and you can see that storage boxes now holding the string. Hello and we can also store 1,000,000,000 data two rather than storing a string. True, to generate a true Boolean, we could coat something simple, like one is equal to one, and here we go storage boxes now holding the Boolean true. Let's create another variable, a name, this one school. You can see that we still have the same four code blocks here, but now you can click the drop down and see that you have the option to select which variable this block of code controls. We can control the the storage box or school. Let's select school and let's de select storage box from the stage as we're no longer going to use it. In fact, we can go ahead rightly can delete the variable. You can also move the variable display on the stage like so or right click to make a large readout format. Let's remove the operator and initialize the variable by assigning a value of zero to begin with. Of course, this is just one way to see the score on the user interface here in scratch. We could de select it and use a say block, for example, to retrieve whatever data were holding in this variable. But I'll go back to showing it directly on the stage for now. So what about updating the variable data in school now that we've initialized it? Well, we could set it again to anything we like. I would change it by a certain amount. What we refer to as incremental it. Let's add some very basic code to increment the score every time I hover my mouse over the cats bright. I'll start by adding a simple if then block and from the sensing menu, find a block that says touching mouse pointer. Here it is at the top. This code will sense whether the Sprite i e. The Cat in this case is touching the mouse pointer, which is the same thing as the mouse pointer hovering over or touching the cats Bright. Now we can increment the school if this condition evaluates. True from the data menu will select Change, score and let's give the player say 10 points every time the mouse touches the cat. Do you think this code will work? Well, let's try it, huh? Because the code is running only once, from top to bottom. Our if statement has evaluated false before. We've even moved the mouse anywhere near the cat. Can you think of a quick way to fix this? Yep. We'll use a loop. Let's use Forever loop and let's place it below set score to keep the check condition running forever while the game is running on, Let's test it again. Wow, look at that school going up. But OK, that is working. But now we've got a different Buck. The code is looping so fast that while I'm hovering over the cat, the score is going up way too fast. Let's slow things down and create a bit of a cooling off period using a weight block. Stop the script and something like two seconds might work for this. Let's run it again. Here we go. We started zero again. 10 20 30 40. Great. Now we can see that every time I move the mouse over the cat, our score variable is updated on incremental my value of 10 and just for demonstration purposes, let's leave the mouse over the cat for longer than our two second wait time. You can clearly see the loop in action here, along with the variable code, and because we always initialize the variable to zero, the school resets every time we start a new game. Variables are so useful for storing temporary or frequently changing data like this, and you can have as many variables as you need running at the same time to keep track of many different parameters, giving you a lot of flexibility to create interesting programs. So that's assigning retrieving on updating variables in scratch. Let's head over to idle and see some variables in Python. Python can also store various data types within variables. For example, let's create an integer and a sign of value off one that's creates float and a sign of value of 0.5 Let's create a string variable Onda sign a string off Python three and finally , let's create a 1,000,000,000 variable and set it equal to false in this case. Now let's go ahead and print off four variables to the console so we can see what they're holding. Let's print my interject, my flight, my string on my 1,000,000,000. That's right. We can print multiple objects using common separation in Python, so let's go ahead and save and run. And here we go. Partners printed are four variables one nor 10.5 python three and false so we can see that Python has no problem storing these four separate data types in variables. So that's assignment and retrieval. Let's do some updating. Next. Let's clear this code and add some code to monitor the sweets in my cupboard, so I know when to buy more. Let's say I currently have 50 sweets in my cupboard. And let's add a print statements so we can see what's going on that say, I'm going to eat some sweets at this point and let's I'm going to eat 10 off my sweets Now that's had some code to update the number of sweets in my cupboard. With this code, we start with 50 sweets in my cupboard, and after eating 10 sweets, we update the number of sweets in my cupboard to be the original number of sweets and my covered, minus the number of sweets eaten. So let's go ahead and print the result so I can monitor my bad habits and let's save and run. And there we go, eating sweets, and we now have 40 sweets in my cupboard. I really should have chosen a short of variable name to make coding this a bit easier. Just kidding. Actually, it's very good practice to make your variable names descriptive and unique, as it's quite easy to confuse them when you have a number of variables running in your program at the same time so we can see that Python did a good update here. We started with 50 sweets in the cupboard. We removed 10 on. We've now updated the original variable toe hold, a new value, which is 40. And aside from me choosing particularly long variable names, did you notice that writing sweets in Greg's covered is equal to sweets and Greg's covered again minus number of sweets eaten is a bit long winded. Updating your variable by adding or subtracting a number to the original contents of the variable does have its own shorthand to make it faster to code and easy to read. We can re factor this code to, say sweets and Greg's covered minus equals number of sweets Eaten, which is shorthand for the original code. Let's go ahead and test that. Let's say this time how about we eat 20 suites? There we go, the correct result of 30 which we've achieved with less code. Let's do another example. We'll start with a clean slate. Let's say I'm going to restock my cupboard and buy some more sweets, and I need to calculate how much money is going to be left in my wallet. Well, let's start with some coats, the same money in what it is equal to 40 and let's say buying a bunch of my favorite sweets is going to cost around 7 50 before tax sales tax is going to be somewhere around 20% Let's say of the net price so we can multiply point to buy sweets price to make that calculation and python. Now that we've calculated the tax, we have to add it to the original Met price to, let's say, sweets price. Let's use our shorthand this time so we can say plus equals sales tax. And finally, that's update the money that's going to be remaining in my wallet. And this time we can use a short hand again but say minus equals the sweets price. So we're now subtracting the gross price, which is thevenet sweet price, plus the tax from the money remaining in my wallet, which is the same as saying money and wallet is equal to money and wallet minus the total sweet price said to help somebody who's never seen this code before, or to remind me what I was doing. If I ever come back to this, let's add some comments. What we're doing here is starting with the money in my wallet. Next we're in putting the net price up sweets. Thirdly, we're calculating the tax Jew on those goods, and we're calculating the gross price, which is adding tax to the Net price. And finally, we're updating the money in wallet. So what the coach should now be doing is using the Net price of the sweets to calculate sales tax. After calculating the tax, we added to the price of the sweets to get the gross price. And finally, we complete the transaction by reducing our money and wallet by the gross cost off the sweets, including sales tax. Let's go ahead and test that code quickly. That's print money and wallet. We don't need to print the price of the sweets or the tax at this point, but let's print the money and wallet at the end of our script so we can see if it's being updated correctly and I'll just go ahead and copy and paste to save a bit of time. Okay, let's save and run. And there we go. We started out with 40. I was a bit naughty and bought some sweets, and we've now ended up with 31.0 remaining. So interestingly, partners updated an integer with a float but we've updated the variable nonetheless, with no problem. And there you have just a couple of examples of how we can use variables in Python to temporarily store data for use anywhere in a program. And that brings us to the end of the lecture. As always. I hope you enjoyed learning another key coding principle. Thanks for watching, and I'll see you in the next lecture. 19. Lists: we've seen that code can manipulate data like numbers, strings and billions. Re codas can end up dealing with a lot of data in our programs. In the real world, we might use something like a list in order to keep our data organized. Thankfully, in the coding world, we also have lists available to us. Some coding language is actually used the term list, while others used the term array. A List or a ray is a type of data structure that contains a group of elements. I'll demonstrate this shortly because they're a type of data structure. A great way to start visualising lists in your head is to think of them like a spreadsheet with rows and columns, lists of names, keys and values. As you'll see shortly. The list itself is actually just a special type of variable, as we can assign, modify or retrieve its contents whenever we want to. It's really easy to retrieve stored data from a list because we can input the list name and a key and get the stored value for that key back out. For example, we may want to retrieve user twos, score from this list at the end of the game. Let's go ahead and see this in action. Now. I've just mentioned that a list is a type of data structure. What that means is that a list can hold various data elements to help us organize our data in a structured way. I also just mentioned that a list is actually a special type of variable because we can assign data to it. Retrieve on, modify data as we wish. Making a list in scratch is therefore very similar to making a variable click on the data menu and click on Make a List. Let's call this one my list. You can see that my list is created, displayed here on the stage, and we can show or hide it, just like we can with a variable. We can also move it around on the stage if we want to. The Block pallet gives us quite a few options for manipulating our list data. We can add elements, delete elements in certain elements, replace elements, identify an element based on its position in the list, get the length of a list and check whether the list contains a given element as well as show or hide a list using code, and you're noticed that as well as manipulating a list with code, we can also interact with it manually by the user interface. On the stage, we can say its name. Length the fact that it's empty right now. Change its size and click on the small plus button to manually add or delete items to demonstrate some basic list functionality. Let's go ahead and delete the cats bright. Let's bring in three new sprites to begin. Cheese Sprite from a library on Let's Add and airplane. Uh, how about a car and perhaps what else? Some sort of everyday item. How about a laptop? Anil position knows, so we can see them clearly on the stage. Let's select the airplane to begin and add when this bright click block. Now we can go back to the data menu and insert some coat. Add an element to my list. So when the airplane Sprite is clicked on the stage, let's add the string airplane to my list. Now let's go ahead and do the same thing for the two other sprites, and remember, we can drag and drop code to copy it across to another Sprite and save time. So let's do that. So here the same code is in our cast, bright. And this time let's add the string car to my list, and we can copy that across to the laptop in the laptop script area. We're gonna change the code to say on the string laptop to my list. Perfect. Let's go ahead and test that code so you can see that my list is currently empty. Let's click on the airplane and there we go about the car. Yep, that codes working nicely and finally, huh? There we go, laptop that's working well, and we can add items in any order we like. For example, going back in reverse, you can see that each element or item is stored at a certain numbered position in the list . Starting at Position one. This is the list name. The numbers are the keys, and the assigned values are here on the right. Each key, which can also be referred to as an index, is paired with the value stored at that index, so Index one is currently holding the value airplane, which is string data. So how about removing an item from the list. Or let's add a button to delete items. Let's bring in a new Sprite something like Button five. The X button will work. Position it on stage, and we can code the button to delete the last item stored in my list every time that we click on it. So let's say when this pipe is clicked, that's delete an element of my list, and you can see that we can delete the first item, the last item on all items, and you can see that we can click on the window and manually override, for example, to delete item number two or the value held at index to more correctly. But right now we want to select the last item. So let's go ahead and test that code. Keep an eye on the list and you can see that when I click the button, the last item is correctly being deleted. So what if we want to add an item halfway in our list? When the length of the list is able to change like this? Well, let's bring in another item. Let's bring in a new sprite, something like, I don't know, an apple. So how could we code this apple to appear halfway in our list. That sale list has so add another airplane and say A list has four items. So we want to add the apple in half Way, which B index to well again, Let's start with a win clicked event and then back on the data menu. We want to insert an element at a given position of my list, and you can see that we can enter position one last or random, and again we can manually override that. So let's say we want to insert the string apple, and we know that the list is currently four items long. You can see that again with the length of four, so position to would be halfway where we could manually enter position to. But when the list length changes, two is no longer halfway, so we'd have a problem. The code wouldn't dynamically calculate the halfway point when the list length changes, so we need another approach to solve this. Firstly, we need to know which index is halfway at any given time, and thankfully, the list length is always being calculated so we can write a little code to work out the halfway index. Let's grab a division operator and then back on the data menu. Let's look for the block that says length off my list and insert that into the operator. So at any given time, this variable is holding the current length, so all we need to do now is divide that by two. If we coat that into our insert that in next block, we can now insert the string apple at the halfway point in my list, wherever that might be. So let's go ahead and test that if we click on the apple, you can see that list is currently six long, so we'd be expecting to see the apple around about Index three. And there we go. You can see that apple is inserted. Everything else is pushed down on the list. Length has increased by one. And if we start by having an odd number, for example, a list length off, Let's go for five. If we insert the apple at the halfway point, it's inserted at position two. Scratch just happens to round down. In this case Now, lists can be used to store numbers, strings and billions in a structured way, and there's a lot more functionality we can play with here. But as we're just looking to cover the basic concepts of how lists can be used in code, I'll let you experiment with the other list related code blocks at the end of the lecture. If you wish, before we move over to Python, you may also be interested to hear that as well. A store. Single items in an index lists can also store other lists in the same way that variables can hold other variables. I didn't cover this in the lecture on variables, but let me demonstrate the concept really quickly. Let's make a variable called bag and let's make another one called pencil case and I'll just move those two things down here so you can see them. And just for demonstration, I'll add a green flag event. Add that anywhere for now so we can go ahead and add some set code to a sign data to our variables. So we're going to set bag and we're going to set pencil case, so we need to set blocks. So first, let's store some stationery in our pencil case. So let's set pencil case equal to or a sign of string value off that said some stationery. How about pencils? Friends? It raises on a magic marker. Now let's set bag equal to or a sign Whatever is currently held in our pencil case, Here we go, variable within a variable. Now let's go ahead and run the script and keep an eye on our variable data here. I might move those over just a little bit on a green flag and bingo. You can see that now. Both pencil case and bag are holding our stationary data. Variable can be assigned to another variable, and in the same way, a list, because it's a special type of variable itself, can be assigned to the index off another list. That is to say, lists, construe or sub lists. You can have a list of lists if you like, and we call this a two dimensional or to D list. Right now, my list is one dimensional because it's only being used to store single elements, whether they're fixed or variable data. So let's quickly turn my list into a to D list. Let's delete are variables and clean up my list to tidy things up. Okay, now let's make the second list, and that's cool. This one. Airplanes. And to speed things up, it's tidy this up a bit to speed things up home manually. Add some airplanes to the list. So how about a jumbo? A 737 on a Triple Seven? Three types of Boeing's? Okay, so now we've got a sub list off airplanes. Let's editor code so that when we click on the airplane Sprite, we add our airplanes sub list into my list. So let's go back to our airplane Sprite. And this time we're going to add the whole airplanes list. Variable to my list. Watch what happens now when we click on the airplanes. Bright. Keep an eye on my list and there we go. Index one off My list now contains a list. My list is now two dimensional. The up we're going to build soon makes heavy use of lists. As to most real world applications on games, you'll find them everywhere. Okay, so that's a brief overview. Off lists and scratch. Let's go ahead and jump into Python now, where lists have even more power in python. We can create a list by starting with the variable name such as my list, and we can turn the variable into a list by signing data using square brackets in tax like so and now we have a python list, which just happens to be empty right now. So let's create a list off numbers. For example, a list of numbers would look something like this. Let's say this is a list off prime numbers and inside out square brackets we concert are primes, so that's hard to three five seven 11 on Let's stop at 13. Each element is separated using common syntax, and you don't have to add spaces after the commerce. But it does help to make the code much more human readable. We can also create a list off strength. Let's do the colors of the rainbow on. In this list. We can add our strings. Let's add read orange yeah, greens blue and to go on finally, islands months of colors in the rainbow. Now, one noticeable difference between scratch and python when it comes to lists is that in python, the sequence of elements starts at index zero and not index one. To prove that, let's retrieve and print the value stored at zero index or key. In each of our lists, we can stop with the standard prince in tax, then at the list name, just like you would print any variable. Let's print the list primes. And now we can use square brackets in tax again to retrieve the data stored in any given index off that list so we can add square brackets to the end of the primes list. And inside the brackets, we want to retrieve the store data at Index zero. So he simply add, See Right, we're looking to print whatever is held in the Primes list at Index zero. Okay, so let's save and run, and that's cool. Our script lists dot pie and there we go. Python has run, and we see the number integer to being printed on. The interview, too, happens to be the very 1st 1 in the list, but the index is Index zero index start at zero and count up. So we have 01234 and five. Let's go ahead and print. Rainbow lists index zero value at the same time. So let's add it to the end of our print statement com a separation, and this time we can type Rainbow Index zero save and run again, and we can see we have to read as Red is also held stored at zero index off the rainbow list. So let's do another quick test and print whatever is stored at Primes Index three and rain bows Index. Let's say full So what would you expect to see this time when we run the prints statement? Let's go ahead and test and there we go. We get seven blue in this case for index 0123 an index 01234 What if we want to replace an item in a list? Maybe we don't like the color red anymore, and we want to call it something else. Perhaps Ruby. Well, this is dead simple and python. We can just code the Rainbow List index zero, which happens to be the red string and let to sign a new value and will make that value this string ruby. Now to test that lets print the whole list just for fun so we can print rain by. Let's go ahead and save and run. And there we go. We can see the whole list has been printed in our code. We created a list called Rainbow and assigned the first item in the list of index zero to read. We then changed that using code and ran a print command, and we can see that Index zero is now holding Ruby. And how about adding an item where we can easy add or upend an item to a blank list or at the end of an existing list with the upend function? Let's say that we've just discovered another prime number after 13 and we want to add it to the primes list. Or we can code primes dot upend. This is called dot notation at some parentheses, and inside those weaken type the new number 17. This will upend the number 17 to the end of the primes list. So let's go ahead and print primes to see if we've successfully managed to add a new number to the end of the primes list using code. And there we go. 2357 11 13 on a new number off. 17 has been appended to the end of the list. Perfect. Then we've now got a slightly longer list. And speaking of length, here's another function. Let's print. And this time let's print the length off the primes list so we can type L E N for length, which, you can see is identified as a python keyword. Add another parentheses. So we've got brackets within brackets or a function within a function. We'll talk about functions very soon. And which list do we want to get the length off? Well, we want the length off the primes list. Let's print the length off primes, and you can see that this time we've printed the number seven as the prime list is seven elements long. 1234567 And remember, this is different to the indexes or indices. An empty list has a length of zero, so our Primes list has a length of seven because it contains seven elements, but it starts with index zero. So we have 012345 and ends index six. So the number two in this list is that index zero. But it's the first element in the list. It's easy to get those two things confused. I often do, and there are lots of other ways. We can use the length function to manipulate lists here in Python. So what about two dimensional lists? Well, that's not a problem at all. We can coat something like this. Let's say let's create a list court men and into the list. Let's manually add some strings. Let's say Greg, Jack and Charlie happened to be men. Let's create another list called women on. Let's assign some strings into this list. Let's say Danielle and Myrtle happened to be women. Let's go ahead and create 1/3 list on Let's call this one people. So now we have a list of men, a list of women, an empty list off people. So let's use some code to upend the list. Men on the list woman into the list people. Well, we've just used the upend function. We can use it again. So let's type people dot upend some parentheses and let's add men. Another line of code people don't up end, and that's also append women. So what we're doing here is we're adding the list containing all the men into the people list, and then we're adding the list containing all of the woman into the people list. So to test that, let's go ahead and print people to see what the people list looks like. After these lines of code run, let's save and run And here we go. Now we have a two dimensional list. We're list of lists. Men is a list woman is a list, and they're both contained within the people list, and men happens to be index zero off the people list. Woman are Index one off the people list, and the list is two elements long, which we can see if we talk. Print the length of people and you can see that it's two elements long and not five elements because each sub list is just one element within the people list. So what would you expect to see if we printed something like people on? Let's say, index one. Let's add another set of square brackets and let's add the index, say zero. Well, let's go ahead and test, and we print Danielle because Danielle is the value in this case, string data stored at Index one of the People list and Index zero of the females sub list. So Python gives us a lot of power when it comes to lists, which is just one of the reasons why Python is such a powerful and useful general purpose programming language. We can even do things like Come Katyn eight or glue lists together. Let's do that just to test. Let's create a list called a On Let's Say That list contains the numbers one and two. Another list, called B contains the numbers three and full. Let's create 1/3 list, see and set it equal to a plus Bay on. Finally, let's go ahead and print this sea. What would you expect to see? And there we go. Python has joined or glued, or the better word is Con Katyn ated two lists into a single list, and we've already seen how we can use a four loop to iterating over a list and search for elements within it and perform actions on the list and its elements. Very powerful stuff, and we definitely don't have enough time to explore all of the list functions now. But if you go on to learn python, you'll almost certainly be introduced to them and quickly discover the python documents online, which will help you to unpack an experiment with all of the list features. In fact, most languages have fantastic online documentation aimed at beginners and a quick search. There will often answer your questions way faster than, say, posting in a forum. But more on that in the bonus section. Well, I hope you've enjoyed this brief tour into how lists can help us coders to store and organize our data in a more structured way. And that brings us to the end of lists. Next up, we're going to explore functions, see that? 20. Functions: functions are snippets of code created specially for doing repetitive tasks. Functions are built to accept an input, do something with that input and return an output. Here's how the theory goes. The code that you pass into a function is called the argument. The code that does something with the argument is called The Body, and the code that the function passes back to us is called the Return. Let's say that we want to Codey Sha Zam style app to listen to a song and tell us what that song is called as this is one of the most common tasks. Are APS going to be performing? We could write a function to handle this. The function body could include all of the code that we need to analyze this song, check it against a global database and make a decision on its most likely title. The argument would be the sound that comes from our microphone, which we could pass in while detection mode is turned on and the return would be the song title, the idea being that we could call or use the same function code again and again to pass in any song and get its hopefully correct title. If we ever want to modify the body code, we only need to do it in one place, functions congenitally, accept any data type as an argument and also return any data type. For example, we could make a function to work out whether an email address is legitimate and contains the at character. We could pass in a string in this case, the email address itself, and ask for a 1,000,000,000 as the return, such as whether the email is true or false. Another quick point to note before we go ahead and look at a few functions in action is that they don't always require an argument to be passed in, and they don't always provide a return. For example, the print function we've already used many times in Python does require an argument, as we have to tell it what to print. If you recall, the argument goes in between the brackets, but it doesn't need to give us a return. It just does the task of printing to the console, ready to see some functions in action. Let's do that now, before I begin the demonstration here and scratch, it's worth mentioning that at the time of this recording, there are some limitations on coding functions within scratch. For example, there are no named or dedicated function code blocks, but we can produce the same behavior by using what scratch cause a custom block At this time. Custom blocks can be built to accept arguments, but not to provide returns. Although I have read that scratch may well be changing this with an upcoming future release . For now, though, will demonstrate the concept here in scratch as we're going to use some functions in our upcoming at project, and we'll be able to expand on this and see returning in action when we head over to Parthenon for this demo. How about we code a super simple virtual assistant where we cannot for the user a few interesting dialogues and some basic functions just for fun? Perhaps we can ask the user if they would like us to guess their favorite color, offer them a pearl of wisdom or calculate the square root of a given number, and we can create three functions to handle these features. Okay, so let's start with a basic function where we create the function and code the body without requiring an argument or providing a return. Let's make a simple function to offer the user a pearl of wisdom. Go ahead and click on more blocks and then click on Make a Block. This will make our own custom code. Block will use it to CO. To function. So first of all, we have to name our function. Let's call this particular one pearl of wisdom and click on OK, now the function has been created, which you can see here, and you can see that it has a head where we can define what pearl of wisdom as a function does. Essentially, this is the head off the function, and we can start to add body code for the function itself. From the Looks menu, Let's grab a say block. Let's say something like the Circle has no beginning and no end something really deep like that. And bingo. Now we have a function. It's ready to go, but it isn't connected up, so there's no way we can run it yet to make it run. We have to call it. Let's add a green flag event and you can see visually from the shape of the function code that it isn't designed to simply slot into another block above it. So we can't slot it into the when green flag click to run. That's not how it works. We have to call the function from within our main code by coding. It's block from the more blocks menu. Like so remember, this is the function block itself, and this is the code block defining the function. So this code calls our function, and we're going to call the function as soon as we click the green flag. In this case, if these two are not attached and we click the green flag, you can see nothing happens. Once we go ahead and attach the code, we're going to call our function and while a apart of wisdom, okay, so let's go ahead and create our second function. This one congest the users favorite color. Let's make another block and name this one. Guess color with good old U K. Spending. Okay, and that's but this one over here. So we want us right to say chosen color, so we'll start with another say block, and this time we want to pick a random color to say so. How shall we coat this? Well, as we've just covered lists, how about we manually create a simple list of colors and then write some code to pick a random color from the color list? Let's go ahead and click on data on Make a List. Let's call the list colors, and I'll manually add a few colors to the list. Mets pat Red Orange Yeah, green. They in to go on and finally, Violet. And there we go. We've got seven items in the list, so now we can say the given item number off the colors list. So let's drag that over into our say block and let's code it to pick a random item from the list. Now we can manually go ahead on hide the colors list from the stage, and we can swap our hell of wisdom function code for our guests. The color code so we can now call guest the color when we click green flag. So let's go ahead and test that. And there we go. We've chosen green. Let's run it again to make sure it is picking around them. Item this time. Yellow Perfect. Nice. Where we've coded to fully working functions. Let's move on to the last function to calculate the square root of a user given number. This time, we need a function that can accept an argument because we'll ask the user which number they want. The square root tough again. Make a new block. Let's call this one square root. But this time, let's click on the little options dropped down. You can see that we can add an argument. Input for numbers, strings on billions. Don't worry about these other options. For now, let's add a number argument to this function. Now we can name the argument. Let's name our argument. Use a number. It's already selected, so I'll just type. Use a number and OK, okay, so you can see that we can now define the Function Square Root, which takes a single argument called user number. Again, let's add a say block and over in the operators menu. Let's look for an operator to calculate the square root of a given number, and here it is at the bottom. You can see that this operator actually allows us to perform many mathematical operations on a given number. Okay, so We're calculating the square root in this case, but off which number? Well, here's the neat trick with functions simply dragged the argument variable down into the function body like so, and it's copied into place. Now we can calculate the square root off whatever number the use it gives us. Let's test the function, and we can pass in the number four is the argument for testing, which will manually enter right now. We're still running our old scripture. Let me stop that and this time and we click green flag. We're going to call our square root function, passing in an argument off four. And hopefully our Sprite will say this gray route off that given number, which in this case is fourth. Here we go. Bingo square root before it is, too. So we know the functions working perfect. Well, we've now got three fully working functions, one of which accept surpassed in argument when the function is called. But we still don't have much of a virtual assistant. We need to add some or interaction. Well, let's remove calling our function. For now on, we can start with some dialogue. Let's say hello. I'm scratchy and two seconds should be about right below that, Let's say, how can I help? Now let's add a user input or ask block to say something like into 14 The hell off wisdom and to To and I'll guess your favorite Keller and three and calculate the square. Root off a number getting a bit long that should work. Now we can use the answer with some conditional code to decide which function to cool based on the number that the user has entered. For now, we can just grab three blocks to achieve this, and that's add some code to check if the answer is equal to 12 or three. So we need three equality operators on back in the sensing menu. We can take the answer variable on, drag it into all three off these operators, and now it can check if the answer is equal to one. Then we'll do something if it's equal to two or do something and if it's equal to three, will do something. So what exactly shall we do? Well, let's go back to our more menu, and if the answer is equal to one, let's cool a pearl of wisdom function. The answer is equal to That's cool, I guess. The color. And if the answer is equal to three, we can call square root. There's just one thing left to do now. We need to ask the user to input any number they like so that we can pass it to the function and calculate that number's square root. We can do that by adding another ask block before we call the square root function like So let's say something like, Okay, it's enter a number. And now we can put another answer variable into the argument window within our function block to pass the answer variable as an argument to our function. The answer Variable. This one here will be updated after this ask code runs. So if it started out as three after this code runs, it'll be updated to a new number and then passed into the function. Also, it doesn't matter that the variable is called Answer here and use a number over here in the function head and body. When data is passed into a function, it effectively gets renamed. So as long as we use the same name here in the head and down here in the body of the function, it should work perfectly. Okay, so let's test our virtual assistant program. Here we go. Hello? I'm scratchy. How can I help and all right, this doesn't look particularly great, but let's see if it works into one for part of wisdom to to get your favorite color and three to calculate the square root. Well, let's start with one. And if we run that wow, Circle has no beginning and no rent. Incredible. Okay, we know that function works. Let's try running the whole script again. And this time, let's enter Number two and Scratchy thinks my favorite color is red. Fair enough. So we know the second function is working. And finally, let's test the 3rd 1 out. This time I went to number three. We get a second dialogue into a number. Okay, how about well, let's use for again as we can easily test the output and there we go. Square before is too beautiful. We can now call any of our three defined functions based on user input and control flow. At this point, you may be wondering why we bother to make the function rather than just putting the body code right here within the main code block. Well, there are two common reasons. The first is reusability. This is an over simplified virtual assistant, but if we were coding a more functional one, it would probably have much more complex dialogues and control flow. We may well need to use the same body code in many different places in our program. Rather than have duplicate code all over the place. We can use functions so that there's only one body or definition that could be called from anywhere in the program with just a single block. The second common reason is modularity. Separating our code into modules like this allows us to read it more easily because it's now in smaller pieces and it's easier to work on. It's easier to see what the code is doing, And if we're in a team, different coders could concentrate on different modules. So there we have a basic demonstration on functions within scratch. Although we didn't see a return in action, we got an idea of how that works by using the same block to see the results of the functions output here in stage price and give us a little more power. So let's head over to idle and see it in action. Here in Python, a function that we've used many times already is the print function. We can use this function because it already exists here in Python. It's part of what we call the standard library off built in functions. The creators of the Python language included this built in print function. Because it's so common, lots of code is need it frequently to print output from their code for testing and so forth . If this built in function didn't exist, we'd have to code it from scratch, and that would take time and effort. So built in standard functions. Give us coders a really head start. We can also very often save time by importing functions from modules or packages if a function that we need isn't built in, there are many modules to python, all of which have lots of specific and useful functions contained within them. For example, there's a whole add on module called math. If we wanted advanced, ready made maths functions, we could simply import the math module like so, and if we needed to, we could then type help math to get helpful information on this module. Let's go ahead and run this code, and here we go. Here's some useful information on this particular module will start back up at the top, and we can see that we've asked for help on the module math. The name of the module is math on the module. Referenced docks can be found at this website address, and if you're wondering where to get information on all of the other modules available, or here's where you could look in the python docks and the library, and you can always do a Web search for python modules to find out what's available out there. There's a description telling us this module is always available, and it provides access to the mathematical functions defined by the sea. Standard C is another coding language, and here we go. Here's a list of all the functions contained within this module functions like a car's a cosh, a sign etcetera on a description on what these particular functions can do. After importing the module, we could use any of these functions as if they were built in just a Z easily as coding the Prince statement. If we only wanted one of them or a hand picked selection of these functions, we don't need to import the whole module. For example, if we only wanted the A cause function, we could code something like from math or from the math module. Go ahead and import a cause or the cause function. And then we could go ahead and just code something like a cause and used the function just like the print function inside our program, they say, cause function would know work as if it's built in Okay, So what if we want to function that's not built in and not available from a module or third party package? Well, we have to create it ourselves, just like the custom block we saw in scratch. We can easily code on define a custom function here in Python. Let's define a simple function to greet a user. Well, we start with the death keyword for define at a function name. For example, Say hello and finish with parentheses. Ana Colon for correct python syntax. We can go ahead now and define any arguments that we want this function to accept, to begin with We won't use an argument will just go dysfunction to print a greeting so we can coat something like print. I had a partner, and now we just need to call our say hello function to make it run so we'll make a line break and remove the end Dent on Let's call say hello. Let's go ahead and run that And there we go Hello, partner without printing from inside a function. And in fact, functions can easily call other functions like this when they run. Next, let's coat an argument parameter. Let's make this function except an argument we can call it name and functions can have as many parameters as we need them to function with. One parameter like this can accept one argument function with two parameters can accept to arguments and so forth. Okay, so let's make our function do something with the name that we're not passing in. Let's join or can Katyn ate the name variable to the end of our greeting in order to personalize the greeting. Let's print hello space and join it to our name. Variable, which were receiving as the argument or we need to do now is called the function again and this time, Parson an argument. Let's pass in a name well, that she's mining. There we go and save and run the code and bingo. More personalized greeting, which we've printed from inside of a function. We called the function passed in string data as an argument, which we called name the function when called Received our argument, added the argument or the string data stored in the name variable to the end off the hello string and then printed that to the console. And we can easily pass in a variable as our argument. For example, let's make a variable court person and set it equal to the string myrtle. Then we can call out say, hello function and this time passing the person variable as our argument as person should now be holding string data, we should be able to join the strings together with no problem. Let's test it out, and this time had oh, Myrtle, no problem at all. So we still haven't seen a function returning data we've printed from inside a function, but we haven't made the function return data when called. So let's do that now. There's actually a good reason why we need functions to return data. It's called scope. Some programming languages and python is one of them have both global and local scope. For example, we can create a variable here inside the function. Let's call it greeting and let's remove our print function to set greeting equal to the string. Hello, Plus our name variable. This greeting variable will be local in scope if we try to retrieve its data outside of the function, for example, by printing or attempting to print greeting, it won't work because outside of the function, greeting is not visible. There we go, a name era. The name greeting is not defined or pythons actually right here We have defined greeting here by setting it equal to hello plus name. But we've done this locally inside of the function outside of the function here in the main code body were looking for a variable called greeting. But as far as python conceit e, it doesn't exist. This code doesn't have visibility into the function. So what we need to do is return greeting from our function. We can talk return and our greeting variable. But where was the greeting actually be returned. Well, it's returned when it's called and it's being called here. So what we have to do is actually make a new variable in order to store the return data. But this variable is going to be global in scope, so we can say greeting is equal to say hello, person. Now this is a fair bit to take in, so let me run through what's happening once more from the top. We're creating a person variable and we're sitting there equal to the string Myrtle were then creating a global greeting variable and setting that equal to, and we're calling our function passing in person as an argument. The function runs person becomes name now and the name is joined to the end off the hello strength that saved in a variable called greeting, which is a local variable. And the local variable greeting is then returned out of the function back into our main code when it's cold and as it returned its being stored inside of our global greeting variable were then inspecting on printing our global variable into the console. So if we save and run this code, we can see that it works perfectly. You may be wondering why languages use scope it all. Why can't this code here simply be seen by this code here? Well, here's one reason. What if we have lots of greetings in our code and decide we need to editor function a little and change the name of this variable so it makes more sense. Perhaps we call it greeting one, and we then want to return greeting one or without scope. If we did this everywhere else we use greeting in our code would now break. Changing one function could risk breaking our entire program or at least causing us a huge amount of work. But thanks to scope, because these variables are local and these global, the code will still work fine after editing the function. Another reason could be because you have lots of different functions, all doing things with the same data. You wouldn't want to risk all the data getting mixed up between functions and thanks to scope. Each function can only see its own local variables, which avoids a mess. So that's functions and a little background info on scope, which I hope you found useful. The up we're about to dive into does make basic use of functions, so you'll be able to play with them a little more. And don't worry, I'll walk you through everything again. In a practical context. In fact, this also brings us to the end of this section on common coding concepts Really well done on all your learning. Thanks so much for watching and I'll see you in the section recap. 21. Common Coding Concepts: Recap: congratulations on completing common coding concepts. With all that knowledge under your belt, you're in a great place to hit the ground running in the next section when we get started on our app. Before we do that, though, let's have a quick recap of everything we've covered in this section. We explored how code runs and saw how code runs left to right and top to bottom. We also looked at how code could be put into blocks, which can be run or ignored while our program runs. We looked at bugs and learned that a bug is simply a mistake or a problem with our code. We learned that bugs are usually caused by typos, syntax, errors or unwanted behavior, and that debugging is the process of finding and correcting these problems. In pseudo code, we learned that pseudo code is used to represent riel programming languages, using simple, everyday language that anybody can understand, which is useful for creating an outline or rough draft of a program. In decomposing, we explored the idea of breaking larger tasks or problems down into their component tasks and then re composing our work so that we better understand the effort place tasks in the right order and ensure that we don't miss anything out. In commenting, we discovered that even though code should be written in such a way that it's easy to understand why it's own commenting, which is like adding notes to our code is a great way to help us and other coders to make sense of it. In common data types, I introduced you to three common data types, numbers, strings and billions and touched on how code can help us to assign, modify, compare and store data in arithmetic and operators. We saw how code is a fast and powerful way to carry out arithmetic and learned how comparison operators like greater than and logical operators like and allow us to perform powerful logical operations with code to mirror the type of everyday questions and problems that occur in the real world. Loops taught us that we can make code repeat itself, saving us a lot of coding work. I introduced you to repeat until while on four leaps, as well as warning you to watch out for infinite loops when you start coding up your own loops in control flow. I showed you how conditional statements like if and else can be used to make decisions and control the flow of a program. We saw that condition ALS effectively decide which blocks of code to run based on whether given conditions evaluate to either true or false. In storing data, we learned that coders often need to store data in the short term while the program is running on the longer term. While the program isn't running, I mentioned that we could use either a say file or a database for longer term storage, and we explored using variables to store frequently changing data in the short term with lists. I mentioned that lists are a type of data structure, like a spreadsheet which could be used to store various data types in an organized way. We looked at a signing data to a list on accessing values again, using their associative key and finally related functions, we discovered that functions or special snippets of code created to do repetitive tasks. We learned that functions can taken argument performed to find actions and return results. We also saw that even though function has these abilities, we don't always have to pass in arguments on We don't always have to get a result. We can call a function simply to perform its defined actions. A really hope that you enjoyed this section and you've probably learned a lot of new information. So again, congratulations in the next section. We're going to pull all of this together and put these concept into practice. We'll do that by building our very own app, and I'll introduce the app in the next section. Before we do that, though, feel free to have a well earned coffee break. I'll see you in the next section. 22. Project: Goals: Hello and welcome to Section five. It's time to get hands on and build on that project. As always before we dive in. Let's take a quick peek at our section goals. Well, now that you've had a brief introduction to so many common coding concepts, we can pull them all together and see how they work alongside each other in a more realistic project. So our first goal is to build a nap. The app itself is going to be very simple, but it will use all of the concept that we've covered in the course so far. I'll also introduce you to value proposition design, an agile development because that's the way that great teams build great APs. We're going to start by decomposing and planning our app. So our second goal is practiced at decomposition. Bill three is to get a little more practice at coding within scratch, and go for is a little more practice in python and to all intents and purposes, will be building the same basic app in both languages. After all, one of my main goals for this course is to take you from scratch to Python to show you that coding and powerful text based programming languages isn't scary at all. It's fun and you can do it. Finally, Gulf five is to really see everything come together and cement your knowledge of common coding principles, giving you a solid foundation to build on. If you want to go and learn a language will take another, more specialised course. Okay, so without goals in mind, let's dive in. 23. Project: Brief: value proposition design is all about creating products and services that people actually want. There's a great book about it, which I'll mention in the bonus section if you're interested to explore Maura about that at the end of the course. One of the key principles in value proposition design is that successful products solve a real problem. So let's take a look at an example problem. Well, I find that eating is great, but choosing my meals each week is a bit of a pain. There are so many great choices. It gives me a bit of a brain freeze, trying to decide what to cook on. Remembering all the key ingredients for those meals and therefore the weekly shop is also a pain. I find it too easy to forget things, but I also find writing the shopping list a bit tedious because I have to go through all my recipe books to remember what's in each dish. Of course, when you've identified a real problem like this value proposition design says that you should design a solution that adds real value by solving that problem in a user friendly or cost effective way. So let's think about a possible solution. There's another fantastic book from a team at Google on how to solve problems and experiment with ideas and again are linked to it in the bonus section. So at a high level, how might a solution to our problem look well? How about a simple app to pick meals for us and then give us a list of all the ingredients that we need for our weekly shopping list? And of course, every idea needs a great name. So how about we call this possible solution idea? Munch? All right, so we've got a real problem, which we could go and test by asking other people if they have the same pains. And we've got at least one good solution idea that we want to go and test out. Where do we go from here? Well, let's take a look at a simple roadmap inspired by the lean and agile development movements and fear not are linked to further resources on those two. So let's start by decomposing are ideal solution will break down the problem on build up a solution that we think solves the problem in the most efficient way. Next up will build a proof of concept in scratch. A proof of concept is the simplest form of the product that you need to build in order to prove that your idea for a solution actually works. Because scratches so quick and easy to play with it should work perfectly to help us prove our concept and get Munch working in a basic way. And lastly, if all goes well without proof of concept, well, move over into python and build on M V P or minimum viable product. An M V P is the most basic or minimum form of a product that's actually viable in the real world. The central theme here is viable, and you can remember that because the word viable is right in the middle of M. V. P. We're not just testing that we can build a solution at this point. We're testing that the solution that we did build is viable. In other words, does it solve the problem in a user friendly or cost effective way? Essentially, do we like the app? If for some reason we don't and we need to change things up, it should still be early enough for us to do that because we haven't had to spend too much time and effort building our M V p. So will we like Munch? Well, we'll certainly find out. Let's decompose. 24. Project: Let's Decompose: Okay, let's begin with the problem. Our problem had two parts to it. Choosing dishes was a pain, and so was remembering all of the required ingredients. So it sounds like we could solve these problems with to call features, choose dishes for us to eat, to remove the pain, of having to plan out all of our own meals for the week when the APP has chosen some nice dishes for us. We wanted to list all the ingredients to remove the pains off, forgetting to add ingredients to our shopping list and searching through old recipe books. Let's think about the kind of questions that Munch may ask if we give it thes two high level tasks. What if we ask Munch to choose dishes for us? Perhaps it will ask. How many days do you want me to plan your meals for? And while we're on the subject, what kind of food do you actually like? That gives us to sub tasks right off the bat? We need to tell Munch how many days we wanted to plan for and what kind of food to choose from. As there's no point building Munch to plan a week of meat dishes. If we're vegetarian, let's think about days first. Whatever happens, we need to define how many days Montreuil plan for. But do we hard code that, or allow a user input and make it variable? Hard coding means that the days would be fixed by a constant in the code, so we can't change that unless we modify the code. A user input would be more flexible as it would allow us to use a variable that we can update any time we run the app thinking about it. I've got a period of a week in mind because I usually shop once a week and planned seven days worth of meals every time I go shopping so he could set a constant of seven days. But what happens if one week my friends take me out for dinner or I stop and get a take away on the way home from a long day at work thinking about it? Let's go with the user input option so we can run Munch for a different number of days without having to touch the source code each time decision made. What about adding the food we like user input would be great. In fact, it would be brilliant to be able to add dishes we like or remove ones we don't like anymore . But doing that is going to require a safe system. We need a database or at least a file safe system to stall of our dishes between runs of the APP. We don't have time to build that now, and we only need to prove the concept to begin with. So for now, let's just use a list and hard code, a list of our favorite dishes into the APP. To begin with, we can just update the source code on the odd occasion that we think of a new meal we like or we get tired of an old meal. And if Munch proves viable as an idea, we can improve on it later with the ability to add edit on removed dishes through user input. That's another decision made. Okay, so what about our second feature? Giving us a list of ingredients Munch may want to know which ingredients are needed for every dish in our food we like list. We call that mapping data we'll need to map all of the ingredients needed for every dish that we add to our food. We, like list will need some kind of array or list within a list. We can explore how to actually coat this without proof of concept. So let's just say we need a sub list. For now, we can use pseudo code to capture what we want much to do and get into the exact details a bit later. Do you remember the lecture on why some languages are better than others and how most of the time it comes down to which tasks each language was originally designed for? Finding a programming language that handles sub lists nicely could be one of the important factors we consider when choosing the final language to build Munch with. Now, let's consider our ingredients list a little more. We're going to need a way to check which meals Munch has selected for us on only add the ingredients to our shopping list. If a dish has been selected, that sounds a lot like an if statement to me. So let's use, um, control, flow or condition. ALS to check which items months elects and then have munch at those ingredients to our shopping list. And speaking of our shopping list, how should we capture our outputs? I think that too simple lists would work nicely for this app. A list of dishes for our chosen period on a shopping list of ingredients we could print both to the screen or console as a starting point in our proof of concept on M V P. And looking at our two core features, I think we could probably use to functions to handle these one function, to choose our dishes and another to build our shopping list. Well, I think that's looking good. We've decomposed and recomposed into a high level plan. I think we're ready to see how he might go about coding Munch up by using a little Pseudo Co to explore. So let's go ahead and do that now. 25. Project: Let's Sketch It Up: all right. So we made the decision to go with a simple user input to ask the user how many days they would like Munch to plan their meals for select pseudo code. That next we thought about creating a function to choose dishes so he could call that function and pass in the user's answer. As an argument, we decided to try a hard coded list of meals that we like so he could code that up and make the function pig meals from that list. This way, we're always getting meals we actually like. When our function has run successfully, we'd like to output a list of meals for the given number of days, and again, we decided to use a simple list for now, which will show to the user we'll call this my menu. So how might our choose dishes function actually work? We need to cut that up soon, so let's pseudo code that as well. We need to define a custom function which we could call choose dishes for now. And we know that the function will need to accept a single argument that chose a number of days so that it can plan dishes for that many days. So let's think about how we might code the body. To begin with. We could code it to choose a random dish from our food. We like list and thinking about it. We probably wouldn't want to eat the same dish. More than one sin are given period just for variety, so we could have a simple conditional to make sure that dish hasn't already bean chosen. And if not, we can go ahead and add it to our menu. Nice, but this code will only run once. So at best we're only going to have one item in our menu. We need our code to run a few times in order to build our menu, and in fact, we need to make it repeat until we have the number of dishes that the user has asked for. Okay, so that's looking good. Do you think this pseudo code would help you to coat the proof of concept in scratch? Is there enough detail? Well, I think so. So let's keep going and head back to our main code to take a look at the next part of our flow. Our second core feature was listing the ingredients for the chosen dishes. Perhaps we can offer a dialogue here and ask the user if they would like much to go ahead and generate a shopping list for them. With that input, we need a little more control flow. If they say yes, let's call a function to generate the shopping list and then show the shopping list to the user as an output. Another list will do for now. If they say no thanks, perhaps we end the programme. At that point, we've already considered using a list of ingredients for each possible dish. So in keeping with out proof of concept approach that's manually code a short list of ingredients for dish a another for dish be and so forth until all of the dishes that we like have their own ingredients sub list, which could be selected within our function. Great. So let's take a look at our second function now, So let's consider our build shopping list function and how we might define it. I think the flow here would be quite simple. Perhaps we could use condition ALS to check whether each dish has been chosen for the my menu list. and if it has at all of its sub list of ingredients to my shopping list, that way the shopping list will be relevant and only tell us to buy what we need for our menu dishes. Excellent. I think that covers our second function in enough detail for our proof of concept and scratch. So let's head back to our main code again and make sure we've got everything covered here in our pseudo code. Well, we can probably include some more dialogue here and there, especially up at the top of our script to introduce the APP and tell the user what the APP does, just in case we give Munch to our friends or the wider community to try. But we can always play around with that when we get into scratch. In general, I think we've planned things out nicely. The overall flow makes sense. I think we've covered all of the lists that we need on the outputs were expecting from the APP. We've got a good idea of our two core features and associated functions, and we can see where we might need some control flow. All right, so keep this picture in mind and let's jump into scratch and get cracking and are proof of concept for Munch 26. Project: Scratch Proof of Concept - Part 1: here we are back in scratch, where we're about to take our pseudo code plan for Munch and turn it into a working proof of concept. First things first. Let's start a new project, as this time will be saving our progress. Let's also give our project a suitable name. Well, I'm calling mine munch. Okay, now, before we write our first code, it's worth taking a minute to talk about. Agile, agile development is a hugely popular methodology for building products, especially products like caps, and we're taking an agile approach with this project. The big idea behind Agile is to build and release products in stages or increments rather than releasing everything right at the end. Taking this approach gives us the chance to test features along the way so we can be sure they're working on that. The APP is really useful. While we're still building it by building and testing a proof of concept, we can be sure it's technically possible. And if we hit any issues, we can adapt. That's a job by building and testing an M V p. We can be sure the APP delivers real value to the user who may just be us. If we hit any issues there, it's still early enough to adapt, as we haven't invested hundreds and hundreds of hours of work into it. That's our job to. If Munch happens to go beyond M V P Stage, which we're not going to cover in this course, we'd almost certainly want to spend a little time thinking about the visual style of the act and what the user interface or you I would look like if we were planning on putting it in front of other users, For example, we might need to think about how it will eventually look on a mobile device. And unless you're going to do everything yourself, it would be best practice to collaborate with a designer so that the U I and your code can come together As you're working on the proof of concept, that's another agile principle. Teams of people like coders, testers and designers work together at the same time and self organized their work to decide what to build. With each new increment off the product, for example, you might decide to change things or get a great idea for a new feature by seeing how uses a responding to your app. In the real world, normally, the proof of concepts are focused on whether what you're trying to build is technically possible. So there's not much point in worrying about design right now, with proof of concept, functionality is usually kick. So with that in mind, we don't need to worry too much about design for a proof of concept. But let's still choose a nice bright to give much more friendly feel as we interact with it by the stage. Let's delete the cats bright by right, clicking on clicking on delete, and we can choose a new sprite from the library to bring in. Choose any sprightly like I just happened to like this one, which I've just found called Robot One Okay, and its position us right down here on the stage. All right, so now we've got our friendly looking Munch mascot on stage. Let's think about our opening dialogue well, let's start with a green flag event, which we can code into robot ones script area, and I'll increase the visibility, perhaps down one. And while I think about it, it might be a good idea to rename our sprites rather than Robot one. Let's call him much. Pepin's Let's add some say, block code to introduce much. So from the looks menu. Let's say hello. I'm much I'll help you to plan your dinner menu and something like three Seconds should work, although we can always test that shortly. Now, thinking back to our pseudo code plan, we need to ask the user how many days they would like Munch to plan their meals for. Actually, one thing I didn't mention in the plan was that the APP is only going to be designed to choose a single evening meal, as I don't have any problem choosing my breakfast lunch every day. But, hey, you can always change that later if you want to. You could easily tweak the app to choose as many meals as you like per day. Perhaps we can add a comment to remind us about this idea later. Let's make a bit of space and right click on the same block and add a comment. And let's say, shall we offer to choose more than just dinner one meal day Perfect. We'll collapse that one again, and that's something to think about later. All right, So let's add an ask block and ask, How many days would you like me to plan? So let's ask, How many days would you like me to plan? Question Mark and we already know that scratch will save the users. Answer in the answer. Variable. How about we keep the dialogue going to make much feel a bit more personal? Let's add another say block. And this time let's say something like Okay, I'm going to plan that many dinners from your favorite meals list, and we can replace that many with the actual number of days given by the user, which will be stored in the answer. Variable. All right, so let's grab another save. Look on to do this. We're actually going to need a joint operator, So let's drag that inter se block and we can say, Okay, I'm going to plan. We could put in a space, and then we can add another joint block. We can bring in the user's answer midway through the string, and we can finish our string with dinners from your favorite meals list on three seconds is probably reasonable here, too, and we might actually need a space here in front of dinners. Okay, so in keeping with best coding practice, we've written a few lines or in this case, dragged a few scratch blocks. So let's test it. Let's green flag to run the script and munches introducing himself. How many days would you like me to plan? He says. Let's plan for one minutes into the input. Okay, I'm going to plant one dinners from your favorite meals list. Well, one dinners wasn't very good English, so let's put the plural s inside brackets. And that should make more sense to the user. It's tried again. And this time let's ask for five. Okay, I'm going to plan five dinners from your favorite meals list on the spaces. All look good. Perfect. Well, I like it so far. All right, Well, now that we've got our intro out of the way and got our number data that we need stored in the answer variable, we can think about coding our first function. If you recall this first function needs to choose the required number of dishes from our food. We like list. Huh? That means it's time to coat off food. We like list as we can test the function without it. So let's go ahead and do that. Now. Can you remember how to create a list here in scratch? Yep. Let's click on the data menu on Make a List. Let's call this list food we like. And it doesn't matter whether we select for all sprites or for this Sprite. Only for this list, as we only have one sprite in the program are leave ful sprites for now on its OK. And here's our newly generated empty food we like list displayed upon the stage. Now I'm thinking that we could start with around seven dishes in this list, as that covers at least a week. Obviously, the more the merrier here on would probably need to think about what happens if the user enters mawr dishes than we actually have in this list. For example, if they ask for 14 days worth of dinners and we only have seven dishes, we'd hit a bug. Actually, let's add another comment to remind us to think about that later on. Let's comment our ask block and we can either scroll over or move the code. Ask all over and let's say something like what happens if the user request small dishes than we have in the food. We like a list. Excellent. And that's a little food for thought, perhaps. Okay, I'll stick to coding, right. Let's add some food we like into our list. Well, I like that's manually. Add at sea pizza. Be Fergus on. Obviously feel free to add your own favorite dishes here. Book. Stir Fry. Always a favorite in this house. Chicken for heaters. I think that's how you spell for heaters, but of Mexican who, uh, bangers on mash. That's a very British one. If you don't know what that is, it's basically sausages and mashed potato. Well, another British one. And I need to make my list a bit bigger. What about fish and chips? That old UK favorite and to mix it up a bit. Mawr. How about a Spanish omelette? Fantastic. There we go. Seven favorite dishes. At least they are in my house. Brilliant. Well, now that we've got our food, we like list, or at least enough for the proof of concept testing. We can go ahead and manually hide it for now. And while we're here, let's create our my menu lists as well as we're going to need it shortly when we use munched auto populated without chosen dishes using the function. So I'll make another list. Cool. This one. My menu full sprites is okay on or position it perhaps on the right of the stage, somewhere like that. And of course, we're going to leave this one blank as much will auto populated and we can leave it visible on the stage for now. Okay, well, now we've got our number of dishes variable stored in our answer, and we've got a basic list off food we like with a blank my menu list ready to populate. I think we're ready to code up our first function. Now, you know, I'm going to ask if you recall how to define a function here in scratch. Of course, we click on the more blocks menu Andi, make block and let's name our function. Choose dishes. Now, this function needs to accept a single argument so that we can pass in the number of days that the user wants the app to plan for which we have in our answer variable. So click on the options dropped down and let's add a number input. And perhaps we want to call our argument days, which would be the same as dishes, as we only want one dish per day so you can use any. I'm gonna go with days for now, though, and then we have our function head and thats position that into the script area. In fact, let's tidy things up a bit, and we can drag a co top left and perhaps have our function somewhere here on the right. I'll move it around. But that's okay for now. Okay, that's looking good. So we can start to think about the function body and what we want. This choose dishing, choose dishes, I should say function to do well if we think back to our plan. We had three key steps to think about at this point. We want to pick a random item from our food. We like list. That's step one. We want to make sure the item hasn't already being picked for our menu otherwise would have a pretty boring menu, and that's Step two on. We want to repeat thes two steps until we have the required number of dinners or days that the user has asked for on that. Step three. Now, as is almost always the case with code, there are many ways we could do this. So I'm just going to pick one way and test it out. Remember, we're building Anjar here so we can always change things if we find a better way later on, we want to experiment with a different approach. Okay, so let's start with step one. Choosing a random item from the food we like list. We could start with some item code to simply choose a random dish from food we like. So if we set item random off food, we like this code will pick a random item from that list, And we could use, um, ad code toe. Add our randomly selected item of food. We like into my menu, essentially moving around. Um, item from one list to the other from here to here. Moving on to the second step. We need to be able to check whether the randomly chosen item has already being added to my menu. And we could do this with some contains code or list contains element code, but what would we put here in the list? contains thing window. We need to know which item this code has chosen so that we can check for it here and see if it's contained within my menu. We need a temporary store off, frequently changing data. Can you think of a neat way to achieve this? With code? Of course, we can use a variable, so let's create a variable to temporarily store the chosen dish from the food we like list so that we can check for it down here. We're already on the data menu. Select Click on Maker Variable and it's called Our Variable Chosen Dish. Perfect. There it is, on its currently displayed on the stage, which we can leave for now. Perhaps we want to test that in a moment. Now we can change the way we're doing things a little with some set code. It's tidy up a little bit so we can see what we're doing. And let's move our item code into our set code so that we can set our newly created chosen dish variable too, and pick a random item from the food we like list. So I random item is now going to temporarily be stored in the chosen dish variable. So now we can check if my menu contains the shows. An item well chosen dish, I should say, by dragging it down into the window. Good. So now we can see whether my menu currently contains the dish we've chosen at random. Andi, think about adding it into the my menu list if it hasn't already being selected so we can really get on the step to now. Well, let's use a conditional if statement toe handle this on. We wanted check that my menu does not contain chosen dish before we add chosen dish to my menu. So we're going to need a not operator within our conditional code over on the operators menu. Let's grab and not operator if we drop our contains code into the not operator. The code is now evaluating whether the chosen item is not contained in my menu, which is exactly what we want. And now, of course, we can do something. If this statement evaluates true, So what shall we do? Well, let's add chosen dish to my men, you know, so we can drop our ad code inside our conditional and take another chosen dish variable and that's what we're going to add. Great. Or before we move on to Step three in this function, how about we do a quick test just to make sure the code is doing what we expected to do so far? Of course, the function won't do much right now if we test the coat because we're not calling it. So in order to test our function out, let's go back to the more blocks menu and let's cool choose dishes. And at this point, we can pass in our answer variable as the function argument. So answer is going to be passed in, and when it's inside, the function is going to become days, which we can use within the function later on. All right, so going to call Jews dishes. We've set some body code here for choose dishes on. Let's test and see how it's working out. Let's click Green Flag. Hello, I'm Munch will help you to plan your dinner menu. How many days would you like to plan for? Well, I'll go ahead and enter one as we know the code can't do more than that because it's not repeating itself. Yet. Two years ago, one and enter. Okay, I'm going to plan one dinner from your favorite meals list. And there we go. We've got our first favorite meal inside my many. Okay, so let's review how that worked. Well, we called our choose dishes function and passed an argument off one. The code picked a random item from the food we like list and temporarily stored that item inside a variable called chosen dish. And we can see that at this point, the code chose pork stir fry the conditional coat, then ran to check whether pork stir fry was not already contained within my menu. Of course, this evaluated true because the list was empty. And so I chose an item. Pork stir fry at this point was added to my menu, and we can see that happened here. Outstanding. All we need to do now to complete our function code is think about step three making the code repeat until we have the required number of dishes. Okay, so let's think about this. Well, we might think about using a simple repeat loop and using our argument variable days to define how many cycles we should loop or repeat this code Well, let's see what happens if we do that. Replace this code inside our repeat loop. We're going to repeat for the given number of days that the uses entered. And let's see what happens if we take this approach. Let's run a few tests cycles and see how it works. Uh, but before I test, I've just noticed that my men you still contains the item pork stir fry scratches, storing our list data between runs of our script. So if we don't do anything to change this, our list will just keep getting longer and longer every time we test or run the app. So let's quickly fix that by resetting our list each time we start the app under the list palette off code, let's grab some delete code and will code that up in the start of our program to delete oh , off my menu on. Let's just test that before we continue. Good. So now we should be able to get a clean test each time we run so we can join our code back up, and we don't need to worry about doing the same thing with our chosen dish variable because that's going to be overridden each time and eventually will hide that on. The user won't even see it. All right, so let's get back to testing are repeat code with the number of days on the repeat loop. Let's go ahead and run a test from the beginning, unless asked much to plan another one day. Good. And this time he's chosen Pizza. Pizza is being placed on my menu by our function, so we know passing an argument of one works with the repeat. Lee. Let's run again. And this time we'll try, perhaps, too. All right, so you got pork stir fry again on bangers and mash, and we could go all the way through to seven. As we've got seven items on our food. We like list, but I seven is the most limiting case. Let's go right ahead and test that. Let's ask much to plan for seven days this time. Ah, when it looks like we've got a bug, we asked Munch to plan seven dishes for my menu, and we've got seven dishes in the food we like list, so why have we only got five dishes here in my menu? Can you see what's going wrong. Well, the answer lies in the fact that we're looping our code fixed number of times, whether or not we're actually adding a dish into my menu each time. What's happened here is that Munch has randomly selected at least one dish mawr than once. So when are conditional? Code ran on a least one occasion and in this case to it returned false and didn't add that dish to my menu on that loop or cycle through the coat and thats out bug to fix it. We need to code Munch so that it's somehow counts. How many dishes have successfully being added to my menu and stops only when that required number taken from the argument variable is reached. I'll demonstrate a couple of ways we can do this. One way is a little more efficient, but I'll start with a method that uses accounting variable, which we only increment when a dish is successfully added to my menu, and I'll show you this first method because it's useful to see this technique. The technique appears fairly often in code, especially in four loops, which you'll probably see later on. Okay, so let's switch our repeat loop for an until loop to use a counter method. We'll start with a new variable, and let's simply call it counter coders often create a variable like this and simply use the letter I to name the variable as I stand for incremental. But for now, counter will probably be easy to follow. Now let's make sure we always set counter equal to zero when we call our function. Now we know that if we successfully ad a dish to my menu, we want to record it so we can add some code inside of our if statement to change the counter by one. If this happens, this is called incremental the counter. So let's do that by adding some change code. And remember, we're only going to change the counter by one. If our statement here evaluate true and we enter the conditional If the statement here evaluates false, these code blocks aren't going to run, and we're going to check with, and we need to loop again. So that's looking good. There's only one thing left to do now. We need to code out until loop to run until our counter is equal to the required number off days or dishes. So now we can pull in inequality operator. Our counter variable on argument variable from the function when we call the function are Coble. Run on more set counter to zero. So unless the user asks for zero, dishes will enter the loop. Choose around him, dish from food we like check that it's not already in our menu list. And if not, who added an increment, the count by one and will repeat the cycle until the counter eventually becomes equal to the number of dishes requested by the user. All right, while the proof is in the pudding, as they say. So let's go ahead and test before we do that. Let's hide our food. We like list again, and while we're testing, it doesn't hurt to leave our two variables visible. So let's run the script starting from the top. And this time let's go for the most difficult case for most limiting case and asked Munch again to pick seven meals. Here we go fantastic while we got there in the end, and you can see the random order that munch work through until we got all seven items. So we asked for seven days, and our counter made it all the way up to seven, which is when I repeat, Loop stopped and the list was fully populated. Very nice. So that's the counter method. But I mentioned there was a slightly more efficient way of achieving the same functionality . It's more efficient because it uses less code unless code means a smaller file size as well as possibly being easier to work on. Can you think of another method to control the loop? And I'll give you a hint? How about we remove the manual counting code and re factor our code to use the length of the list, which is automatically being calculated in the background? We can simply coat are repeat until loop to repeat until the length off. The MIC menu list is equal to the number of days requested by the user less code for hopefully the same result. But let's go ahead and test to make sure it works. And again, let's ask for seven dishes. Perfect. The list populates as required, with no duplication of dishes, so I think we can call our choose dishes function done. Great work. I'll just go ahead and tidy up a little bit. We can remove this code now on. Let's hide out two variables as we don't need them to be visible. And perhaps now is a good time to take a second to comment our code a little more and explain what this function cheese dishes does. Just in case we ever need to collaborate with other people on this project. Let's say something like this. Function cheeses, variable number off dishes and generates my menu list accident and thinking about the mind menu list. I'm not sure whether we want it. Always visible on the stage is it's not particularly useful to see the list data in between runs. Perhaps we can add some code to hide my menu at the start of the program and then show it again. And at an appropriate point, let's add some hide list code up with start off the program. Let's hide my menu on. We can always show it again at the appropriate point, perhaps just before we call the Choose dishes function, and by that time it should be clean because we've already run out delete code. Let's give it a quick test just to make sure it works before we move on. Good, much cleaner on the stage. Let's go for something like three this time, Andi. Great up pops the list, and then we even see it getting populated as the function runs perfect. Well, I think that's looking good. And I think we're now ready to move on. Great work. 27. Project: Scratch Proof of Concept - Part 2: So now that munches generating a given number of meals, we can start to think about our second core feature building the shopping list with the required ingredients for the dishes that Munch has just chosen. Rather than diving straight into that, let's offer another fund dialogue on a user input to ask the use if they would like the shopping list, as that may not always be the case. So let's take a say block and say something like Done, Is your dinner menu on Let's Keep With the theme of three seconds is that seems to be working for these dialogue lengths. So let's grab another say block and immediately below. After three seconds, we can say something like out of all these meals. My favorite dish has to be leave a space, and in fact, now we need a joint operator, and we can copy and paste this text across into the start of the join operator. Put that back into our say block on. Let's join that to a randomly picked item are the newly generated. In fact, I move the function up over here randomly picked item off the newly generated my menu list and three seconds again should work, and that's test that to see how it feels. All right, Munch, go ahead and plan for two. Please. Don has your dinner menu out of all these, my favorite has to be beef burgers. Well, that's a little bit of fun, and it does feel a bit more personal. Perfect. Munch feels a bit friendlier already. Okay, so now let's code up on Ask Block, and we can ask the user if they would actually like the shopping list to be generated. So let's use a string like, Would you like they shopping list for these dishes? And we can ask them to enter Why? Or and so the Y or end keys for yes or no? And then we can detect the eso no input in our answer variable and run some control flow, depending on what the user wants. So for now, let's just start with two, if conditional, statements and into those we can add to equality operators, and we can coat those up now to evaluate whether, uh, newly over written answer variable contains the string. Why of the string end? Of course, that's just one way to do that. we could perhaps user, if key pressed on, detect the y or N Keys. That way, as is often the case, there are a number of ways we can manage that, and in fact, there are a number of ways we could manage the control flow here as well. We could use an if else block, perhaps to catch the yes condition here in the if bit and the no condition here in the l Spit. And in fact, the else code were detect any other input other than perhaps the yes key or the UAE strength. Now that's something else to think about. What if the user makes a typo or does something unexpected, For example, they mean to enter why, and they actually press the letter t or you using two. If statements, it wouldn't be picked up so no code would run. But here for use in fl, statement a T or you would run because it's something else other than what we might be looking for, which would be a why perhaps, well, this is a little beyond the scope of this course, but what we're running into here is something that we code is called validation. Actually, users always doing unexpected things in the real world. So good codas use validation in that code to check whether a user input and other things like various types of data or key presses are actually valid as part of the control flow in their programs. But for now, we'll stick without, too, if statements. But while we're thinking about it, let's add a comment, and this can remind us to think about possible validation ideas a bit later on. So let's say something like, What shall we do here? If the user antis a string other than why we'll end and it's a envy, we could detects key presses as an alternative. How shall we handle validation? There we go. Something to come back to you spelt What wrong looks a bit better? Okay, so perhaps at the end of the lecture, you can come back to this code now and think of a way to have munch catch any unexpected input. Like if the user enters a key other than why or end, I should say, enters a string other than why? Oh, and on maybe we can say something like, Oops, I didn't catch that and then ask them for the input again using a simple loop. For now, though, let's press on without basic proof of concept. So if they use, it does ask for a shopping list we've already planned to call a dedicated function. In order to handle this, we've also discovered from our decomposition and planning that we're going to need a shopping list, which will start out empty and get populated by the build shopping list function. So let's go ahead and create that empty shopping list. Now we can click on data and make another list. Let's call this one my shopping list, okay? And for now, let's manually leave it hidden, although perhaps we can position it on stage, all right, we can manually leave hidden until we need to test it. Good. So when we define our build shopping list function in a moment, we've got an empty list ready for that function to populate in order to populate my shopping list. Oh, we discovered that we need a set off sub lists. We need a list of ingredients for every dish that we currently have in our food. We like list. Once we've done that, we can use our build shopping list function to check if each dish has Bean added to my menu . And if it has on the user's asked for a shopping list, we can copy all the ingredients. Sub list data over into my shopping list. Okay, so that was the theory from our plan. Let's go ahead and do it now that we can see our food. We like list clearly here on this stage because I've set it to show manually. Let's go ahead and make our sub lists. Let's go ahead and make a new list for each of those dishes on. Add the ingredients. I'll start with the first dish. So let's call this list something like ingredients. Maybe we can just abbreviate that toe I n g and underscore on say, pizza. Okay, and now we can add our ingredients for our pizza dish. Well, I'm not much of a cook myself, so I don't really know how to make the pizza base from scratch using dough and everything. So I like to buy my pizza basis ready made. So I need those. That's that's, um, tomato. So cheese pepperoni slices. And how about some nice Pepa's all right, so there's the first of our ingredients sub lists for our pizza dish, and what we can do now is manually hide the sub list of ingredients to avoid clutter on the state as we work our way through and add ingredients for every single dish. So let's hide that one. And we're gonna make another one now for beef burgers. Well, you can see that this is going to be a little tedious manually entering all this data. So what I'll do at this point is fast forward the video to the point where I've created all of the ingredients lists. At this point, if you're coding along, you can always pause the lecture and finish creating all of your lists now. Okay, so welcome back, and you can see that I've now added all of my ingredients to my ingredient sub lists on Let's go Ahead and our hide the food we like list and just show you the results of all my hard work. I will hide my menu, and there we go the sub list or a list of ingredients I should say, For every item that we have in our food, we like list. So let's hide all of them again. And I think we're just about ready to cut up our second function. So let's go ahead and make a new block, and we can name our function build shopping list, which is what we've planned for this function. Doesn't need any arguments so we can place it on screen and crack on with coding up the body. Now we sketched up an idea to use a Siri's off. Conditional is here inside the function to check in sequence if each dish in food we like has been chosen for my menu. And if it has bean, we add its ingredients to my shopping list. All right, so let's start with our first conditional Let's say if and we can say my menu contains, and now need to make sure that what we enter here, the string data we enter within this window matches perfectly What's going to appear in the mind menu list and we can find that originally in the food we like list. Essentially, this strength that we're searching for has to match this string that's going to appear eventually in my menu. Exactly. We have to match case on any spaces perfectly. After all, if it's not an exact match, Munch considers it a different string and more hit a bug. So we've used in uppercase P and then lower case lettering for the rest of our string. That's copy that over here. Okay, so if my menu ends up containing the string pizza, then well, now we can add its ingredients to my shopping list, the blank list that we've created in the background. So let's go ahead and take some ad code. We want to add something into my shopping list, making sure selecting the correct list here and what we want to add into my shopping list is the ingredients for pizza so we can take our ingredients pizza list variable and inserted like so excellent and again, there are a number of ways that we could deal with this functionality here in our coat. Perhaps there is a more efficient way that we can identify later, or perhaps even when we move over into our python, M V P. And we have a little more coding power available to us like coding four loops. But for now we can simply right click and duplicate our code to cover every possible use case. Oops, I didn't mean to add a damn and add it below, and it's fixed that up, so I should have done is click duplicate and make sure I added it in the correct place. And we're going to keep duplicating until we've covered every use case, which is every item in food we like. So we need seven of these, if conditional, statements. So let's say we've got 12345 two more, six on seven. Very good. Well, we need to do now is check our strings and are variables. So let's do that. This string should be thief space burgers. In the correct case on its no ingredient pizza, we're going to add its ingredients. Beef Vegas. In fact, to avoid a mistake, I'm going to remove all of these. Otherwise, on my just forget to add the correct ingredients, Andi. Next, we have book staff right on its correct ingredients. Chicken heaters, making sure that spelling matches perfectly on its list. Variable bangers and mash, fish and chips and finally Spanish on it on its sub list of ingredients. Nice think that's all seven possibilities covered We're using the correct sub list variables, and we're using the correct strings in our conditional check. Okay, so you can see how this code would become cumbersome as we start to really pad out the food that we like in our food We like list and increase the number of possibilities here. So with that in mind, let's hide our food. We like list to tidy up the stage. And let's add a comment to our function definition and say something like, Is there a more efficient way to handle this function rather than using all of these? If statements, it's safe, perhaps a fully to remind us as we get into Python on our M V P. Very good. All right, Well, I think that's looking good. So all that remains here is to call our build shopping list function at the correct point in our programs flow. So let's remind ourselves where we're going to do that. And we want to call out, build shopping list function here. If the user enters. Why, when we ask whether they want a shopping list generated so back in our more blocks menu, we can find the build shopping list function, call and coated in him. And, of course, we can check that were actually showing the shopping list at the correct point on the stage . Perhaps right after it's being generated. So back in the day to menu, it's add some code to show our list after we call the function and generate it on the list that we're going to show is my shopping list. Let's also hide it at the start of the program to keep things nice and clean for the user each time they run the app. So that's also hide our list. I didn't mean to run the code. Let's hide my shopping list and seeing this code here. That actually reminds me that we should also make sure we're deleting everything on the shopping list at the beginning of each program run so that it's being correctly reset each time. So let's take another delete block, and that's delete or off my shopping list. So now we're always going to start with a blank shopping list before we call the function to populate it. All right, and let's test from the top. Let's ask months to plan. Well, let's just go with one for now on his plan. Bangers and MASH. And, of course, that's his favorite one, because it's the any choice. Okay, Would you like a shopping list for these dishes? Into why or end? Well, let's say why. And there we go. My shopping list has bean generated, and we can see all of the ingredients for bangers and mash, which are sausages, potatoes, gravy, onion and peas. Accident. I think that's looking great. Perhaps Weaken. Just finish by adding a couple more dialogues to create a more user friendly ending to the program. So let's grab a couple of say blocks on will put one in each conditional. So if we've gone ahead and given the user a shopping list, let's say something like, Well, uh, bon appetit and three seconds or work again. And if we haven't given the user a shopping list, let's say something like, You got it Bye for now. And that reminds me that we should actually test our no condition just to make sure that's working properly as well. All right, so bit more testing from the top. Always good practice. Okay. Please plan four days worth meals for me. Munch well and There we go. Done. Here's your dinner menu. Out of all these months, his favorite is Pork stir Fry. Yeah, I came on to Would you like a shopping list for these dishes? Yes or no? Well, let's go ahead and say yes to make sure we can generate a shopping list for more than one item, while our bon appetit that's looking great and looks like all of the items are correct. So we've got Let's expand this a bit. Okay for Pete so we can see our pizza items here. This is our pork stir fry chicken for the for heaters on fish for the fish and chips. Perfect. All right, let's do another run. And this time we'll ask Munch not to generate the shopping list. Let's go ahead and go for all seven dishes. There we go. All seven fish and chips this time is his favorite. Let's say no thanks. We don't need a menu. We've got everything in the cupboard. You got it. Bye for now. Fantastic. Well, I think we've got ourselves a successful proof of concept. We've proved that we can code an app to do what the plan called for, but does it solve the problem? Well, it certainly feels like we're going in the right direction. So planning has definitely paid off. I'm looking forward to moving into M V P. And as Python is very good with handling lists, it still seems like a great language to take Munch to the next level and get it that bit closer to an app that we could get on our mobile devices and give to our friends and family really great stuff and good work. If you've been coding along with me, I think at this point we all deserve a cup of coffee and I'll see you in the next lecture where we get into munches. M V P. 28. Project: Python MVP - Part 1: Welcome back to Idol and this lecture on coding our Munch M V. P here in Python, to all intents and purposes will be coding the same app that we've just built in scratch so that you can see how similar code looks in a text based language like Python. We'll also be looking to cement your understanding of the common coding principles that we've studied throughout the course, and I'm sure you already have an excellent foundation level understanding of these. So let's keep up that traction, Okay, So thinking back to our plan and remembering how we built the Munch proof of concept, let's start with some comments to help us plan out out python coding. Let's start with a helpful blood comment at the very top of our script to introduce the app to any potential collaborators. So Blood Comment starts with triple quotes on ends with triple quotes and inside that we can say this is our munch app, M V P version North 0.1 by applause. And how about a helpful description? Something like. The purpose of Munch is to create a dinner menu from a favorite list of dishes and produce a shopping list of ingredients for the chosen dishes if required. So let's go ahead and do that quickly. All right, that's looking good. So next up, that's you've comments again to plan out our tasks or the three main areas of our coding work, which are, If you're cool, I will say Step one. How many days to actually plan for we need to find out from the user. Secondly, we need to cheese dishes, which will do with a function and, thirdly, another function to build a shopping list if requested. And, of course, before we can move into these three areas of our code will need to define our two functions and create all of the lists that the program is going to need. And we'll need to do this before this code actually runs. So let's create two more comments up at the top of our program to remind us to do that. Let's say a is going to be how functions bay is going to be, how lists and within our functions, we're actually going to have to say to make sure we don't forget. Let's say a one is going to be our cheese dishes function and they, too can be are build shopping list function and African at some some lines just to make things really, really clear for us. Okay, that's looking good. Well, I like to work in this way as the comments help me to organize my coding efforts and stay on track, and I hope you also find this technique useful. Now we can look at these higher level tasks and decide on the order that we want to tackle them in. Perhaps we can use the same approach that we used in scratch, working on each piece of code as it appears in the time context off our program. So let's start with the opening dialogue in Step one of the main flow. Okay, so we don't have the stage this time, so we'll have to start by using print statements to see munches output in the console. This will definitely suffice for our M v P. And if, after testing, we see that Munch has real potential, we can always start to think about mixing our back end python code with some front end code in order to build a user interface that we could potentially put on the Web or mobile devices. We might even choose to use a framework like Django for that. And Django spelt like this. Frameworks can help coders to take short cuts with features like front end templates on easy database hookups so we could store our list data in a database rather than hard code it one day. And Django being a powerful python framework, could be an excellent addition to a python stack for our M v. P, though the console is fine. And at least for now, we're more concerned about how useful Munch is rather than how pretty it looks. So here we go, let's code our first print dialog so we can code print and let's print this string the same one we used in our proof of concept. Hello, I'm much I'll help you. What should you say to plan your dinner menu? Next? Let's create an answer. Variable as Python doesn't automatically store user input like scratched us with its ask block. And let's code a user input to assign the data to answer. So let's say answer equals and let's use a user input function to collect the answer. Now we can add our dialogue string or question. And let's say how many days would you like me to plan? Let's leave a space after that as it's neater in the console and you'll see that shortly Next up we added some dialogue where we said, Okay, I'm going to plan that many meals from your favorite meals list and then we can Katyn ated the strings and our answer variable to join them together to make much feel a bit more personal. So let's go ahead and do that and then we contest. So let's say print Okay, I'm going to plan so we can leave a space at the end of Plan Con Katyn eight or join the string held in our answer variable. And it will be a string because we know that python stores user inputs, strings. Even if the user chooses to enter a number, it won't actually be interdigital float data here in Python. It will definitely be string data, so we should have no problem joining these strings together. And after our answer, we want to join another string on this time of space at the beginning of this strength, and we can say dinner Dinner's from your favorite meal's list. I think we said, And in fact, we're not sure if dinner is going to be plural. So let's add some rockets within the string there to cover the use case where the use of my ask for just one meal so we should not have. Okay, I'm going to plan. Answer Dennis from your favorite meal meal's list. Okay, that looks good. Great stuff. So let's go ahead and run our first test, and you can see that I still need to save my file. So let's hit run and will say first, What better name them munch dot Tie and Pathan will run. Okay, so hello, I'm Munch. I'll help you to plan your dinner menu. That's looking good. How many days would you like me to plan a nice little space for clarity and let's say one. Okay, I'm going to plan one dinner from your favorite meals list. Perfect. We're coding like python pros. Now we know that in our proof of concept, we called our first function at this point to choose dishes. But before we can call the function, we have to define it. We also have to think about our lists again at this point. So again, let's start with the lists and then define the function to use them. So in scratch, we coded list off food we like, and here in Python Weaken do exactly the same thing. Can you remember how to code a list in Python? Well, here we go. You can say food. We like a sign. A list with square bracket syntax. We've now got a named list or a list variable off food we like, and at the moment it's an empty list. So let's go ahead and populate it with our food. We like data. We're going to save the food. We like data as strings, so first up is pizza. We can add a comma space, so it's nice and easy to read on. Let's keep going with the rest of our meals. So next up we had beef burgers. Then we had pork staff fry. Obviously feel free to go ahead and add all of your own meals if you're cutting on with me and you can add as many as you like, but for testing purposes, go with more than or at least seven because that's what I will be doing on. Then we had chicken for heaters, bangers and mash, fish and chips. I think we have next and finally we had our Spanish omelet great, and we've scrolled over a bit, so I'm gonna add a line break, and that's no problem at all. I know you can't see all of this list right now. It's all there and after food we like. We had a blank list called My Menu, which we used the choose dishes function toe auto populate. So let's coat that up and say, My menu is a blank list right now. But of course we need to create a list, even if it's a blank list before we can add things into it using code. All right, so that's looking good. Shall we coat our choose dishes function now? Well, I think we're ready. Let's just take a second to remind ourselves what we're doing here. So we want to call our choose dishes function from down here in our main code. Then we want to take the data held here in our answer variable, which remember is string data and use that to randomly choose that many dishes from here in our food. We like list, which we've created Andi populated manually. Finally, we want to add the chosen dishes to the my menu list, which at the moment is blank and eventually show it to the user. Okay, so let's begin by defining our choose dishes function. And do you remember how to define a function here in python? Well, we can type death for define and then Neymar function. I'm calling mine choose dishes. Next up. We can add parentheses for any required argument parameters and finished the line or the head of the function with the colon. In our case, we require a single argument, which in scratch we decided to call days so that we could pass in the data for how many days or how many dinners. Three User wants us to plan, and now we're ready for the function body. So let's add a line break and check for correct indentation to make sure our block code within the function is indented nicely after the semi colon. Now what I'm going to do to begin with is add a another block comment, and again, I like to do this just to make a note. While I'm working of the tasks that I need to achieve. In this case of sub tasks for the function body and in scratch, we coded our function body in three parts. Can you recall what the sub tasks were? Well, festival. We needed to choose a random dish from food we like. Okay, next up, we needed to check that the dish hasn't already bean chosen. And if not, we can add it to my menu so we can say Jack Dish hasn't chosen, if not to my menu. And finally, if you recall what we did in our brief of concept, we needed to repeat our code until we have the required number of dishes from our user and put present in the mind menu list so we can remind ourselves and say, Repeat, until we have required number off dishes in my menu. Good. So let's tackle choosing a random element from our figure like list or step one first. Now, here in Python, the random function isn't in the standard library off default functions. We can't just code random brackets because the random function doesn't exist here in the default functions. To recall from the lecture on functions that we can either code your own or look for an import to save time. Well, I've done exactly that. I've gone to the python docks and run a search for random, and I could bring that up just to show you. So here we go. I'm in the python docks English for python version 3.6 point seven in this case on our inside the docks Pythons Standard Library on What I did was I ran a search in the docks and can't quite see it on the screen. But there is a search window in the docks. I just entered the keyword random What Python came up with. Waas. The random module. Here it is, and the random module generates pseudo random numbers. But it does other things, too. If I scroll down and have a look at all of the various functions within the random module, for example, the seat function which happens to be a bookkeeping function in the random module, there's a get state function set state function, your various functions for inter GIs like random range Andi. Here are functions for sequences on a list happens to be a sequence inside the random module. That happens to be a ready made function called choice, which can accept an argument in this case, a sequence. This function will return a random element from the non empty sequence if the sequence is empty and index error is raised. So if we pass in a list, variable here as our sequence this functional make a random choice from the list and return it. And we know that this function is present inside a module called random. We know that from this dot notation here, So what we want to do in this case rather than import the whole random module, which we don't really need. We don't want a love these functions. We only want the choice function. So from our random module, let's just import the choice function so that we can use it. Let's go ahead and coat that up so at the top of our script, because we want to make sure we always do our imports first. Before we do anything else. Let's add another comment and let's say this is where we're going to do our imports. And now let's import our function ready for use so we can simply code from random from the random module that we know exists. We want to import, and you can see that that, too, is a key word. And we want to import the function called choice. Okay, so now we're importing the choice function. Let's go ahead and use it. Let's start. And again, I'm just going back into our function body, making sure I have the correct indentation and we can start by choosing a random element from the food we like list to, let's say, choice to call the choice function we've just imported. And we know that that function accepts a single argument, which can be a sequence. So let's pass in food we like. And this code should now make a random choice from the food we like list. And by the time we call this code or rather, we call this function on the body code runs, Ah, food We like list will have already being generated on populated because we do that manually and we're only going to call the function down here all right. And let's create a new local variable here in the function to store the randomly chosen dish so that we can go on below to check if it's in the my menu list and if not at it, I'll use the same name that I used back and scratch. So create a temporary or correction, a local variable called chosen dish, and we will assign the random choice that we made from the food we like list food we like list contained strings, so chosen dish will eventually contain a random string from that list. And that's step one done as we've achieved that outcome. Or at least we think we have so far. I'm going to make a note here. Se done question mark to remind me that I still need to actually test that way. Are choosing around and dish four. I removed my notes, so let's continue on to step two on will test very shortly. Now we can coat a conditional check to check if chosen. Dish is not already in my menu, and we can simply say if chosen dish not in both Python keywords. My menu finished the line with Colon, and when we line break check for correct indentation because we're now adding another block within our conditional if code on this code will only run if our conditional value It's true. We can say my menu dot append open and close brackets and time chosen dish like, say, this code should upend or add our randomly chosen dish onto the end off the my menu list if the conditional code evaluates true and it should evaluate true if the chosen dish is not already in my menu. So I think what we should do now, before we go any further, is to test things out. But of course, none of this cope will run unless we actually call the function. So let's call out Choose dishes function and it really doesn't matter. Whereabouts. We do it in the main code right now, but let's go ahead and put it in the right position or in context. This is where it's going to go. So that's type two dishes. Open close brackets to call our newly created Choose dishes function, and we know the function is looking for a single argument parameter, so I'll go ahead and actually add answer variable, which we are going to pass through to the function, which will become days inside the function a little bit later. But We know that we're not actually using days inside of the function yet. So answer or the data held in answer doesn't really matter at this point is right now we just want to test that the function is picking a random element, checking whether it's not in the list and if not adding it so to check that chosen dish has , Bean added, Let's also print my menu to the console after our function call so that we can see what's going on. Okay, so let's test that Here we go on. How many days would you like me to plan? Well, as we said, the day data doesn't really matter, so I'm just going to say one because we certainly couldn't do more than that. Anyway, at this point, okay, I'm going to plan one dinner from your favorite meals list on Let's See. Much has picked pizza, super, so we seem tohave a randomly chosen dish in my men, you know, let's try again, just to make sure we are getting a random choice each time, and at this point we don't need to worry about deleting the list as here in idle. Our list data isn't stored between runs of our python code. Each time we run our script, or each time we test our script, we are reassigning a blank list to my menu. So no data is persistent. We could always change that later on if we wanted to, perhaps using a say file or database. But we don't need that. M v p stage. Okay, so we know we're going to get a blank my menu list each time so we can safely test again. Let's run again. And this time we're looking for a different dish choice to prove we are actually picking a random element from food we like. Try another one great stuff. This time we've got Spanish omelet, definitely different pizza perfect. So it looks like that step to done. So that's say, done on. In fact, that one definitely is done, So it's not gonna be a question mark on. We've proved random dish is also random on that one's done good, so it looks like steps one and two have been successfully coded. Let's move on to Step three, and now our days argument comes into play. We want to coat things up so that we stop adding dishes to my menu when we have this required number. Would you remember using an until Lupin scratch toe? Add elements to our my menu list until the list length was equal to the day's variable? Well, there's no until loop here in Python. That's just a quirk of the python language or syntax. So how shall we play it with no until loop available to us? Well, do you recall me? Also mentioning that doing something until something else is true can also be expressed as doing something while something else is false or good news. Here in Python, we've got the while loop to play with, so let's use it to loop our function code. While the length of the mind menu list is less than the days argument variable, it's up at the top of our functions. Body block. Let's code while the length going to call the LEC function to get the length off my menu at this point. So while the length of my menu is less than were using the less than comparison operator here, we can type days, which is our days function. Variable argument variable, I should say. And don't forget the colon. Now we need to be really careful about our indentation. We need a block inside of the wildly, which should be indented so we can intent our line to a sign chosen dish and pick around them. Choice. And we can also, in dent our if conditional. But now the if conditional, also has its own sub block of code. Remember the colon here? So we need to intend our code to upend or add are chosen dish into my menu like so. And of course, it's really important to get all of this indentation right. Or Python won't know which blocks to run when, and we'll end up getting a syntax error. Okay, so I think that looking good. But let's pause for a second and think about our data types as we're using an operator up here. And in this case, it's a less than or comparison operator to compare the data on the left the length of my menu with the data on the right, the number of days. So what type of data do we have here on the left? When we call the function to get the length of my menu? Well, we could take a short cut on somewhere in our code. We could coda print statement to print the type off the length off the mind many list, which would require quite a few functions within functions. Or we could look for it in the python docks, which is what I did. And to save time, I can tell you that this data here, when we called length my menu is actually an integer the list links comes back as a whole number. So what about the right side? What type of data is stored here in our days? Argument. Variable. Well, days comes from answer and answer comes from Python asking for a user input. And we've already seen that Python stores user inputs as strings, even if the user enters a number. For example, one or seven person isn't storing that data as an integer or even afloat. It's stored as a strength. If we test our coat now, we're going to hit a nera because we're trying to compare string data with interview data. We can't do that. We can't compare two different data types with a comparison operator. In fact, why not save and run the code now just to prove it to you. So how many days would you like me to plan? Let's go for one, ah, half type era less than operator not supported between instances off integer on strength. And I think we've seen this era before, so can you remember Quick fix for it? Well, how about we convert our answer? Input from a strength into an integer as it's always better to compare to interview is using an operator rather than comparing strings. Comparing strings can give slightly unusual behavior, which is a bit beyond the scope of this course. Other probably mentioned before. But let's think about how we're going to do this. We don't want to convert our answer. String into an integer at this point in our code because we needed a string data here to join it to these other strings. So how about we do it up here inside our function? Well, we need to do is call the integer function to convert our data into integer form. So let's just type integer open bracket and, after all, wrapping around our days, variable close the bracket now days, which we get as a string, will be converted into an integer on our operator is then comparing an interview to an interview much better. So course the proof is always in the pudding. Let's give it a go and test it out. Okay? How many days would you like me to plan? Well, as we haven't tested it yet, let's try with zero. Okay, I'm going to plant zero dinners from your favorite meals, lists as much, and it's given us a blank list. Very good. So let's try again this time will enter an argument off one good on munchies. Picked a Spanish omelet. Let's keep going on. Let's go for the most limiting case off. Seven now. Fantastic. So mansions picked seven meals, and it looks like, ah, while loop is working perfectly. But let's just double check. So we've got 12345 six and seven. Perfect. No more, no less exactly what we want. So we can see that our loot code is stopping when the length of my menu is equal to or not less than the requested number of days. I'm loving it, and now we can say that Step three is done, and in fact, as we've tested everything. Now we can remove this comment code to clean things up, and I'll go ahead and safe. All right, So let's code the dialogues that we want to show after the menu has Bean generated. We wanted to say something like Done. Here's your many So let's coat that with another print statement and you can see that I've forgotten And I can see that because of the strange colors going on here. I've forgotten to add my quotes to make this a strength. So there we go, saved by idle. Okay, and let's test. Let's go for that over here. Let's go for three and I've got three. Done. Here is your menu. Well, that's working, but I'm not sure I like the order of that. So perhaps let's swap these prints statements around. So we're printing. Here's your menu, our ellipsis, and then the menu follows. That should make sense, and you know what we can do? We can make our menu stand out a little more by printing blank lines above and below it. It's above the menu. That's cool, an empty print, and in fact we don't need one below the man, you know. So Let's run that. All right, great stuff. The menus a little bit separate now in the console, which is a bit easier to read. And it does still have the the square brackets and the single quotes and the commerce etcetera because we're printing out the whole list so it does look like a horizontal list. We can always change the way the menu is actually printed later on, if we want it, for example, printing it as a vertical list with numbers or perhaps days of the week on the left hand side. But perhaps we can leave that until we think about the U I. After testing our Munch M V P out on riel users or next, we had some more personalization in a proof of concept on, we chose a random dish from the newly generated my menu, and Munch told us that that randomly chosen dish waas his favorite one. Let's go ahead and coat that up below the menu. We cannot add a blank list or I should say a blank line just to make the menu stand out, and then we can say another print statement. This time I'm going to say house of ALS. These dishes, my favorite has to be on a space. And now we can come. Katyn eight. That with and let's make a random choice, but this time from my many. So he can say choice. Open close brackets on my menu this time and let's see how that works. That's good for six. Okay, so we've got 12345 or having were 12345 and six. And out of all these dishes, my favorite has to be drum roll Spanish on that. Fantastic. Okay, great job that's looking good on. I think we can call our 1st 2 steps How many days to plan on actually choosing our dishes on presenting them to the user. Done. So that concludes Part one on in the next part, we'll get onto building our shopping list. I'll see that 29. Project: Python MVP - Part 2: a case of Welcome Back to Munch M V. P. Part two. And as we've just coated our first steps one and two in our app, we can move on to building the shopping list now, or at least asking the user if they want a shopping list. So thinking back to a proof of concept at this point in our programs flow, we asked our users if they would like Munch to generate a shopping list and looked for either a yes or no response. Using Conditional is to decide what to do next. So let's coat that up again. We're going to use input toe, ask the user a question and collect their response. Once again, we can start by using the answer variable At this point in the flow. Answer was simply overwrite any data that we've previously assigned here in the answer variable a puff by signing any new data that we go on to capture with our input statement. Of course, if we were going to need the original days requested anywhere else in our program, we would need to create a new variable down here, giving it a different name. For example, shopping list answer or something like that. But after we've captured it here past it into our function and reached this point in the code, I'm happy to simply overwrite it using the same name. So let's continue again. That's cool Input on past the string to ask the user question. Would you like the shopping nous for this menu? Question Mark and I like to finish with the space as if you recall its NATO when we ask the use of for an input in the console. So now we can move into our conditional code. Let's say if answer. Do you remember the Equality operator here in Python, setting data versus checking data? In this case, we want to check data so we can simply code are double equals. This time. We're not setting it with checking it. We can say if answer is equal to and let's check for a lower case. Why string and finish with a colon so we can get into our conditional code block and to remind me what I'm about to do, I can say Call the build shopping list. I'm adding a comment at this point just to remind me what I need to do here. And then I can finish my conditional code removing the in dent on in scratch. We used another if statement at this point for the no condition, we could do that here too. But how about we use an else statement this time so we can say house colon in dent on the else condition returns true, we can end the program. The else condition here will catch any input other than lower case why and end the program . That's fine for now. But do you recall me mentioning data validation back when we coded are proof of concept in future. If Munch starts to look viable and we wanted to perfect it, we could use validation at this point to improve this code a little. For example, what happens if the user did try to say yes, but they had their caps lock on, entered an upper case. Why here? We're looking for a lower case string. Why? So in upper case, why would return false? I would also get a false evaluation if the user makes a mistake and hits the wrong key. So it would be annoying to end the program by running our else code rather than giving the user another chance to do what they want or to correct the mistake. But that's for another time. For now, let's get back to it. Alright, so I've added comments here to remind me what still needs to be done. But if we try to test the program with these comments, in place will get an error because we're actually leaving both the if and else blocks blank . There's no code like for if there's no code block for else, the comments are invisible. If we run this code, Python will get to this line. See, it's an if statement. See the semi colon and be expecting an indented code lock. This codes invisible and will actually get straight to the else block and python or say, Hold on. I'm expecting an indented block here. I'm not seeing one. Here you go. Here's a syntax error and in fact, why not demonstrate every guy expected? An indented block here on Pathan isn't seeing one. So in this case, because we've commented inside a conditional block without any visible indented code, let's convert these comments into print statements just to save those annoying syntax errors while we test. So let's instead of a comment, let's say print to do and the same without end program. That's print today and program. Now we can go ahead and test the code as much as we want to before we get around to doing those things and our code will simply remind us. Yes, we need to do something. Okay, So speaking of what we still have to do if the user says yes, here, we need to make our shopping list. So let's have a think about coding up our build shopping list function in scratch. We had seven manually populated lists of ingredients and one blank shopping list that we used the build shopping, this function to populate. And in the function, we used conditional statements to check if each dish was added to my menu. And if it waas, we copied that dishes ingredients into the shopping list. So again, before we actually get onto defining our function, let's think about our lists, and it's time to add our ingredients. So lets model our dish data to say that the dish consists off lest off ingredients which will look something like this. But the ingredients will actually be string data like so on the list. Name will be a variable name. This is exactly what we did in scratch. So let's pad out our dishes in this way. Let's say pizza on. Set the ingredients for pizza to be string saying pizza face ingredient to be tomato source . Terrible spending. Tomato short. Okay, um, let's say Jeez, how about some pepperoni? Sorry if you're a vegetarian, Andi Finally, let's add some nice hot chilies That looks like a pretty decent pizza. We can go ahead and repeat the process saying beef burgers, etcetera. So again, this is a little bit tedious to coat. I'm going to skip ahead in the video to the point where I've defined all my dishes on ingredients. And again, if you're coding along, feel free to pause the lecture while you do yours and then resume. And in fact, I've just spotted a typo here Base base that should be pizza base. OK, so here we go. Okay, so welcome back and you can see that I have now defined all of my ingredients lists. Each list contains a set off ingredients as strings with common separation, and you can see that each list also has a name on the left hand side here. All right, so now that we've got all of our ingredients to find, we're going to need a blank shopping list to put them in. If that dish has been chosen by Munch, so let's go ahead and code up a blank shopping list. And that's cool It my shopping list on a sign, a blank list with square brackets. And I think we're now ready for our second function. So let's go ahead and define build shopping list. Let's define and Neymar function, build shopping list and add the parentheses, even though there's no argument required for this particular function. And we'll end with the colon on a line break to enter our function Body code, which is correctly indented. So are conditional. Code here is going to put the right ingredients in my shopping list, depending on what's in my menu. But we're just doing it in a bit of a round about way. If we actually wanted to link these two lists together, we could make use of a two dimensional or to delist if we turned food we like into A to D list or a list of lists. And by the time the dishes make it into my menu, every chosen dish would also have its ingredients associated with it. So we could simply Code Python to say, for every dish in my many print, every ingredient, and that would be really nice and clean. The downside, of course, would be that the dish names that we give to the user wouldn't be strings anymore. If we use the list of list approach or to delist approach, then our dish names would again be variables. Asil. This names are variable, and these can contain spaces, so they wouldn't be very nice to look at printing beef. Burgers like this, for example, isn't as nice to the users printing it like this. Another approach that we could use would be to remodel the way that we use our data. Essentially, if we tell Python that all dishes are a certain type or class off object, and that they all have various attributes like a dish has a name, a list of ingredients, maybe even a picture or a recipe. We could then easily create multiple versions or instances off that dish object that would be really efficient because all the ingredients would always be associate ID with a dish object. And the dish object could still use string data as a name to keep it user friendly. I know I'm jumping ahead, but I just wanted to let you know what's possible as you continue your coding journey. This way of thinking or modeling objects in code is called object oriented programming, and it's incredibly popular. So now, at least if you see the term object oriented programming, you'll have a basic understanding about what that is. It's a way of structuring riel world objects in code, but for now, let's finish our build shopping this function okay, so we can coat if this string pizza being careful to check the correct case. Remember, we're looking at this list as these are the items that are going to be in my menu. If pizza his in my menu Colon on line break New block, we're going to add or upend the list pizza to my shopping list, which will actually add all of the ingredients into my shopping list. So let's do that. We can coat my shopping list dot depend and we can upend the pizza list, which is a lower case p This time it's a variable name. So know quite marks. We're now going to add pizza, which will add all of the strings contained within the pizza list into my shopping list. If the string pizza this one makes it into my menu, and that will happen when I choose dishes function runs before the build shopping list function is called. So, of course, we need to duplicate this code and create a number of, if statements, checking for every dish that could possibly appear in my menu. So what we can do is copy and paste and be very careful, very, very careful doing this, because we need to make sure that we using the correct indentation and that we change all of our string and variable data without missing anything out. So go ahead and do it. So how many have we got? 1234561 more and seven. Okay, now we just need to work our way through carefully. So here we've got beef burgers. Andi that's looking for variable cold beef burgers like so if the string pork stir fries in my menu. We want to upend con spell, stay the pork stir fry list into my shopping list on we keep going and as it's a bit tedious, Do you know what? I'll just skip ahead in the video and there we go. I think I have now got them all in my list. We're checking for the correct string Names here on were pending the correct list names here with our conditional code. A note that is definitely a Siri's off if statements we want here not else if or else because we want every if statement to evaluate. Essentially, we want to check them all. If we used else. If on our burgers, for example, like so And we actually did have pizza in my menu. So this conditional returned or evaluated truth this else if statement wouldn't even run, it wouldn't check its condition. So it end up with missing items, definitely not what we want. So if statements are what we want at this point, because we want to check for every dish, Okay, so I think our build shopping list function code is well defined and ready for testing. But of course we can't test it unless we call the function. So let's go ahead and do that at the correct point down in Step three of our main code. Now we can remove our print statement for our to do, and we can simply say that's called build shopping List. And of course, we're calling a function. So we still need the parentheses, even though there's no argument required. And one more thing to do before we can fully test the function. Can you think what it is? Well, how will the user actually see the shopping lists? Data? Of course. We need to print it now. He could print it from here in the main code, but how about we print it from here inside off the function. This will keep our code nice and modular. In fact, if we're taking this approach, we can create our blank list here inside the function to. But first, let's print my shopping list and we'll print it. After all of this code has run on, the function has being called. And if we are going to keep things modular like this, why not actually build or create our blank shopping list from inside the function as well. We can do that here at the top of the function. So when the function build shopping list is called, a blank shopping list will be created. All of our conditional code will run, and then we'll print the shopping list at the end or from inside the function. So one line of code down here is what we need. Of course. Now we would need to take a bit of care if we're creating the variable my shopping list inside of the function. It's local in scope, so we have to print it from inside of the function as well. If we create the list or variable, name my shopping list from here inside the function and then try to print it from down here in our main code, it won't be visible because it's local in scope when it's created inside of a function here in Python. And perhaps you want to take the same approach with the choose dishes function, making that code a bit more modular. We could move all of our print coat into the function itself. Let's go ahead and do it actually well, we're going to leave is the function call down here in our main code. So after the function has run, really careful with putting the in dent in the correct space. Here we go. We're going to run all of our print statements from inside the function Onda. We can build the blank my menu list at the top of the function, just like we've done with the build shopping list. Making sure, of course, has always we get the correct in dent. So now, in cheese dishes creating a blank my menu, then we're checking the length, etcetera on running this code to put random dishes into it on. Then we are going to run all of our print statements, so codes looking much more modular now, which is great. But we didn't finish testing our build shopping this function. So let's go ahead and do that now. Okay, here we go from the top. How many days would you like me to plan? Let's go for three on munchies. Chosen three dishes out of his favorite. It's Spanish omelet. And would we like a shopping list? Well, so far we know that are slightly redesigned. Choose dishes. Function is working nicely now that we've moved some of the code into the function itself, which is great. So we get onto testing our shiny new build, shopping this function. Would you like shopping list? I have to say yes at this point. Lower case Why and we get a name era. My menu is not defined. So let's see what we did here. Ah, and Python is telling us that there's an error occurring somewhere inside of our build shopping list function. In fact, it's in build shopping list if pizza is in my menu. So when we're running our first conditional check here and we'll look and see if the string Peter is in my menu, guess what? Python can't see my menu. It's saying my menu is not defined because we've now created my menu inside off another function. This is local in scope, so this function can't see it. We'll have to go back to creating my menu in our main code. If we do that, my menu is now global in scope, so we can use it in both of our functions. Just a little example of how scope can easily catch you out. Okay, so let's save and run again. Under a fresh test. Here we go. So we asked for three before. Let's ask for three again. Every guy, three dishes chosen. Pizza is much favorite. Would you like a shopping list? Yes, please. Excellent. So we've now got a list of ingredients. Okay, so it does look a bit strange as we've still got all of the square brackets and commas, etcetera. Now, I did mention that we weren't going to worry too much about a pretty you I this stage in the apse lifecycle. But how about we tidy up the list presentation? Just a little. Let's make both of our lists. Ingredients. Andi dishes appear a little bit neater in the console as menu is the first list that we print. How about we look at tiding up my menu? Okay. So rather than printing the entire list in line like this with its square brackets, comments and quotes, how about we do something a bit different? How about we print each dish on a new line without all of the other characters? This will give us much more of a clean vertical star list. So here's a question. How could we use code to iterating over the my menu list and perform an action like print each element in the list over and over again until we've got them all. Well, how about we use a four loop iterating over every dish in the my menu list and print that dish on a new line that, say, full every dish in the my many list Colon line. Break with an indent print every dish. Let's give it a guy. It's ask much for five this time. Ah, much better each dish on a new line printed without all of the comments, quotes and square brackets that is going to be much easier for us to test with. Four loops are great for doing this sort of activity because we can essentially say, for every element that exists in a particular list or sequence. Do something, and I have typed dish here because that makes sense to me in this code. But the python syntax means I can call this element variable anything I want. For example, I could say for Element in my menu print element in the code will work just as well, but let's go back to dish as that makes sense here in our program and because the code loops over and over again until every element in my menu has been covered. The Prince statement is called again and again. That means it prints on a new line each time it runs, giving us the vertical list presentation that we wanted nice. So shall we do the same thing for our ingredients list? Rather than printing the entire shopping list? We can use a for loop again, but let's stop and think for a second. My shopping list is a to D List a list of lists, and you can see that back here when we tested it with a print statement, the first dimension in See the double square brackets. The first dimension. All of this is a list made up of dishes. The second dimension. Each dish list is a list itself made up of ingredients. So if we only have 14 loop here on recode, something like for Element in my shopping list, print the element. Let's see what happens when we run it. Let's ask for Well, let's go for seven so we get a nice long list. There we go. You can see how each dish is now on its own line, and we have 123456 We have all seven dishes on an individual line. That's partly what we want. But we're still printing a list in this case, the sub list on ingredients with all of its special characters. So how can we fix this? Well, do you remember me saying that we can nest loops? How about we do that? We could nest to four loops and only print the second dimension off my shopping list. Here's health. First, let's change our element variable name to something more unique. So let's say, for every dish in my shopping list, that's the first dimension. And for now, I'll delete the Prince statement and say for every dish in my shopping list and then nest another four leap and say, for every ingredient in every dish print ingredient. With the 1st 4 leap, this one python should go over the first dish in the list. And then, with the 2nd 4 leap, this one python should go over every ingredient in that dishes list and with a Prince statement, print every ingredient on a new line. It will then move over to the second dish, printing all of its ingredients the third, the fourth and so forth repeating until every required ingredient is on a new line. Okay, so let's see it in action. Its Plan. Seven Meals With a Shopping list. Wow, nice long list. And, of course, above are four. Luke are nested for loops. What we could do his prints of blank line just to separate the top of our list from the rest of our text here in the console just to make it cleaner. Fantastic. So I'll call that done for now, and that's a nice opportunity for you to see four loops in action. They really do give Python tremendous power for manipulating list data. So let's think if the user did ask for the list on, we've just printed it. Perhaps we can print another blank line and then finish bought Printing a string. Well, uh, bon appetit. That takes care of the yes case for the shopping list down here on what about the else case where the user enters end or anything other than lower case? Why? Well, now it can remove out to do on. Let's say you got it. Bye. for now and Smiley Face and that's it. I think we're done. Let's just run a couple more tests to make sure our final dialogues are working nicely. Let's say please plan for we've got four beef burgers is the favorite. Would you like shopping list? Yes, please. There it is. Well, our bon appetit. Fantastic. Let's try again. That's good for three this time. Would you like a shopping nurse? Let's say n you got it. Bye For now, I'm perhaps we can close with a blank line above that just to keep it neat. I'll save the code and we're done. Well, outstanding. It looks like munches python, M V P is also complete. Of course, in the real world, we could start testing it out. At this point, we could give the app to our friends and family to find out if other people find it useful to, Of course, they would need python installed on their machine to test it, which could be a limiting factor. Maybe we need to take the M V p a little bit further and give it a front end if we were going to do that. But if we find that our friends and family like it. It certainly would be a viable product to continue working on. But perhaps you just wanted to code Munch for a bit of fun. And that's great, too. Coding fun. Absent games is a great hobby because it's such a good creative outlet, so really well done for completing Munch and especially well done. If you've been coding along, I do hope you enjoyed the challenge. And by the way, if you're keen to challenge yourself a little more, take the end of course challenge. Or I've set a few tasks for you to try and coat some simple Munch improvements on your own . And don't fear I've provided the solutions just in case you need them. Well, that wraps things up for this lecture as always. Thanks for watching, and I'll see you in the section recap 30. Project: Recap: well, welcome to the Section five recap Here in this fairly meaty project section, we set out to put all of our theory on common coding concepts on our foundation level, knowledge of scratch and python into practice in order to build a simple app. We looked at the problem that we wanted to solve and decomposed it. We came up with a possible solution and composed a plan sketching up how we could deal with smaller tasks and features. And we use pseudo code to visualize how app might work. Next, we coded up a proof of concept in scratch, and when we saw that our app was technically possible, we used a slightly more powerful text based language python to create a minimum viable product. I think the M V P turned out really well, and I, for one, actually use a version of it in the real world to plan my meals and build my actual shopping lists. And don't forget, you can improve on Munch even further by completing the end of course challenge. So let's look back and ask the question. Did we achieve our goal? Well, let's think in building Munch we saw and practiced how code runs. We fixed some bugs. We used some pseudo code. We decomposed a task. We commented our code. We definitely had practiced with different data types. We used operators. We coded loops, control flow and store data in variables. We definitely coded plenty of lists and used functions to make our code more modular. Wow. So I think that we can call that a success. Great job. And by the way, if you'd like the source code for scratch or python, just follow the links in the bonus section. Well, that concludes this section, and indeed, the bulk of the course. At this point, I'd like to say a huge congratulations to you. You can really code take a moment to think about that. You built a nap, you've coded in scratch and you've coated in python. You can coat now. Perhaps it's time for another well deserved cup of coffee to celebrate. Thanks for all the hard work. And I'll see you in the next section. 31. Course Summary: we're once again a huge congratulations on thanks so much for completing this coding for beginners. Course, you've probably come a very long way. As a lecturer, I have three personal wishes for you right now. Firstly, I hope that you had some fun. Secondly, I hope that you don't feel like you're on the outside of that exclusive coding club looking in anymore. And thirdly, I hope you've bean inspired and feel more confident to go on experiment with code. If there are two big takeaways from this course, they should be the fact that code allows us to unleash our creative potential and that you can code. So let's take a moment to recap. At the beginning of the course, we looked at what you would be able to do. By this point. I mentioned that you would hopefully say that you can understand many of the common building blocks found in most programming languages and we covered that in common coding concepts. I hope that you can now say that you understand these at a foundation level. I also said that you would be able to call yourself a coda. Do you feel like a coder now? I certainly hope so. I mentioned that you'd have a good understanding off the scratch on python interfaces so you could go on to keep experimenting and practicing on your own. I hope that you feel comfortable enough to do that now. And I've included some resources for you in the bonus section if you are king to keep exploring scratch and python, and there's some information there on how you can learn other languages to on where to begin. I also covered the possibility of progressing toward a new job. Or if you have kindled an interest in coding now, perhaps you can think of a way to bring coding skills into your current job. Well, perhaps you're thinking about a new job or a career change again. There are some useful resources over in the bonus section to help you explore some possible next steps, and I really hope you find those links useful. So what next? Well, good question coding for beginners was created to be a springboard into a more creative future, and we always hoped that you would be keen to move on. Here's some ideas on where to go next. Firstly, why not sign up to receive news on our next course where possible, via voucher codes, etcetera will be offering discounts to all of our students on all future courses, So don't miss out on that. Go ahead and visit applause interactive dot com and add jury Melt our followers list now. Secondly, explore the bonus material in the next section. As I mentioned, there are some great resources there, and as this is a very early version off, this course it's still growing will be adding resources into the bonus section continually over time. Another reason to go ahead and join our mailing list as will notify you when we add some useful content at your experience level. The links in the bonus section may help you to learn your first language. We've already used to languages during this course scratch and python. But while we have touched on them, there's always more to discover. Why not take a beginner's course dedicated to one of those languages to keep growing your experience? Or maybe you're interested in something else, like html, CSS, JavaScript, c shop, ruby or even go. Many students ask us what's the best language to learn, and the answer to. That depends largely on what you'd like to build either toe have fun or to build an application or game of some kind. We've already explored the fact that the best language depends on the application, so it follows that the best language to learn depends on what you would like to do with your coding skills. We've got you covered. So check out the bonus section and you'll find some useful tips there. And that concludes our summary again. Thank you so much for choosing coding for beginnings. We appreciate you choosing us as a learning provider. Don't hesitate to ask any course related questions, offer feedback or leave suggestions for improvement via the usual channels. And for general coding related questions, check out the links in the bonus section for where to find help. There's a massive community out there, one of the most active on the Internet and almost always willing to help beginners. Oh, and finally, please do leave a kind review for future students. If you enjoyed the course bye for now, 32. Bonus Content: Hello and welcome to coding for beginners. Bonus content. Here we've put together some bonus content links and resources that we think you'll find really useful at your stage. If you're looking to take the next steps as a coder or firstly, if you enjoyed coding the man shop and you'd like to challenge yourself a little more, why not take the end of course challenge? When we coated the app, I mentioned a few possible ideas for improving it. In the end, of course, challenge are set three assignment tasks. Each task is a small coding challenge to make incremental improvements to munch. And if you get stuck, you'll find solutions there in the assignment. So fear not. Do you think you can complete all three tasks? Well, I'm sure you can. Next up, we've created a page on our website with some fantastic resources, such as links to find free scratch and python projects. Information on various code editors that you could try where to find answers to coding related questions. Where to look for coding related documentation on where to go in order to learn a coding language for free, as well as some other useful links you can find the page by visiting our website at applause interactive dot com and simply following the really clear link through to coding for beginners on the bonus material. And don't forget, this page is being updated frequently with new resources as they become available. So why not go ahead and book market now? Well, all that remains is for me to say, Enjoy those resources and I hope you find them really useful. And finally, good luck on your coding adventure. Please do leave a rating. If you enjoyed the course in order to help other students to discover it. Thank you.