|
| 1 | +''' |
| 2 | +
|
| 3 | + Introduction To Computing Using Python (Werkboek) |
| 4 | +
|
| 5 | + Final Assignment: NS-kaartautomaat |
| 6 | + In deze opdracht maak je een NS-kaartautomaat. Gegeven is het treintraject |
| 7 | + Schagen - Maastricht: |
| 8 | +
|
| 9 | + Schagen, Heerhugowaard, Alkmaar, Castricum, Zaandam, Amsterdam Sloterdijk, |
| 10 | + Amsterdam Centraal, Amsterdam Amstel, Utrecht Centraal, 's-Hertogenbosch, |
| 11 | + Eindhoven, Weert, Roermond, Sittard, Maastricht. |
| 12 | +
|
| 13 | + De bedoeling is dat je 3 functies schrijft waarmee je 1) het beginstation aan |
| 14 | + een reiziger vraagt, 2) het eindstation aan een reiziger vraagt, en 3) de |
| 15 | + reisgegevens op het scherm print. Jouw functies zouden als volgt gebruikt |
| 16 | + moeten kunnen worden: |
| 17 | +
|
| 18 | + stations = ['Schagen', 'Heerhugowaard', ..., 'Sittard', 'Maastricht'] |
| 19 | + beginstation = inlezen_beginstation(stations) |
| 20 | + eindstation = inlezen_eindstation(stations, beginstation) |
| 21 | + omroepen_reis(stations, beginstation, eindstation) |
| 22 | +
|
| 23 | + Een mogelijke uitvoer van deze code (als de functies zijn geschreve), staat |
| 24 | + onderaan de volgende pagina. Op de volgende pagina staat ook een stappenplan |
| 25 | + waarmee je dit programma kunt realiseren. |
| 26 | +
|
| 27 | +''' |
| 28 | + |
| 29 | +def inlezen_beginstation(stations): |
| 30 | + while(True): |
| 31 | + station = input('Wat is je beginstation? : ') |
| 32 | + if station in stations: |
| 33 | + return station |
| 34 | + return |
| 35 | + |
| 36 | +def inlezen_eindstation(stations, beginstation): |
| 37 | + while(True): |
| 38 | + station = input('Wat is je eindstation? : ') |
| 39 | + if station in stations: |
| 40 | + if(stations.index(station) > stations.index(beginstation)): |
| 41 | + return station |
| 42 | + print('Deze trein komt niet in {}'.format(station)) |
| 43 | + return |
| 44 | + |
| 45 | +def omroepen_reis(stations, beginstation, eindstation): |
| 46 | + beginstationIndex = stations.index(beginstation) |
| 47 | + print('\nHet beginstation {} is het {}e station in het ' |
| 48 | + 'traject.'.format(beginstation, beginstationIndex + 1)) |
| 49 | + eindstationIndex = stations.index(eindstation) |
| 50 | + print('Het eindstation {} is het {}e station in het ' |
| 51 | + 'traject.'.format(eindstation, eindstationIndex + 1)) |
| 52 | + verschil = abs(beginstationIndex - eindstationIndex) |
| 53 | + print('De afstand bedraagt {} station(s).'.format(verschil)) |
| 54 | + prijs = verschil * 5 |
| 55 | + print('De prijs van het kaartje is {} euro\n'.format(prijs)) |
| 56 | + print('Jij stapt in de trein in: {}'.format(beginstation)) |
| 57 | + for index, station in enumerate(stations): |
| 58 | + if index > beginstationIndex and index < eindstationIndex: |
| 59 | + print(' - {}'.format(station)) |
| 60 | + print('Jij stapt uit in {}'.format(eindstation)) |
| 61 | + |
| 62 | +stations = ['Schagen', 'Heerhugowaard', 'Alkmaar', 'Castricum', 'Zaandam', |
| 63 | + 'Amsterdam Sloterdijk', 'Amsterdam Centraal', 'Amsterdam Amstel', |
| 64 | + 'Utrecht Centraal', '\'-Hertogenbosch', 'Eindhoven', 'Weert', |
| 65 | + 'Roermond', 'Sittard', 'Maastricht'] |
| 66 | +beginstation = inlezen_beginstation(stations) |
| 67 | +eindstation = inlezen_eindstation(stations, beginstation) |
| 68 | +omroepen_reis(stations, beginstation, eindstation) |
0 commit comments