In this course, we're going to write some code and create an application. We will create a basic guessing game where you guess a number between one and ten. This course is part of a series of content designed to help you learn to program with the Python programming language.
Learning Objectives
- Operators, Conditionals, and Callables are three important components of the Python runtime
- Operators enable us to perform operations on objects
- Conditionals enable us to make decisions in code
- Callables enable us to perform actions in code
Intended Audience
This course was designed for first-time developers wanting to learn Python.
Prerequisites
This is an introductory course and doesn’t require any prior programming knowledge.
Hello, and welcome! My name is Ben Lambert, and I’ll be your instructor for this course. This course is part of a series of content designed to help you learn to program with the Python programming language. Should you wish to ask me a specific question, you can do that with the contact details on screen. You can also reach support by using the email address: support@cloudacademy.com. And one of our cloud experts will reply.
It’s time to write some code and create an application. Albeit, a basic application. In this lesson, we’re going to create a basic guessing game where you guess a number between one and ten. Let’s recap some things that we know about Python. The Python programming language is used to create and control objects. And using the combination of operators, conditionals, and callables, we can actually create an application.
The language includes a wide range of operators which can perform operations on objects. For example, the comparison operator == is used to compare two objects for equality. The assignment operator = is used to bind a name to an object. And the list of operators goes on for a while. The language includes syntax for making decisions using conditional statements. The if-family of keywords enables us to make decisions based on boolean values of True and False. For example: if a user is authenticated, allow them to perform some action.
The language enables us to perform actions with callables. Callables such as functions and methods enable us to provide input -> perform some action -> and receive output. Functions are a common type of callable because they allow us to create a standalone unit of code that we can call later in our code. The python runtime provides several built-in functions which fall into several categories, based on their purpose.
Two of those functions are the input and print functions. The input function is used to prompt a user to enter text in a console window. And the print function is used to display text in a console window. With these three features in mind: Operators, conditionals, and callables, let’s write some code. Let’s talk about the requirements of the application. We’re going to build an application that prompts the user to enter a number between 1 and 10.
If the user guesses the number correctly we inform them that they won. If the user guesses incorrectly we prompt them to guess again. That’s it. Those are our only requirements. We know that the user is going to guess a number between 1 and 10. We’re going to compare that guess against a number that we select. Let’s bind the name answer to the integer with a value of 9. This is what we’ll compare with the user’s guess.
Next up we need to prompt the user for a guess. We can use the built-in input function to accomplish this. The input function accepts an optional string as input. If provided the string is displayed to the user as a prompt. The input function reads whatever is typed into the console. It waits until return or enter is pressed. And then returns a string containing whatever was typed into the console. If we ask Python to compare a string to an integer it’s not going to understand what to do.
We need to compare similar object types. Which means we can change the guess into a string. However, we’re going to turn the input into a number. Even if we enter text into this prompt that represents a number, Python only sees a string. So, how do we turn this input into a number? Python provides built-in functions used for creating built-in object types such as strings, integers, booleans, etc.
The int function is kind of cool. If we call the int function without any arguments it simply returns zero. Which is the default number used for an int. That’s not the cool part. The cool part is that the function also can determine if a string is actually a number. If so, it returns a new integer object containing that number. This function can also throw an error if the string provided isn’t actually a number.
Notice that we’re rebinding the name guess. First we bind it to the object returned by input, which is a string object. Then the next line passes that string as input to the int function. Since this returns an integer object, the name guess is now bound to the integer object. Python enables us to rebind names to other objects. This allows us to reuse names while we work on producing the final desired object type. As you start using Python to solve problems you’ll commonly need to perform a few operations before you have the object types that you actually need.
Okay, when the interpreter reads these lines of code, we’ll have two name bindings. Answer is bound to the integer 9. And guess is bound to an integer containing the user’s guess. Now we need to compare these two objects. We can do that using the if keyword, our two objects, and the equality operator. The equality operator compares two objects and determines if they contain the same data. So: “if guess equals answer” followed by a colon and a new line. The code that we want to run if this condition is true begins with the standard indentation. Then we call the print function the string input: ‘you win!’
If the guess and answer match then this text will be displayed in the console. If the guess and answer don’t match then we perform a different action. So, we’ll type else followed by a colon and a new line. We’ll use the standard indentation, followed by calling print to inform them to guess again.
Okay, with just these few lines of code, we have a basic guessing game. In order to run this code, I’ve saved it to a file. I’ve named my file playground.py. The Python runtime enables us to specify the code file to run upon startup. To run this I’ll run the python3 application and specify the playground.py file. Notice it prompts us with the string we provided to the input function. Let’s test both paths through our code. First, let’s see what happens if we guess correctly.
Notice it displays the string ‘You win!’ This is actually kind of interesting because it implies that our code flows through this path without error. Let’s run this code again and see what happens if we enter an incorrect answer. Notice it tells us to guess again and then the application stops. In order to guess again we currently need to run this code again.
With just these few lines of code, we’ve created a basic guessing game. And while this game is basic it demonstrates an understanding of operators, conditionals, and callables. These name bindings are each set using the assignment operator. The comparison of guess and answer is performed using the equality operator.
Once we have our answer and guess our code has to make a decision. If they match then we want to perform one action. Otherwise, we want to perform another. The if-family of conditionals is well suited to model this decision. If the two integer objects are equal then we perform an action using a callable. Specifically the print function. Using the else keyword, we can determine what to do when the guess and answer doesn’t match. And again, we take action using the print function.
While this code does enable us to play, it doesn’t stay running when we guess incorrectly. We’re required to actually run the code again. This is something that can be fixed using a while loop. However, a rabbit hole for another lesson.
Okay, this seems like a natural stopping point. Here are your key takeaways for this lesson:
- Operators, Conditionals, and Callables are three important components of the Python runtime
- Operators enable us to perform operations on objects
- Conditionals enable us to make decisions in code
- Callables enable us to perform actions in code
That's all for this lesson. Thanks so much for watching. And I’ll see you in another lesson!
Ben Lambert is a software engineer and was previously the lead author for DevOps and Microsoft Azure training content at Cloud Academy. His courses and learning paths covered Cloud Ecosystem technologies such as DC/OS, configuration management tools, and containers. As a software engineer, Ben’s experience includes building highly available web and mobile apps. When he’s not building software, he’s hiking, camping, or creating video games.