CHAPTER 4 - Programming It All Adds Up
CHAPTER 4 - Programming It All Adds Up
4 - A class project
Counting up
Class discussion
The class discussed what to include in the number counter program.
Student ideas included:
o A variable is needed to store the number of birds.
o The variable should start at 0.
o The program must include a loop.
o The loop should be a conditional loop since the number of birds is unknown.
o A logical test is required to control when the loop stops.
1
b. Error Messages
When you run your program, Python will show an error message if something is wrong.
These messages tell you:
o What kind of error it is.
o Where in the program the error happened.
o An idea of what went wrong.
Syntax errors
Every programming language has rules. The rules of a language are called syntax. If you break
the rules for programming language, you make a syntax error.
1. Translating
The computer translates your commands (source code) into machine code. Machine
code is the only language the computer understands.
2. Running
The computer then executes (runs) the machine code commands step by step.
The error message helps you find and fix the problem
2
Using the wrong word
Python showed a syntax error and highlighted the line with the problem.
This helped Abdel identify and understand the mistake.
Abdel replaced repeat until with the correct Python keyword: while.
After the fix, the program ran correctly.
Stefan accidentally removed the indentation from the lines inside his loop.
This caused an error message: "expected an indented block"
3
What the Error Message Means
The message told Stefan that Python was expecting indented code after the loop.
That clue helped Stefan realize the mistake.
If you forget to write the colon, Python will not understand your command and will show a
syntax error.
4
Using single equal sign
Milan’s Confusion
Milan was confused because he had used the equals sign = in other parts of the
program, and those worked fine.
He didn’t understand why it was wrong in the while statement.
5
Mr. Shakir’s Reminder
A new purpose
Mr. Shakir’s students created a bird counter program where users press Y each time a bird
visits, increasing the count by 1. They shared this program with students at North Mountain
School, located in a colder country. However, at North Mountain School, birds visit the feeder
in groups, especially during winter. The original program was hard to use because it only
counted one bird at a time. So, the students at North Mountain School decided to create a new
program that could handle multiple birds arriving at once.
6
The students discussed how they wanted the new program to work and wrote down the
requirements. They decided to name it the Bird Addition Program.
Program requirement
Each student will watch the bird feeder for one minute.
They will count how many birds were fed during that time.
Each student will enter the number of birds as a value in the program.
After all students have entered their numbers, the program will output the total number
of birds.
Input a number
In the old bird counter program, the user entered “Y” for each bird visit.
In the new Bird Addition Program, the user enters a number instead.
The variable name was changed from visitor to visits.
The visits variable stores the number of bird visits entered by the user.
The program includes clear reminders to tell the user to enter a number.
The user input must be converted to an integer so it can be used in calculations.
A new version of the Bird Addition Program has been created with changes.
The user now enters a number instead of just typing “Y”.
The variable is renamed to visits to store the number of bird visits.
The program still has several problems and does not work yet.
Exit condition
The program uses a while loop to keep
running.
Original condition: visit == "Y" (checks if the input is the letter Y).
This does not work in the new program because visits now stores a number, not a letter.
7
As a result, the logical test is never true, so the commands inside the loop never run.
To fix this, the students changed the exit condition.
The students ran the BIRD addition program, and the loop worked as expected.
But they faced a problem during actual use.
Sometimes, no bird visited the bird feeder during a minute.
The user had to enter 0 to record no visits.
However, entering 0 caused the program to stop, because:
This helps avoid stopping the program when 0 birds are seen (which could be a real
input).
Problem remains:
o The Bird Addition program still doesn’t work correctly.
o Why?
The program may still be adding 99 to the total before stopping.
The order of commands might be incorrect, causing the exit value to be
included in the total.
8
Add Up the Visitors
The program’s goal is to add up
the total number of bird visitors.
Problem:
o Currently, the program
adds just 1 to the total each time, no matter what number the user enters.
Improvement Attempt:
o Students modified the Bird Addition (A-D-D-I-T-I-O-N) program to add the user’s
input number (e.g. 3 birds) to the total.
New Issue:
o Despite the change, the program still doesn’t work correctly.
Possible Error:
o The wrong value might be getting added.
o Maybe the program is still adding 1 instead of the input number.
Suggested Action:
o Run the code in Python.
o Check what goes wrong:
Is the right number being added?
Is the exit value accidentally being included in the total?
To fix this, the students reversed the order of commands inside the loop: first, add the input
number to the total, then prompt for a new number. When the user enters the exit value 99,
the program stops immediately.
9
With this change, the program works correctly. The students have found and fixed all the logical
errors.
2. Add a Prompt
o Use a clear prompt in the input command to tell users exactly what to enter.
These small changes make your program more user-friendly and help prevent input errors.
10
Clear outputs
Most programs end
with a result (like a total or count).
The result might be a number, such as:
o Total birds seen
o Number of words typed
11
Make readable programs
Readable programs are easier to understand and maintain.
Teamwork improves when code is clear, since many programmers may work on the
same project.
Clear code helps you remember what you did when you return later.
It’s easier to improve and update your code when it’s readable.
Readable code helps everyone — not just the original programmer.
Variable names
A good variable name tells what value it stores.
It makes your program easier to read and understand.
Other programmers can quickly understand your work by looking at your variable
names.
Clear variable names improve teamwork and communication in coding.
Comments
Comments are messages added to your program to explain what the code does.
In Python, a comment starts with a hash symbol (#).
Anything after the # is ignored by the computer but readable to people.
Comments make programs more readable for you and other programmers.
They are helpful for explaining your thinking, logic, or purpose of the code.
12
13