Specialty Digest

Global English editionLearners’ Study Dashboard — in development
The Knowledge Library for All

Online Courses / University Track / Python from Zero

Module 2 · Wrap-up

Discussion prompt, recap and references

Reflect on one question, review the module and find the named sources.

  • 25 minutes
  • Recap
  • References

Real-world case: The day the music players froze

31 December 2008 · Microsoft Zune 30GB

At about midnight on 31 December 2008, Microsoft Zune 30GB music players froze. The cause was a clock routine written by a supplier. It counted days since 1980 and turned them into a year. 2008 was a leap year with 366 days, so 31 December was day 366.

The loop kept going while the number of days left was more than 365. Inside it, one branch handled leap years and subtracted 366 only if the days were more than 366. On day 366 the days were exactly 366, so no branch changed anything and the loop ran forever. The players were unusable until the next day.

The mistake was a condition that treated more than 366 and not 366 itself. Nobody had tested the value on the boundary.

How the case shows what you learned in this module

Module ideaIn the case
Comparisons give True or Falsedays > 366 is False when days is exactly 366, so the leap-year branch was skipped.
if needs an else for the other caseThe inner if had no branch for the case it did not match, so nothing changed.
Order and conditions decide the pathThe outer test (days > 365) stayed True while the inner test (days > 366) was False.
Test at boundariesA test on day 365, 366 and 367 of a leap year would have found the fault.

Workplace takeaway. Write the condition, write the boundary values, and predict the outcome for each before you run the code.

Sources: Microsoft engineering blog, Hello w0rld0; TechCrunch, Zune bug explained in detail (31 Dec 2008); Wikipedia, Zune 30

Discussion prompt

20 minutes. Think of a decision you make at work or study with at least three cases. In 150 to 200 words, write its conditions in plain language, say which would be the boundary values, and which test values you would use.

Private: nothing you type here is sent or stored.

This course has no discussion board yet, so the prompt is a private reflection.

Recap

  • A comparison gives True or False; == compares and = stores.
  • if runs a block when its condition is True; else catches the rest; elif adds cases.
  • The first True block runs, so order matters; and, or and not combine conditions.
  • Test at and around boundaries, with expected outputs written first.

Flashcards: 12 workplace review cards

Tap a card to reveal the answer. Read the question, answer it aloud or in writing first, then check yourself against the explanation.

Card 1 of 12 · What does a comparison give you?
ValuestwoCompareoperatorAnswerTrue or False
Comparison

A Boolean value, True or False. Comparing two values answers a yes-or-no question. At work, stock < 10 is the question that decides whether to reorder.

Card 2 of 12 · What is the difference between = and ==?
=store==askCheckif line
Store or ask

= stores a value in a variable; == asks whether two values are equal. Writing = in an if line gives a syntax error. Reviewers check this first in any decision code.

Card 3 of 12 · Why does "Ann" == "ann" give False?
Anncapital Aannsmall aResultFalse
Exact text

Python compares text exactly, so capital and small letters differ. When matching user input, convert both sides to the same case first, which is a common step in sign-in forms.

Card 4 of 12 · What must an if line end with, and how do you mark its block?
ifcondition:Indentfour spacesDedentends block
if block

It ends with a colon, and the lines to run are indented by four spaces. The indentation is how Python knows which lines belong to the if.

Card 5 of 12 · When does else run?
ifTrue pathelseFalse pathNextrest
else

When the if condition is False. else has no condition of its own. It is the safe default path in a program, for example refusing a request that failed every check.

Card 6 of 12 · What does elif add?
ifcase 1elifcase 2elserest
elif chain

More cases between if and else. Python runs only the first block whose condition is True. A pricing program with three bands uses if, elif, else.

Card 7 of 12 · Why does the order of elif checks matter?
80check first60thenRestelse
Order

Because the first True wins. If you check score >= 60 before score >= 80, a score of 85 gets the 60 band. Put the narrowest or highest case first.

Card 8 of 12 · How do and, or and not work?
andbothoreithernotreverse
Logic

and needs both sides True; or needs at least one; not reverses. For example, a discount for members who spend over 50 uses and.

Card 9 of 12 · How do you write a condition for a range, such as ages 13 to 19?
Lowage >= 13AndjoinHighage <= 19
Range

Repeat the variable: age >= 13 and age <= 19. Writing age >= 13 and <= 19 is an error. Range checks are common in form validation.

Card 10 of 12 · What is a boundary value?
17below18on19above
Boundary

A value where the answer changes, such as 18 for age >= 18. Bugs hide there because a single wrong symbol, > instead of >=, changes the result for that value only.

Card 11 of 12 · Why write the expected output before running a test?
PredictexpectedRunactualComparematch?
Predict first

If you run first and then write what you saw, you copy the bug. Predicting first checks the program against what it should do, as in any acceptance test at work.

Card 12 of 12 · How would you test a fee program with limits at 5 and 18?
4Free5Child18Adult
Test table

Test 4, 5, 17 and 18 at least. Write the expected result for each first, then run. This is how a team checks a pricing rule before release.

Module notes

Key terms. Boolean: True or False. Condition: a comparison that gives a Boolean. Block: the indented lines under an if. elif: else if. Boundary: a value where the answer changes. Test case: an input with its expected output.

Rules and formulas. a % 2 == 0 is True when a is even; low <= x and x <= high is True when x is from low to high inclusive.

Limits of this module. Examples use Python 3 and four-space indentation. Run each example to confirm the output shown.

Next

Continue to Module 3: Repetition: for and while Loops. You can also return to the course overview or the University Track.

References and further reading