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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.