Applying discounts

In this example, I want to show you a technique I like a lot. In many programming languages, other than the if/elif/else constructs, in whatever form or syntax they may come, you can find another statement, usually called switch/case, that in Python is missing. It is the equivalent of a cascade of if/elif/.../elif/else clauses, with a syntax similar to this (warning! JavaScript code!):

/* switch.js */switch (day_number) {    case 1:    case 2:    case 3:    case 4:    case 5:        day = "Weekday";        break;    case 6:        day = "Saturday";        break;    case 0:        day = "Sunday";        break;    default:        day = "";
        alert(day_number + ' is not a valid day number.')}

In the preceding code, we switch on a variable called day_number. This means we get its value and then we ...

Get Learn Web Development with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.