0% found this document useful (0 votes)
6 views13 pages

CHAPTER 4 - Programming It All Adds Up

Mr. Shakir assigned a biology project for students to create a Python program called Bird Counter to track bird visits at a feeder. The document discusses common syntax errors, how to fix them, and the transition to a new program, Bird Addition, that allows users to input the number of birds seen. It emphasizes the importance of user-friendly interfaces and readable code in programming.

Uploaded by

Sehrish Humayun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

CHAPTER 4 - Programming It All Adds Up

Mr. Shakir assigned a biology project for students to create a Python program called Bird Counter to track bird visits at a feeder. The document discusses common syntax errors, how to fix them, and the transition to a new program, Bird Addition, that allows users to input the number of birds seen. It emphasizes the importance of user-friendly interfaces and readable code in programming.

Uploaded by

Sehrish Humayun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

4.

4 - A class project
Counting up

 Mr. Shakir, a science teacher, assigned a biology project on bird populations.


 Students must observe and count how many birds visit the school’s bird feeder.
 They are required to create a Python program named Bird Counter.
 The program will allow the user to input 'Y' each time a bird is seen at the feeder.
 Once observation ends, the program will display the total number of bird visits.

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.

 Mr. Shakir agreed with the students’ suggestions.


 He said their ideas would help in creating the program.
 All programmers make errors when writing programs.
 A good programmer can recognize and fix these errors.
 Fixing errors helps programs work as planned.

 Python IDE helps find and fix errors by:


a. Using color and layout to make code easier to read while writing.
b. Showing error messages when running the program.

How the Python IDE Helps You Fix Errors


Python has tools in its IDE (Integrated Development Environment) that help you:

a. Color and Layout


 Python shows your code in different colors.
 Keywords (like if" and print) are purple.
 Strings (text in quotes) are green.
 This helps you see mistakes—like missing quotes or incorrect spelling.

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.

When you run a program, two main things happen:

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.

What if There’s a Mistake?


If your program has a syntax error (a mistake in how the code is written):
 The computer cannot understand the command.
 It cannot translate it into machine code.
 The program will stop and show you an error message.

The error message helps you find and fix the problem

Fixing the problems


Mr Shakir checked the programs his students made. He wrote this note for his students. It lists
the most common syntax errors that he found.

 Using the wrong word (e.g., typing pritn instead of print)


 Not using indentation where required (e.g., after if or while statements)
 Leaving out the : at the end of control statements like if, for, or while
 Using a single = instead of double == in comparisons

2
Using the wrong word

 Abdel, a student of Mr Shakir, confused Scratch and Python.


 In Scratch, a conditional loop starts with repeat until.
 But in Python, a conditional loop starts with while.

What Went Wrong?

 Abdel typed repeat until in his Python program.


 This caused a syntax error because repeat until is not valid in Python.

The Error Message

 Python showed a syntax error and highlighted the line with the problem.
 This helped Abdel identify and understand the mistake.

Fixing the Error

 Abdel replaced repeat until with the correct Python keyword: while.
 After the fix, the program ran correctly.

Not using indent

 In Python, all commands inside a loop


(like while or for) must be indented.
 Python usually adds this indentation
automatically, but stefan made a
mistake.

What Went Wrong?

 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.

Fixing the Error

 Stefan corrected the program by adding the indentation back.


 After that, the program worked properly.

Leaving out colon


In Python, many commands must end with a colon (:). The colon tells Python that a block of
code is coming next. For example:
 if answer == 12:
 for i in range(15):
 while answer > 9:

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 Equals Sign Error— What Went Wrong?

 Milan wrote a Python program to count bird visitors.


 He made a mistake in the logical test by using a single equals sign (=) instead of a
double equals sign (==).

 This caused an error message in his program.

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

Mr. Shakir explained:

 = is used for assignment (giving a value to a variable).


o Example: total = 0

 == is used for a logical test (checking if two values are equal).


o Example: while visitor == "Y":

Fixing the Error

 After the reminder, Milan understood the difference.


 He corrected his code to use == in the condition, and the program worked!

4.5 - Extend the project


Logical Errors
 Syntax errors stop the program from running. The computer shows an error message
because it can’t translate the code.
 In contrast, logical errors occur when the program runs but does the wrong thing.
 Logical errors happen when the logic is incorrect, even though there’s no visible error
message.
 These errors are harder to find because the program appears to work, but it doesn’t
meet the requirements.

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.

Discuss and plan

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.

To find the issues:


o Try entering the code in a Python file.
o Run the program and observe what happens.
o Check:
 Does the program run without errors?
 Does it meet the requirements (like total bird count)?

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 new condition: visits != 0


o This means the loop continues as long as the number of visits is not zero.
o If the user enters 0, the loop will stop.

Issue with Inputting Zero:

 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:

o The loop condition was visits != 0


o When visits is 0, the condition becomes false
o So, the loop ends, even though the user didn’t mean to finish the session.

Change Exit Condition


 Students changed the logical test in the loop to use a number instead of a letter.
 The test now compares the input number to a special value (e.g., 99).
 Reason for using 99:
o It’s a number that would never be a real number of birds at the feeder.
o If the user types 99, the loop will stop.

 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?

Change the order of commands


There are two problems:
1. The first input value, entered
before the loop starts, is not added to the total.
2. The final input value, 99, is incorrectly added to 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.

4.6 - Readable and user


friendly
Make user-friendly programs
User-friendly programs are easy to use. They have simple
inputs with prompts, clear outputs, and helpful messages.
The parts of a program that handle input and output are
called the interface. In Scratch, the interface is colorful and
easy to use with backdrops and sprites. In contrast, Python
uses only plain text, so making the interface user-friendly
takes more effort.
The interface of your program
The current Bird feeder visitor-counting program is not user-friendly. It doesn’t clearly tell the
user what to input or what the output means. The next step is to improve the program by
making instructions clearer and labeling the input and output properly.

Simple inputs and prompts


1. Keep Input Simple
o Use short, easy answers (e.g., ask for Y instead of YES).
o This reduces typing and the chance of mistakes.

2. Add a Prompt
o Use a clear prompt in the input command to tell users exactly what to enter.

3. Include a Space at the End


o Add a space at the end of your prompt string so the user’s input appears nicely.

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

 To make the program user-friendly, use print() with a message.


 Adding text helps the user understand what the number means.

Other on-screen messages


 Use the print() command to
show helpful messages to the
user.
 You can add a title to your program.
 Use print statements to explain what the program does.
 This makes the interface more user-friendly — easier for people to understand and use.
 Students used print() to improve their Bird Addition program.

Other useful print

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.

How to Make Code Readable


 Use well-chosen variable names (e.g., bird_count instead of x).
 Add comments to explain what your code does.

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

You might also like