0% found this document useful (0 votes)
4 views20 pages

Week 3 Input Function & Errors Debugging

This document outlines a lesson plan focused on user input and error debugging in programming. It covers the input() function, integrating print statements with variables, and identifying common error types such as syntax, name, type, and logic errors. The session concludes with a challenge for participants to write and debug their own code.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views20 pages

Week 3 Input Function & Errors Debugging

This document outlines a lesson plan focused on user input and error debugging in programming. It covers the input() function, integrating print statements with variables, and identifying common error types such as syntax, name, type, and logic errors. The session concludes with a challenge for participants to write and debug their own code.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Week 3

User Input & Error Debugging


Mastering print() • variables • input() + Finding & Fixing Errors

Practical Computer Science • Biology & Geology Tracks


What We'll Cover Today (40 min)

Part 1: User Input (5 min)


Learn the input() function - make programs interactive!

Part 2: Integration (5 min)


Combine print(), variables, and input() together

Part 3: Errors & Debugging (25 min)


Syntax • Name • Type • Logic errors + hands-on fixing

Part 4: Challenge (5 min)


Write and debug your own code!
PART 1
The input() Function
Making programs interactive

5 minutes
How input() Works

The Code:
name = input("Enter your name: ")
print("Hello", name)

What happens:
1. Program asks: "Enter your name: "
2. You type: Ahmed
3. Stored in variable: name = "Ahmed"
4. Output: Hello Ahmed

→ User types input → Program stores it in a variable!


PART 2
Putting It All Together
print() + variables + input()

5 minutes
Biology Lab Data Collection
Complete Code:
# Collect sample data
sample = input("Enter sample name: ")
temp = input("Enter temperature (°C): ")
count = input("Enter bacteria count: ")

print("=== Lab Report ===")


print("Sample:", sample)
print("Temperature:", temp, "°C")
print("Bacteria:", count)

Try this now on Pydroid 3! Type different values each time.


PART 3
Understanding Errors
Finding bugs & fixing them like a pro

25 minutes
Errors = Bugs in Your Code
Every programmer makes mistakes! The key is learning to find and fix them.

4 Common Error Types:


• Syntax Errors - code won't run
• Name Errors - undefined variables
• Type Errors - mixing wrong data types
• Logic Errors - code runs but wrong result

→ Debugging = Bug Hunting!


Syntax Error: Missing Quotes
Broken Code:
bacteria = MRSA
print(bacteria)

Pydroid 3 Error:
SyntaxError: invalid syntax
bacteria = MRSA
^

Fixed Code:
bacteria = "MRSA"
print(bacteria)

Why it works:
Text needs quotes! Python thought [Link] was a variable name.
Syntax Error: Missing Parenthesis
Broken Code:
temp = 37.5
print("Temperature:", temp

Pydroid 3 Error:
File "<string>", line 2
print("Temperature:", temp
^
SyntaxError: '(' was never closed

Fixed Code:
temp = 37.5
print("Temperature:", temp)

Why it works:
Every opening ( needs a closing )
Your Turn: Fix These (3 min)

Find and fix the syntax errors:

sample = input("Enter sample name: )


print(sample)

depth = 150.5
print "Depth:", depth)

Hint: Look for missing quotes and parentheses!


Name Error: Undefined Variable
Broken Code:
bacteria_count = 150
print(bacteria_Count)

Pydroid 3 Error:
File "<string>", line 2
NameError: name 'bacteria_Count' is not defined. Did you mean: 'bacteria_count’?

Fixed Code:
bacteria_count = 150
print(bacteria_count)

Why it works:
Python is case-sensitive! count ≠ Count
Name Error: Typo in Variable Name
Broken Code:
sample_name = "Sample A"
print(sampl_name)

Pydroid 3 Error:
File "<string>", line 2
NameError: name 'sampl_name' is not defined. Did you mean: 'sample_name'?

Fixed Code:
sample_name = "Sample A"
print(sample_name)

Why it works:
Variable name must match exactly! Check spelling carefully.
Your Turn: Fix These (3 min)

Find and fix the name errors:

Temperature = 36.5
print("Temp:", temperature)

mineral_type = "Quartz"
print(minral_type)

Hint: Check capitalization and spelling!


Type Error: Mixing Wrong Types
Broken Code:
count = input("Enter count: ")
total = count + 10
print(total)

Pydroid 3 Error:
File "<string>", line 2
TypeError: can only concatenate str (not "int") to str

Fixed Code:
count = int(input("Enter count: "))
total = count + 10
print(total)

Why it works:
input() returns text! Use int() to convert to number.
Logic Error: Wrong Formula
Wrong Logic:
# Calculate average of 3 samples
total = 10 + 15 + 20
average = total / 2
print("Average:", average)

Output: Average: 22.5 Wrong!

Correct Logic:
# Calculate average of 3 samples
total = 10 + 15 + 20
average = total / 3
print("Average:", average)
Output: Average: 15.0 Correct!

Logic errors don't crash - they give wrong answers! Think carefully!
Quick Error Reference
Syntax Error
Example: Missing quotes, parentheses
Output: Code won't run at all

Name Error
Example: Undefined variable, typos
Output: Variable doesn't exist

Type Error
Example: Mixing text & numbers
Output: Use int() to convert

Logic Error
Example: Wrong formula/math
Output: Runs but wrong result
PART 4
Your Challenge!
Write buggy code & fix it

5 minutes
Final Challenge (5 min)

Task:

• Write a program that asks for 2 numbers


• Add them together and print the result
• Intentionally add 2 different errors
• Exchange with a partner - debug each other's code!

Use: input(), variables, print(), and math (+)


Example errors: missing quotes, typo in variable name, forget int()
What We Learned Today
New Skills
• Using input() function
• Combining all 3 concepts (print, variables, and inputs)
• Reading error messages

4 Error Types
• Syntax - missing symbols
• Name - undefined variables
• Type - mixing text/numbers
• Logic - wrong formula

Remember: Every bug you fix makes you a better programmer!

You might also like