
First Great Western offer "Groupsave" tickets where 3 or 4 adults can travel for the price of two on most off peak, book at time of travel, services. And children can go along at a pound each. But there's no "Groupsave 5", so if there are five of you, you'll need to buy a Groupsave 4 plus an extra ticket ... and if there are six of you ...
As an exercise during yesterday's course, I wrote a "groupsave calculator" to work out, given a regular fare and group sizes, what you might expect to pay for your party. It's a great illustration of how you can code a series of conditions (and conditions within conditions) into a program.
[source].
If you run this program a few times, you'll find some curious anomalies thrown up - that it's cheaper for 3 adults and 2 children that for 2 adults and 2 children, for example (the adults pay the same, 2 or 3, but the children drop from half fare to just a pound each). But then if the adults have a "family railcard", the 2 + 2 may get a better deal there. And is a child allowed to travel on an adult ticket, so buying a 3 + 1? All absurdly complex.
Python is great language for trying out algorithms ... and indeed for then encapsulating those algorithms into more complex programs. The code this article refers to was written on the first day of training for delegates new to programming, so I kept it fairly straightforward. But we could (with suitable data sources) go on to put other factors into the code, such as the fact that you can't use Groupsave to Henley on Thames on Regatta day, or to Reading during the pop festival - but you are allowed on trains that pass through there is you're not changing ...
Me thinks learning Python is easier than learning about train fares!
P.S. - Groupsaves are a well hidden secret, it seems. You have to know to ask for them on many online planners, and ticket vending machines don't sell them (or didn't). Anyone would think that the Train Operating Companies don't really want too many people to buy them! (written 2010-08-02)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y103 - Python - Conditionals and Loops [299] What - no switch or case statement? - (2005-05-03)
[353] Wimbledon Neck - (2005-06-20)
[657] The ternary operator in Python - (2006-03-25)
[668] Python - block insets help with documentation - (2006-04-04)
[788] New - Conditional expressions in Python 2.5 - (2006-07-01)
[835] Python - when to use the in operator - (2006-08-16)
[909] Python is like a narrowboat - (2006-10-30)
[1201] No switch in Python - (2007-05-23)
[1477] Decisions - small ones, or big ones? - (2007-12-18)
[1661] Equality, sameness and identity - Python - (2008-05-31)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[2778] Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24)
[3083] Python - fresh examples from recent courses - (2010-12-11)
[3200] How a for loop works Java, Perl and other languages - (2011-03-12)
[3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
[3439] Python for loops - applying a temporary second name to the same object - (2011-09-14)
[3558] Python or Lua - which should I use / learn? - (2011-12-21)
[3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
[3762] Learning to program - the if statement. Python. - (2012-06-12)
[3895] Flowchart to program - learning to program with Well House - (2012-10-14)
[4092] Identity in Python - (2013-05-17)
[4210] If elif elif elif - multiway selection in Python - (2013-11-16)
[4322] Learning to Program - the conditional statement (if) - (2014-11-21)
[4323] Learning to program - Loop statements such as while - (2014-11-22)
[4402] Finding sum, minimum, maximum and average in Python (and Ruby) - (2015-01-19)
[4541] Setting up and tearing down with the Python with keyword - (2015-10-16)
[4545] Method, Class, Module, Package - how to they relate in Python? - (2015-10-17)
[4713] Equality (in Python) - (2016-10-30)
[4723] Conditional operators in Python - (2016-11-05)
Some other Articles
Bath to Devizes bus via Melksham - changing again in SeptemberCommunity consultation - a true open inputAmbassador or Anorak?Redirecting a page - silent, temporary or permanent?Groupsave tickets - 3 or 4 train tickets for the price of 2Programming Standards from the start!The Land of the Black LabradorLAMP - Linux, Apache, MySQL, PHP - install, configure, administerGlobal Computer Maintenance DepartmentSorting people by their names