Bugs that hide
Read · 1 of 3
Think about special values
Some bugs only appear on special inputs: perfect squares, years divisible by 100, numbers at exactly the limit. Random tests can miss them for a long time.
Ask: which values does this code treat as special? Then test exactly those.
Read this lesson as text
Code that's right for almost every input.
Think about special values
Some bugs only appear on special inputs: perfect squares, years divisible by 100, numbers at exactly the limit. Random tests can miss them for a long time.
Ask: which values does this code treat as special? Then test exactly those.
Break it: Break the prime check
This should print YES if n is prime and NO if not. It's right for most numbers. Find one where it's wrong.
n = int(input())
prime = n >= 2
for d in range(2, int(n ** 0.5)):
if n % d == 0:
prime = False
print("YES" if prime else "NO")Input rules: one whole number from 1 to 1,000,000.
Break it: Break the leap year
This should print Leap or Not leap. A year is a leap year if it divides by 4, except years divisible by 100, unless they're also divisible by 400.
y = int(input())
print("Leap" if y % 4 == 0 and y % 100 != 0 else "Not leap")Input rules: one year from 1 to 9999.