Skip to content

Commit e9a0ade

Browse files
authored
Add all exercises of lesson 10.
1 parent 9fa5aa3 commit e9a0ade

File tree

6 files changed

+367
-0
lines changed

6 files changed

+367
-0
lines changed

Lesson-10/artikelen.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<artikelen>
3+
<artikel nummer="121">
4+
<code>ABC123</code>
5+
<naam>Highlight pen</naam>
6+
<voorraad>231</voorraad>
7+
<prijs>0.56</prijs>
8+
</artikel>
9+
<artikel nummer="123">
10+
<code>PQR678</code>
11+
<naam>Nietmachine</naam>
12+
<voorraad>587</voorraad>
13+
<prijs>9.99</prijs>
14+
</artikel>
15+
<artikel nummer="128">
16+
<code>ZYX163</code>
17+
<naam>Bureaulamp</naam>
18+
<voorraad>34</voorraad>
19+
<prijs>19.95</prijs>
20+
</artikel>
21+
<artikel nummer="128">
22+
<code>MLK709</code>
23+
<naam>Monitorstandaard</naam>
24+
<voorraad>66</voorraad>
25+
<prijs>32.50</prijs>
26+
</artikel>
27+
<artikel nummer="128">
28+
<code>TRS665</code>
29+
<naam>Ipad hoes</naam>
30+
<voorraad>155</voorraad>
31+
<prijs>19.00</prijs>
32+
</artikel>
33+
</artikelen>

Lesson-10/fa/pe10_fa.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'''
2+
3+
Introduction To Computing Using Python (Werkboek)
4+
5+
Final Assignment: XML-stationslijsten
6+
In deze opdracht lees je gegevens uit een XML-bestand. Dit bestand is op de
7+
volgende bladzijde gegeven. Het bestand kun je overnemen voor je eigen
8+
programma: en bevat de gegevens van vier stations. Van ieder station is
9+
staat vermeld:
10+
11+
- Code
12+
- Type
13+
- Namen (Kort, Middel & Lang)
14+
- Land
15+
- Synoniemen (niet altijd aanwezig)
16+
17+
Lees de gegevens uit het bestand en plaats deze in een dict! Laat je programma
18+
nu achtereenvolgens de onderstaande gegevens afdrukken op het scherm:
19+
20+
1. Van alle stations de code en het type.
21+
2. Van alle stations de code en synoniemen (maar alleen als er
22+
synoniemen zijn).
23+
3. Van alle stations de code en de lange naam.
24+
25+
Let op: bij stap 2 mag je de dict met alle synoniemen uitprinten, maar dat kan
26+
natuurlijk netter. Bij stap 3 is het de bedoeling dat je wel netjes de lange
27+
naam van een station uit de namen-dict haalt!
28+
29+
Mogelijke uitvoer:
30+
Dit zijn de codes en types van de 4 stations:
31+
HT - knooppuntIntercitystation
32+
ALMO - stoptreinstation
33+
ATN - stoptreinstation
34+
ASA - intercitystation
35+
36+
Dit zijn alle stations met één of meer synoniemen:
37+
HT - OrderedDict([('Synoniem', ["Hertogenbosch ('s), 'Den Bosch'])])
38+
39+
Dit is de lange naam van elk station:
40+
HT - 's-Hertogenbosch
41+
ALMO - Almere Oostvaarders
42+
ATN - Aalten
43+
ASA - Amsterdam Amstel
44+
45+
'''
46+
import xmltodict
47+
48+
with open('stations.xml') as file:
49+
document = xmltodict.parse(file.read())
50+
# Codes en types van 4 stations.
51+
print('Dit zijn de codes en types van de 4 stations:')
52+
for station in document['Stations']['Station']:
53+
print('{0:4} - {1}'.format(station['Code'], station['Type']))
54+
55+
# Stations met synoniemen.
56+
print('\nDit zijn alle stations met één of meer synoniemen:')
57+
for station in document['Stations']['Station']:
58+
if(station['Synoniemen']):
59+
synoniemen = station['Synoniemen']['Synoniem']
60+
print('{0:4} - {1}'.format(station['Code'], ', '.join(synoniemen)))
61+
62+
# Lange naam van elk station.
63+
print('\nDit is de lange naam van elk station:')
64+
for station in document['Stations']['Station']:
65+
print('{0:4} - {1}'.format(station['Code'], station['Namen']['Lang']))

Lesson-10/fa/stations.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Stations>
3+
<Station>
4+
<Code>HT</Code>
5+
<Type>knooppuntIntercitystation</Type>
6+
<Namen>
7+
<Kort>Den Bosch</Kort>
8+
<Middel>'s-Hertogenbosch</Middel>
9+
<Lang>'s-Hertogenbosch</Lang>
10+
</Namen>
11+
<Land>NL</Land>
12+
<Synoniemen>
13+
<Synoniem>Hertogenbosch ('s)</Synoniem>
14+
<Synoniem>Den Bosch</Synoniem>
15+
</Synoniemen>
16+
</Station>
17+
<Station>
18+
<Code>ALMO</Code>
19+
<Type>stoptreinstation</Type>
20+
<Namen>
21+
<Kort>Oostvaard</Kort>
22+
<Middel>Oostvaarders</Middel>
23+
<Lang>Almere Oostvaarders</Lang>
24+
</Namen>
25+
<Land>NL</Land>
26+
<Synoniemen></Synoniemen>
27+
</Station>
28+
<Station>
29+
<Code>ATN</Code>
30+
<Type>stoptreinstation</Type>
31+
<Namen>
32+
<Kort>Aalten</Kort>
33+
<Middel>Aalten</Middel>
34+
<Lang>Aalten</Lang>
35+
</Namen>
36+
<Land>NL</Land>
37+
<Synoniemen></Synoniemen>
38+
</Station>
39+
<Station>
40+
<Code>ASA</Code>
41+
<Type>intercitystation</Type>
42+
<Namen>
43+
<Kort>Amstel</Kort>
44+
<Middel>Amsterdam Amstel</Middel>
45+
<Lang>Amsterdam Amstel</Lang>
46+
</Namen>
47+
<Land>NL</Land>
48+
<Synoniemen></Synoniemen>
49+
</Station>
50+
</Stations>

Lesson-10/pe10_1.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'''
2+
3+
Introduction To Computing Using Python (Werkboek)
4+
5+
Control Structures & Dictionaries (Perkovic – §5.4 t/m 6.1)
6+
7+
Practice Exercise 10.1 (XML-bestanden schrijven)
8+
In Practice Exercise 9_4 heb je een CSV bestand aangemaakt van deze producten:
9+
Maak nu met een teksteditor en op basis van deze tabel, een XML-bestand dat
10+
er zo uitziet:
11+
12+
Uitvoer:
13+
<?xml version="1.0" encoding="UTF-8"?>
14+
<artikelen>
15+
<artikel nummer="121">
16+
<code>ABC123</code>
17+
<naam>Highlight pen></naam>
18+
<voorraad>231</voorraad>
19+
<prijs>0.56</prijs>
20+
</artikel>
21+
...
22+
</artikelen>
23+
24+
Installeer nu ook de module 'xmltodict'. File > Settings > Zoek op
25+
'interpreter' en klik op 'Project Interpreter'. Klik rechts in het scherm op
26+
het plusje. Zoek nu op 'xmltodict' en installeer het package.
27+
28+
Schrijf een programma waarmee je het XML-bestand inleest en alle artikel-
29+
namen onder elkaar print!
30+
31+
'''
32+
import xmltodict
33+
34+
with open('artikelen.xml') as file:
35+
document = xmltodict.parse(file.read())
36+
for artikel in document['artikelen']['artikel']:
37+
print(artikel['naam'])

Lesson-10/pe10_2.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
3+
Introduction To Computing Using Python (Werkboek)
4+
5+
Control Structures & Dictionaries (Perkovic – §5.4 t/m 6.1)
6+
7+
Practice Exercise 10.1 (Namespaces)
8+
In onderstaande programma's wordt verkeerd gebruik gemaakt van namespaces.
9+
Verbeter de code!
10+
11+
'''
12+
import time
13+
14+
# b = 7
15+
# def verdubbelB():
16+
# b = b + b
17+
# verdubbelB()
18+
# print(b)
19+
20+
b = 7
21+
22+
def verdubbelB(b):
23+
return b + b
24+
25+
b = verdubbelB(b)
26+
print(b)
27+
28+
# print(time.strftime(("%H:%M:%S)))
29+
print(time.strftime("%H:%M:%S"))
30+
31+
# def f(y):
32+
# return 2*y + 1
33+
# print(f(3)+g(3))
34+
# def g(x)
35+
# return 5 + x + 10
36+
37+
def f(y):
38+
return 2 * y + 1
39+
40+
def g(x):
41+
return 5 + x + 10
42+
43+
print(f(3) + g(3))

Lesson-10/py10_3.py

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
'''
2+
3+
Introduction To Computing Using Python (Werkboek)
4+
5+
Control Structures & Dictionaries (Perkovic – §5.4 t/m 6.1)
6+
7+
Practice Exercise 10.3 (Locals & Globals)
8+
In de onderstaande programma's worden locals en globals gebruikt. Ga na wat de
9+
uitvoer is van deze programma's. Het is natuurlijk erg makkelijk om ze gewoon
10+
uit te voeren, maar probeer voordat je dat doet te beredeneren wat de uitvoer
11+
is en controleer dan pas jouw antwoord!
12+
13+
'''
14+
15+
# Uitvoer 8_3A:
16+
a = 3
17+
18+
def f(y):
19+
global a
20+
a = 9
21+
return 2 * y + a
22+
23+
print(a) # B) 3
24+
25+
# Uitvoer 8_3B:
26+
x = 1
27+
y = 4
28+
29+
def fun():
30+
x = 2
31+
global y
32+
y = 3
33+
print(y, end = ' ')
34+
35+
fun()
36+
print(y, end = '\n') # D) 3 3
37+
38+
# Uitvoer 8_3C:
39+
x = 2
40+
y = 5
41+
42+
def fun():
43+
y = 3
44+
global x
45+
x = 1
46+
print(x * y, end = ' ')
47+
return x * y
48+
49+
x = fun()
50+
print(x * y, end = '\n') # A) 3 15
51+
52+
# Uitvoer 8_3D:
53+
a = 3
54+
55+
def fun1():
56+
global a
57+
print('a:', a, end = ' ')
58+
b = 7
59+
a = 0
60+
return b
61+
62+
def fun2(y):
63+
a = y + fun1()
64+
b = 7
65+
a += 1
66+
return a
67+
68+
a = 9
69+
fun2(5)
70+
print('a:', a) # B) a: 9 a: 0
71+
72+
# Uitvoer 8_3E:
73+
x = 1
74+
y = 4
75+
76+
def doe1():
77+
global x
78+
y = 7
79+
x = 0
80+
return y
81+
82+
def doe2():
83+
x = doe1()
84+
x += 1
85+
return x
86+
87+
x = doe1()
88+
print(x) # C) 7
89+
90+
91+
# Uitvoer 8_3F:
92+
a = 5
93+
def fun1():
94+
global a
95+
b = 2
96+
a = 4
97+
return a + b
98+
99+
def fun2(y):
100+
global a
101+
a = y + fun1()
102+
a += 1
103+
return a
104+
105+
print('a:', a, end = ' ')
106+
a = fun2(3)
107+
print('a:', a) # D) a: 5 a: 10
108+
109+
# Uitvoer 8_3G:
110+
x = 1
111+
y = 3
112+
113+
def doe1():
114+
global x
115+
y = 4
116+
x = 0
117+
return x + y
118+
119+
def doe2():
120+
x = doe1()
121+
x += 2
122+
return x
123+
124+
doe2()
125+
print(x)
126+
127+
# Uitvoer 8_3H:
128+
def doe1():
129+
y = 7
130+
x = 0
131+
return y
132+
133+
def doe2():
134+
global x
135+
x = doe1()
136+
x += 1
137+
138+
doe2()
139+
print(x) # D) 8

0 commit comments

Comments
 (0)