What Is It Caller When a Program Runs Over and Over Again?
Chapter 1. Starting to Lawmaking: Finding your fashion
Writing programs gives you the power to control your PC.
Almost everyone knows how to employ a computer, just few people take the next pace and larn how to control it. If you use other people'due south software, you will always be limited by what other people think you lot want to practise. Write your own programs and the just limit volition be your own imagination. Programming volition make you more than creative, it will make yous retrieve more precisely, and information technology volition teach you to clarify and solve bug logically.
Practise you lot want to exist programmed or be the programmer?
Programming lets you practice more than
Y'all've got problems to solve and work to exercise, but your existing software doesn't quite cut it. Even with all those programs on your computer, you still need to practise something different, something specific to you .
You want to do more than with your computer. You desire to take command .
Learning to program gives you lot the power to create and solve .Learning to programme puts you lot in charge.
But, how does programming work?
Allow's expect at a simple game written in Python.
Simply what are one thousand and gauge?
You lot might be wondering what g and guess are in the code. They are chosen variables and they're used to go along track of data in the computer'due south retentivity.
Watch it!
Be careful with = signs in code.
Programming languages use = signs for dissimilar purposes. In most languages (including Python), a double equals (==) is a examination for equality. It means, "are these two things equal?" In contrast, a single equal (=) is an instruction (known equally assignment) that means "prepare the value to."
A variable is really but a label for data. So if the user inputs "3" at the keyboard, then judge volition be set to the number 3, and whenever the computer reads guess, information technology will read it as the value 3.
So how do you run your code?
There are two things that you will demand to run the guessing-game programme: an editor and an interpreter .
The editor saves the lawmaking you write into a file on your hard disk drive. The code (sometimes called the source code ) is simply text, and information technology can be written and read by humans.
But computers can't process text meant for humans , at least not very well. That'southward why we need a tool to translate the human-friendly source code into the binary 1s and 0s that computers do understand. That's what an interpreter does. In this book, an interpreter called Python is used.
So we need an editor and a Python interpreter. Fortunately, Python three comes with a built-in application chosen IDLE , which does both jobs and more. IDLE enables you to write and edit Python code, information technology translates this code into binary form, and finally, it runs the Python 3 programme. Considering of this, IDLE is known as an Integrated Evolution Surround .
Let'southward run across these steps in action.
Create a new program file
When you first start IDLE, it displays a window called the Python Shell . Select the New Window selection from the Python Shell File menu, which creates a new edit window for you. Input your programme code equally text into this edit window and y'all'll be on your way.
Practice this!
Become ahead and open up a new IDLE edit window and blazon in the code from Sharpen your pencil.
Prepare and run your code
The adjacent pace is to set your program code for execution. To do this, select File → Save from the carte to save your programme lawmaking to a file. Choose an appropriate name for your program.
Python programs are usually saved in files that stop with .py, so let's call this program game.py.
It doesn't really matter to IDLE which directory you save the file in. Some coders like to create special directories for each new programming projection. But for now, only save the code in some directory that'due south piece of cake to remember.
At present, permit's see what happens when nosotros run the program.
The plan needs to practice more.
At the moment, the guessing game tells the user whether his guess is right or wrong, but nothing more than that. It might exist more helpful if the program displayed more informative messages, such as whether the estimate is higher or lower than the correct answer. That would aid the user to hone in on the right answer the next time the programme is run.
We can do this past changing the code. Simply in what manner?
Encephalon Power
Think about the original code. You will need to employ more than just print() commands to provide more informative feedback. What else volition you need?
A programme is more than than a list of commands
You lot could create a programme that was just a listing of commands. But you almost never will. This is because a simple list of commands tin only be run in one management. It's just like driving down a straight slice of road: there'south really only one manner of doing information technology.
But programs need to be much smarter than that.
Codeville: Your plan is similar a network of roads
Programs need to do different things under different circumstances. In the game, the code displays "You lot win!" if the user guesses the number correctly, and "You lot lose!" if not. This means that all programs, even really simple programs, typically have multiple paths through them.
A path refers to the set of instructions that the computer will actually follow (or execute). Your code is similar a street network, with lots of sections of code connected together just similar the streets in a city. When you drive through a city, you brand decisions as to which streets y'all bulldoze down by turning left or correct at different intersections. It's the aforementioned for a program. It also needs to brand decisions from time to time every bit to which path to accept, but for your lawmaking, it is non like driving along a road, it's executing a particular path .
Permit's look in more item at how a plan decides which path to take.
Branches are code intersections
Driving down a street is piece of cake. You lot need to make a determination just when y'all get to an intersection. Information technology's the aforementioned for your program. When a program has a list of commands, it can blindly execute them one afterwards another. But sometimes, your program needs to make a decision. Does information technology run this piece of code or that piece of code?
These determination points are chosen branches , and they are the route intersections in your lawmaking.
Your plan makes a conclusion using a branch condition . A branch condition has the value true or false . If the branch condition is true, it runs the code on the true branch. And if the branch condition is false, it runs the code on the simulated branch.
if/else branches
Yous need to amend the game program to give more informative messages to the user.
But what will the paths in the program look like?
The Python lawmaking needs interconnecting paths
The solution's mapped out, and now nosotros know that the program code will demand to accept paths that match this:
But isn't at that place a trouble hither? In the pattern there are many interconnecting paths, but so far, we have just written code that contains only 1 co-operative:
if guess == v: print("Y'all win!") else: print("Y'all lose!") In the new code, nosotros will demand to connect two branches together . Nosotros need the second co-operative to announced on the false path of the first.
So how do you connect branches together in Python?
Python uses indents to connect paths
The code inside the if and else statements is indented . This isn't but to make the lawmaking pretty. In Python, indents matter . Let's consider a unlike piece of example code: something that will decide if you can drive downtown. Python uses indents to connect a sequence of commands together to form paths.
Then how practice yous connect branches together? You only indent the second co-operative in by one more than level .
Watch information technology!
Indents matter in Python.
Be careful how yous indent code in Python; if y'all don't indent your lawmaking correctly, your code might practise something wildly different from what yous look.
You should now have enough information to become fix the code, but earlier we exercise that, let'due south take a look at how IDLE helps yous indent lawmaking.
The users still don't like it.
The plan works, and at present generates extra feedback, but in that location'due south a problem. If the users desire to take another estimate, they have to run the program again. They really want the program to keep request them for another guess until they finally become the correct answer.
Can you see what the problem is?
How do we become the calculator to practise something repeatedly? Should we just make a copy of the lawmaking and paste it at the end of the file? That would make sure the user is asked twice. But what if they need to make 3 guesses ? Or 4 guesses ? Or x,000 guesses ? What well-nigh the instance where the guess is correct?
The guessing game program needs to exist able to run some code repeatedly.
Loops let you lot run the same piece of code over and over again
Programs often need to proceed running the same piece of lawmaking many times. In addition to branches, programming languages also provide loops .
Loops are a petty like branches. Only similar branches, loops have a condition (the loop condition ) that is either true or imitation. Also, like the if function of branches, if the loop status is true, then a loop volition run a given piece of code. For a branch, this code is called the body . For a loop, information technology's called the loop body .
The big departure between a loop and a co-operative is how many times information technology runs the code associated with information technology. A branch will run its lawmaking merely one time. But a loop will run the loop body, then check the loop condition again and, if information technology's still true, it will run the loop trunk again. And again. And over again. In fact, it will continue running the loop body until the loop condition becomes false .
Python's while loop
Programming languages have lots of different means of creating loops, but one of the simplest means in Python is to use a while loop . Here's an instance:
This is what the loop looks like when you write information technology as a Python while loop. The code keeps asking the question "Are we there?" until the user types something other than no. This is what it looks like when it runs:
Did yous discover that you had to prepare the value of the reply variable to something sensible before you started the loop? This is important, because if the answer variable doesn't already accept the value no, the loop condition would have been imitation and the code in the loop body would never have run at all.
Bear that in heed. It might be useful in this side by side exercise...
Y'all've got Affiliate one under your belt. Let's await back at what you've learned so far.
Programming Tools
* Programs are created from code statements:
-
commands do things.
-
branches decide things.
-
loops repeat things.
* Conditionals help you decide if
-
something is True or False.
* Assignment sets a name to a value.
* A named value is stored in a "variable".
Python Tools
* if/else branches
* while loops
* = assignment operator
* == equality operator
* != inequality operator
* > greater than operator
* impress() displays a bulletin on screen
* input() gets and returns user input
* int() converts characters to numbers
* randint() produces a random number
Source: https://www.oreilly.com/library/view/head-first-programming/9780596806682/ch01.html
0 Response to "What Is It Caller When a Program Runs Over and Over Again?"
Post a Comment