Table of Contents |
Now that we have the initial set of high-level steps, we will want to start breaking some of the steps down further, if it makes sense, to start defining what the overall program should do. Let’s take a look at the steps and start to define patterns. In particular, we want to think about what conditional statements may need to be used, what statements could benefit from the use of a loop, as well as what pieces of code may be repeated which could be broken down into a function or method. Remember back to Unit 1 when we started to define the algorithms for the guessing game. We looked at ways to break things down through conditional statements and loops in the algorithms.
To help with finding patterns with conditional statements, we should start by looking at situations where the program may need to branch. Typically, a good example may be after user input or after a calculation. If we’re prompting the user for a value, we should think about whether we need to do different things based on the input. After a calculation, do we have different output based on the result of that calculation? Some of these calculations and results may have been performed already.
When it comes to loops, there are some common ways to use them. If we find ourselves needing to perform the same task over and over again, using a loop would help with that. An example could be a program that has a menu of options. We may have the user continuously being presented with a menu of options until the user enters an option to exit the loop. There are other situations that would benefit from using a loop such as iterating through a list or other data collection type, or performing calculations on a set of criteria that requires a loop like calculating averages, reading data from a file, etc.
Remember that a for
loop is one that would be used when you have a known number of times that a loop should be executed prior to the loop. A while
loop should be used if it is an unknown number.
With methods or functions, this generally would be useful if we have a set of statements that would be used in more than one place in a program. Think of all of those times in past code examples when you needed input and provided output. Rather than writing the lines of code to perform those tasks, we can simply call a function or method that performs those repeating tasks. To make functions or methods useful to multiple programs, they should only perform a single simple task.
Remember in our game, we based things on the initial criteria and questions. Having those specifically defined is important since we want to ensure that we’ve captured each criterion and consideration. Here is the example of questions and answers we originally had:
EXAMPLE
Scenario 1:
Let’s break down the logic as that’s the crucial part of the program to determine how the game plays. There will be other parts of the program that may also be needed but we’ll focus purely on the logic here.
One initial step is to determine what the variables may need to be. A good starting point would be to identify the variables that would be used to store values. This would include things like the player, the dice, the condition for whether the player won or lost, the point value, and the rolled value. The other part is to define patterns. With the full scenario (scenario 3), we’ll see that steps 4-6 repeat until the condition is met. In the first and second scenarios (1 and 2), the player could win right from the start.
Let’s start to break this down into pseudocode.
Remember pseudocode is English-like statements that describe the steps in a program or algorithm. It is a way to layout logical steps but not get too lost in syntax.
So, based on our steps above, we can start defining these variables and patterns that we see.
EXAMPLE
This is what we have so far in terms of the logic for our program. We started to create some variables like “rolled value”, “player status”, and “point value”, although not using standard variable naming conventions since it is only pseudocode—but we can start to see where they would come in. We also see that we’ve run into a situation where we have some repeated items that are obvious to create a loop for. In revising using loops, it will look like the following:
EXAMPLE
Now that we have the logic in place, it should be easier to build this further, one layer at a time. In every program, there are aspects where we may have some of those items that are going to need to be expanded on but at this point, we’re looking strictly at the logic. For example, with a roll of the dice, think at a high level. What should this actually look like in code? We’ll get into that later on.
When it comes to your own program, you’ll want to do the same thing in terms of the overall logic. In the next lesson, we’ll start expanding this to another level.
This is a great starting point, but we now have some additional questions. For example, how do we store the information about the dice or even how many sides are on the dice? Someone who plays craps may know that they are using a six-sided die but there are four-sided, eight-sided, 10-sided, 12-sided, and even 100-sided die as well. We need to consider this as part of the actual program. We need to think about what a die consists of and what would be needed. For a die, we need to know how many numbers there are and how the die is rolled. In our case, each die has six sides, so the number needs to be between 1 and 6. By default, we can set the value to 1.
Now that you have an example, it’s time to start with yours! Break down the steps and find where those repeating patterns are. Are there certain conditions or examples where you can see those items being repeated? What variables exist that you would want information stored in? These are some things to think about with each of those steps to really break this down further.
In looking back at the Drink Order program from Unit 1 again, we had a few drink choice options. We defined the following four potential runs of the program based on the selection in the last lesson.
EXAMPLE
EXAMPLE
This would end up being a bad example for an entry for Part 3 of the journal, as this consists of specific choices rather than what the choices were. Remember that you want to break down the steps prior to actually developing the code. The logic is the part that you want to consider, as that generally is the most complex part. Although we already had a solution in place with the code, you should find it helpful to see what that would look like as pseudocode.
If you remember this completed program from the end of Unit 1, we had the initial menu structure of what can be selected:
EXAMPLE
EXAMPLE
This gives us that first set of criteria that we can build on. Each of the items based on the drink selection has its own selections so there is no overlap between them. Next, we can start to add the components for the water
EXAMPLE
Next, we can work on the coffee part. Each of the choices and selections could overlap which is different than with the water choices. In the water choices, the ice selection would only be there if cold was selected for the heat. However, with the choices, if coffee is selected, each of the selections is independent of one another. This is something that you want to think about when designing algorithms and finding patterns.
EXAMPLE
Notice with our algorithm that we only looked at the instance where items like decaf are set to yes. We don’t worry about the “no” input because for any of these, we’re not adding any items to the outputString. Lastly, we’ll just have to complete the algorithm for the tea. This will be easier as it’s only doing a check between green and black tea.
EXAMPLE
This concludes this part of the algorithm around this program.
Directions: This is a time for you to take your different examples/scenarios and work through each to build out the algorithm and determine what patterns you have. Think about each scenario but also consider if you may have missed any scenarios, as there may have been other factors or paths that may have been missing.
Source: THIS CONTENT AND SUPPLEMENTAL MATERIAL HAS BEEN ADAPTED FROM “PYTHON FOR EVERYBODY” BY DR. CHARLES R. SEVERANCE ACCESS FOR FREE AT www.py4e.com/html3/ LICENSE: CREATIVE COMMONS ATTRIBUTION 3.0 UNPORTED.