HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →
Live examsInternational live exams are comingTell us which exam you want first. Timed practice papers are open now.See what is coming →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Space Science and Astronomy
  • Earth and Environmental Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
  1. Python Programming
  2. /Python Going Further
  3. /Testing and Debugging

Testing and Debugging

Python Programming · High School (11–12) · 1 course, 10 worked problems, 20 practice questions

Programs rarely work the first time, so programmers test and debug them. Testing means running code with chosen inputs and checking the output against what you expect. A simple test uses assert, which stops the program if a condition is false. Good tests include normal values, edge cases and wrong input. Debugging means finding the cause of a fault. You read the traceback, print values at key points, or step through the code line by line. Logic errors are the hardest to find, because the program runs but gives the wrong answer.

With practice you will be able to

  • Write tests for a function with assert
  • Choose normal, edge and wrong inputs as test cases
  • Find a logic error by printing values or stepping through code

On the syllabus of

  • CBSE / State Class 12
  • Python Certification
  • A-Level
  • IGCSE

Lessons

  • Testing and DebuggingWhere do most bugs hide? How do you find a bug without guessing?High School (11–12)

Practice questions

Choose an answer, then check it. Each one shows a full explanation.

add(a, b) has a bug: it returns a - b. Which test would let the bug slip through?

Why does assertEqual(0.1 + 0.2, 0.3) fail, while assertAlmostEqual(0.1 + 0.2, 0.3) passes?

A bug hides somewhere in 64 lines. About how many checks does bisection need to find its line?

Worked problems

Each one has a hint, the key idea and a full worked solution.

  • A function is_even(n) should say whether a whole number is even…Easy
  • A function ticket_price(age) gives 0 for ages under 5, 50 for ages 5 to 12, 100 for ages 13 to 59…Easy
  • A student writes these names for test methods in a class that tests a Stack class…Easy
  • A test run prints this. How many tests passed? …Medium
  • A function longest_word(text) gives the longest word of a text, and when two words are equally…Easy
  • Fix second_largest so that it gives the second largest different value, and None when there is none…

Python labs

  • Fill the table
  • Catch the bugs
  • Test a function with a table
  • Find a failing input
  • Fix the running total
  • Fix the second largest

Related topics

  • Python Going Further
  • Classes and Objects
  • Inheritance and Special Methods
  • Files and Data Formats
  • Comprehensions, Iterators and Generators
  • Data Structures

What does len("".split(" ")) give?

assert 2 + 2 == 4, "maths" is run. What is shown?

assert len([]) > 0, "empty" is run. What happens?

A function double(n) returns n * 3. The test self.assertEqual(double(2), 4) is run. What does the report say about the values?

In a test run, the line of letters is .F.. What does it mean?

Which assert method compares 0.1 + 0.2 with 0.3 correctly?

def last(items): return items[-1]. What does last([]) do?

All 20 practice questions on Testing and Debugging →

Medium
  • Write three properties that must be true for any list x and the function reversed_list(x) that…Medium
  • A pipeline has three stages. For the input "Hello, World", it should give ["hello", "world"], but…Medium
  • This function should give the average of a list, but gives a wrong answer for [2, 4, 6] (it gives…Easy
  • Think of a bug that you met while working through this book, or write one on purpose…Easy