├── .github └── FUNDING.yml ├── Cook the Books.md ├── Cook_the_Books_de.md ├── Divine a Date.md ├── Divine_a_Date_de.md ├── README.md ├── The Letter Logjam.md ├── The Morse Code Mystery.md ├── The_Letter_Logjam_de.md ├── The_Morse_Code_Mystery_de.md └── donors.txt /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: edent # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: edent # Replace with a single Patreon username 5 | open_collective: edent # Replace with a single Open Collective username 6 | ko_fi: edent # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | custom: https://www.AMAZON.co.uk/gp/registry/ref=as_li_ss_tl?id=13GFCFR2B2IX4&tag=edentgithub-21 # Replace with a single custom sponsorship URL 10 | -------------------------------------------------------------------------------- /Cook the Books.md: -------------------------------------------------------------------------------- 1 | # Cook The Books 2 | 3 | ## Clearing Out 4 | 5 | The mood in the school assembly was grim. Mrs Hopper, the Head Teacher, had a face like stone. 6 | 7 | "I'm sorry children," she said, "but we simply can't afford to keep the school library open any more." 8 | 9 | Poppy's heart sank. She'd spent so long in the school's library. It was the perfect place to hang out after school, or during soggy lunchtimes, or just when she just needed a break from the world. Some of her best friends were characters from books... 10 | 11 | Just then, she noticed Penny's hand spring up. Penny was her _real_ best friend. Always encouraging, not put off by the situation with Poppy's dad, and **always** ready to ask an awkward question. 12 | 13 | Mrs Hopper couldn't ignore Penny's frantic hand waving, "Yes, Penny, you have a question?" 14 | 15 | Penny gathered up her breath and let all of her frustration out in one long breath, "But all the kids use the library and there are really great books in there and it's just a STUPID idea!" 16 | 17 | Penny went bright red as she realised what she had just said. 18 | 19 | "Detention!" said Mrs Hopper, "And see me after assembly." 20 | 21 | ## Hopping Mad 22 | 23 | Poppy loitered outside Mrs Hopper's office. She casually leaned in towards the doorway and strained to hear what Mrs Hopper was saying to Penny. 24 | 25 | "Penny, I simply will ***not*** be spoken to like that." The Head Teacher sounded really angry, "I don't get to decide how much money to Government gives us. I have to make the best use of the resources we have. It was a choice between closing down the library or closing down the computer lab." 26 | 27 | She paused. The Head knew how much Penny loved computing lessons - how could she possibly choose between them? It was a real "Sophie's Choice". 28 | 29 | "So," the Head continued, "We need to start selling off all the books in the library. Hopefully we'll raise enough money to upgrade some of the computers. For your detention, I want *you* to make a list of all the books we have so we can start to sell them." 30 | 31 | Poppy scuttled away from the door as Penny came bustling out - her cheeks red with shame and her eyes brimming with tears. 32 | 33 | "Oh Pen!" 34 | 35 | "Well," sniffed Penny, "I guess we'd better get cracking." 36 | 37 | "We?" said Poppy in mock horror. 38 | 39 | "Oh, I'm sorry, don't you want to help your best friend try and save the library?" 40 | 41 | Penny strode off down the corridor in the direction of the library, her shoes clacking on the cheap wooden flooring. After a second's hesitation, Poppy ran after her. 42 | 43 | ## Library 44 | 45 | The ancient library computer wheezed into life. Covered in layers of book dust, old library cards, and what looked like some abandoned knitting - the computer was older than both Penny and Poppy put together. It took so long to boot, it was no wonder that students rarely bothered to use it to check books in and out. 46 | 47 | "Do you think we can get a list of all the books from here, Poppy?" said Penny suspiciously. "Only, that thing looks like it belongs in a museum." 48 | 49 | With suspiciously poor timing, the hard drive in the machine made a prolonged and ugly crunching sound. The screen glowed with an ominous message: "File allocation table bad drive C." 50 | 51 | Poppy glared at the monitor, the error message steadfastly refused to budge. "Right!" she proclaimed, "Time for Plan B." 52 | 53 | She rummaged around in her school bag. There, nestled under her pencil case and an old packet of crisps was her Raspberry Pi. 54 | 55 | Penny scrabbled around the library and found a keyboard and helped her friend set it up. 56 | 57 | "Python to the rescue?" 58 | 59 | "Yes, and I think I know just how to do it. Are you happy to type?" Poppy asked. 60 | 61 | "Sure." 62 | 63 | "Ok, this is what we want to do..." Poppy paused while she gathered her thoughts, "Every book has a unique number printed on the back of it, right?" 64 | 65 | "That's right," said Penny, "The ISBN. It's a code which gets given to every book when it's published. But it's just a number - it doesn't tell us anything about the book itself." 66 | 67 | "I know, but we can look the ISBN up on the Internet and see what it tells us." 68 | 69 | "You mean use Google?" Asked Penny. 70 | 71 | "Well, sort of. If we typed the ISBN into a search engine, it would give us lots of information but it would be quite hard for the Raspberry Pi to understand. Luckily, Google has a free JSON API." 72 | 73 | "You're just making things up now," said Penny defensively. "Who is Jason and what's his ape eye?" 74 | 75 | Poppy stifled her giggles, "An API is an Application Programming Interface. It's a way for computers to talk to each other. One computer sends a specially formatted code to another computer, that second computer replies with another code the computer can read." 76 | 77 | Penny's eyes lit up, "I see! So the code is simple enough for a computer to understand. But, wait, I still don't understand who Jason is...?" 78 | 79 | A smile spread across Poppy's face. "Let me show you. First, we have to tell Python that we want to work with URLs - web addresses." 80 | 81 |
 82 | import urllib2
 83 | 
84 | 85 | "That just means we'll be using the URL Library version 2 to help us. Now, we need to know how Google's books API works. Some APIs are really complicated. Luckily, this one is as easy as 3.14." 86 | 87 | Poppy made sure the coast was clear and then plucked out her emergency phone. Strictly speaking she wasn't meant to have it in school - but it was too cheap for anyone to bother stealing and was awfully handy when she needed to look something up. She searched for information on Google's books API. 88 | 89 | "Right, that looks pretty simple," she said, "Type this in, please." 90 | 91 |
 92 | https://www.googleapis.com/books/v1/volumes?q=isbn:
 93 | 
94 | 95 | "Basically, you just add the ISBN after the colon and it should give you back all the data Google knows about a book. Let's try it!" 96 | 97 | At random, Poppy picked up a slim volume from the English section and carefully read out its ISBN "014018385X" 98 | 99 | Penny's fingers deftly flew over the cheap beige keyboard. "Ok, so we want to ask Google for the information and then display it on screen." 100 | 101 |
102 | import urllib2
103 | print urllib2.urlopen('https://www.googleapis.com/books/v1/volumes?q=isbn:014018385X').read()
104 | 
105 | 106 | #### What happens when you run the code? 107 | 108 | "Whoa!" Exclaimed Penny, "*That's* JSON?" 109 | 110 | "Pretty much," said Poppy after glancing at the text which had filled the screen. "Want me to decipher it for you?" 111 | 112 | Penny nodded mutely; dumbfounded by the data her Pi had gobbled up from the Internet. 113 | 114 | Poppy continued, "So JSON is just a way of structuring data so it's easy for computers to understand and *fairly* easy for humans to read. It's just a way of describing a thing, or group of things. Look, here's you represented in JSON..." 115 | 116 | She shoved her friend to one side and typed away. She wasn't a touch typist like Penny, so had to spend some time hunting for the "U" key which always seemed to vanish right when she needed it. 117 | 118 |
119 | {
120 | 	"Person": {
121 | 		"name": "Penny",
122 | 		"eyes": {
123 | 			"number": 2,
124 | 			"colour": "green"
125 | 		}
126 | 	}
127 | }
128 | 
129 | 130 | "There you go, do you see how it works?" Poppy asked. 131 | 132 | Penny read carefully over the code. "I think so. Every thing has a value. So 'name' is equal to 'Penny'. But, some values can hold lots of things. So my 'eyes' have multiple properties. Hmmm, let me see..." 133 | 134 | This time it was Penny's turn to push her friend aside and type. 135 | 136 |
137 | {
138 | 	"Person": {
139 | 		"name": "Penny",
140 | 		"eyes": {
141 | 			"number": 2,
142 | 			"colour": "green"
143 | 		},
144 | 		"favourite_food": "Mango",
145 | 		"hair": {
146 | 			"length": "short",
147 | 			"colour": "red"
148 | 		}
149 | 	}
150 | }
151 | 
152 | 153 | "How's that?" 154 | 155 | Poppy gave it a brief look and said "I think you've got it. There only one other, teeny, tiny, minor complication." 156 | 157 | Penny sighed dramatically. There was always some spanner in the works. 158 | 159 | Poppy took over typing duties again, "Right, let's say you have multiple people, here's how you'd represent it in JSON." 160 | 161 |
162 | {
163 | 	"People": [
164 | 		{
165 | 			"Person": {
166 | 				"name": "Penny",
167 | 				"eyes": {
168 | 					"number": 2,
169 | 					"colour": "green"
170 | 				}
171 | 			}
172 | 		},
173 | 		{
174 | 			"Person": {
175 | 				"name": "Poppy",
176 | 				"eyes": {
177 | 					"number": 2,
178 | 					"colour": "brown"
179 | 				}
180 | 			}
181 | 		}
182 | 	]
183 | }
184 | 
185 | 186 | After Poppy had finished scrabbling around on the keyboard, she said "Right. So. The square brackets `[` and `]` mean that there is a group of similar things between them. In this case, there are going to be multiple people. We could put millions of people in there. Or just one. It depends on what we want to do." 187 | 188 | Penny thought for a moment, "OK, that doesn't seem so bad. What's the catch?" 189 | 190 | "Heh," Poppy laughed feebly. "Well, if you want to access the first item, you have to use the number '0'. If you want the second, it's the number '1' and so on." 191 | 192 | Penny yawned and stretched her arms up the the ceiling. "Fine, whatever. Let's get on with this." 193 | 194 | Poppy grinned her Cheshire Cat smile. "So, we have our JSON back from Google's API. Now we just get the precise information we want out of it. Let's start by telling Python that we want to use JSON." 195 | 196 |
197 | import urllib2
198 | import json
199 | 
200 | 201 | "Now we need to eat that JSON up and store it as a variable." 202 | 203 |
204 | book_data = json.load(urllib2.urlopen('https://www.googleapis.com/books/v1/volumes?q=isbn:014018385X'))
205 | 
206 | 207 | "Nice," said Poppy, "Let's print `book_data` to the screen so we can make sure it's right." 208 | 209 | #### Try to print book_data to the screen 210 | 211 | Penny stared at the screen, rubbed her eyes, and stared again. "Well, it's something. But how do we get the title out of this mess?" 212 | 213 | "Oh, that's easy," said Poppy with a self-confident air. "Now we've got the JSON data we can pretty easily get anything we want out of it. So, the JSON has a load of `items`, we want the first one, then we want the `volumeInfo` which contains the `title`." 214 | 215 | "Clear as mud!" 216 | 217 | "It's like this:" 218 | 219 |
220 | print book_data['items'][0]['volumeInfo']['title']
221 | 
222 | 223 | And with that, the title of the book appeared on the screen. The girls cheered and Poppy tickled the Raspberry Pi's ears and offered to give it a biscuit. 224 | 225 | "Ok, one down," said Penny. "Now we need to get the author and the thumbnail image of the front cover." 226 | 227 | #### Can you get your program to display the Author's name and the URL for the image? 228 | 229 | Penny's plan was simple. They would get the ISBN of every book in the library, use the Python code they had written to find out the author, title, and image. Then they would create a web page on their school's website to sell the books. 230 | 231 | "This library is **ancient**," Penny explained, "There must be hundreds of old books in here that someone wants to buy." 232 | 233 | #### Can you change the program so it asks you to input an ISBN, then prints the results? 234 | 235 | It had been hours since they started. They swapped between typing and fetching books from the shelves. Some of the books just didn't appear to be in Google's mighty brain, so they put them to one side for now. Poppy's back was beginning to ache, and Penny's fingers were suffering from typing for so long. 236 | 237 | They weren't even halfway done. Shelves and shelves of books seemed to extend into the far distance. 238 | 239 | "One more, then let's call it quits for the evening." Yawned Poppy. 240 | 241 | Penny picked up the next book. She couldn't see an ISBN on the back, so she flipped it open. There, in spidery ink above the ISBN was written "With best wishes" followed by a scrawled signature. 242 | 243 | "Pops... I think I know how we're going to save the library! Type this ISBN in, would you? 0747532699." 244 | 245 | #### What's special about that book? 246 | 247 | ## Making The Papers 248 | 249 | Being in the news was _fun_! When word spread about their incredibly rare find, the media descended on the school like a plague of locusts. 250 | 251 | The local MP had turned up to the school and was busy trying to charm the reporters. "I'm so incredibly proud," he oozed, "of these two remarkable young ladies. With the sale of this book, it looks like the school library will be well stocked with books for many years to come!" His smile had far too many teeth for Poppy's liking. 252 | 253 | A reporter shoved a microphone in Poppy's face. "How come," the reporter asked, "you found that book then?" 254 | 255 | "Oh," said Poppy sweetly, "When we heard that the Government wanted to shut down our library, we just knew we had to do something to save such a precious resource." 256 | 257 | A silence fell on the room. A single flash from a camera illuminated the MP's face. His grin had become rictus. 258 | 259 | Penny span round and in her politest voice said, "Why *did* you want to close our library, sir?" 260 | 261 | All of a sudden the assembled hordes of press began screaming "Minister! Minister! Do you have any comment..." 262 | 263 | The MP briskly pushed his way through the reporters and out to the waiting car. 264 | 265 | Mrs Hopper strode over to the girls. 266 | 267 | "Uh oh," whispered Penny. 268 | 269 | "Girls," she began, "If I *ever* catch you talking in that way to a member of the Government again..." She paused for dramatic effect, "I will be *very* proud of you." 270 | 271 | And, with that she gave a curious little smile and wandered off into the bowels of the school. -------------------------------------------------------------------------------- /Cook_the_Books_de.md: -------------------------------------------------------------------------------- 1 | # Die frisierten Bücher 2 | 3 | ## Die Räumungsdrohung 4 | 5 | Die Stimmung in der Schulversammlung war düster. Frau Hopper, die Direktorin machte ein steinernes Gesicht. 6 | 7 | "Es tut mir sehr leid, Kinder," sagte sie, "aber wir können es uns die Schulbibliothek nicht mehr leisten." 8 | 9 | Poppy rutschte das Herz in die Hose. Sie hatte so viele Stunden in der Schulbibliothek verbracht. Es war der perfekte Ort, um nach der Schule rumzuhängen oder an regnerischen Mittagen oder auch einfach, wenn sie eine Pause von der Welt brauchte. Einige ihrer besten Freunde waren Charaktere aus Büchern... 10 | 11 | Gerade da bemerkte sie, dass Penny die Hand hob. Penny war ihre echte beste Freundin. Immer Mut machend, nicht abgeschreckt von der Situation mit Poppys Vater und immer bereit, unbequeme Fragen zu stellen. 12 | 13 | Frau Hopper konnte Pennys wildes Handwedeln nicht mehr ignorieren: "Ja, Penny, hast du eine Frage?" 14 | 15 | Penny holte tief Luft und ließ dann ihrem Frust freien Lauf: "Aber alle Kinder benützen die Bücherei und es gibt großartige Bücher da drin und das ist eine völlig blödsinnige Idee!" 16 | 17 | "Nachsitzen!" sagte Frau Hopper. "Du kommst nach der Versammlung zu mir." 18 | 19 | ## 20 | 21 | Poppy lungerte vor Frau Hoppers Büro herum. Sie lehnte sich an den Türrahmen und strengte ihre Ohren an, um zu hören, was Frau Hopper zu Penny sagte. 22 | 23 | "Penny, so redet man nicht mit mir." Die Direktorin klang richtig wütend. "Ich bin es nicht, die entscheidet, wieviel Geld uns die Regierung gibt. Ich muss das beste machen aus dem, was wir haben. Es war die Entscheidung zwischen Schließung der Bibliothek oder Auflösung des Computerraums. 24 | 25 | Sie hielt inne. Die Direktorin wußte, wie sehr Penny die Computerstunden liebte - wie konnte sie zwischen den beiden wählen? Es war die Wahl zwischen Pest und Cholera. 26 | 27 | "Und jetzt", fuhr die Direktorin fort, "müssen wir anfangen, die Bücher in der Bibliothek zu verkaufen. Hoffentlich verdienen wir damit genug, um einige der Computer aufzurüsten. Als Aufgabe für dein Nachsitzen wirst du eine Liste aller Bücher anfertigen, so dass wir anfangen können, sie zu verkaufen." 28 | 29 | Poppy fuhr von den Tür zurück als Penny herausstürmte - ihre Wangen rot vor Scham und ihre Augen voll Tränen. 30 | 31 | "Oh Pen!" 32 | 33 | "Nun," schniefte Penny, "dann machen wir uns besser an die Arbeit." 34 | 35 | "Wir?" fragte Poppy mit gespielter Entrüstung. 36 | 37 | "Oh, Entschuldigung, ich dachte, du wolltest deiner besten Freundin helfen, die Bibliothek zu retten?" 38 | 39 | Penny steuerte den Flur in Richtung Bibliothek entlang, ihre Schuhe klackerten auf dem billigen Holzfußboden. Poppy zögerte kurz und rannte ihr hinterher. 40 | 41 | ## In der Bibliothek 42 | 43 | Der archaische Bibliothekscomputer bootete pfeifend. Bedeckt von Staubschichten, alten Karteikarten und von etwas, das wie eine aufgegebene Strickarbeit aussah - der Computer war älter als Penny und Poppy zusammen. So lange brauchte er zum Booten, dass es kein Wunder war, dass kaum ein Schüler je darin Ausleihen vermerkte. 44 | 45 | "Du meinst, wir kriegen damit eine Liste aller Bücher hier?" fragte Penny misstrauisch. "Dieses Ding sieht aus als gehörte es ins Museum." 46 | 47 | Mit verdächtig akkuratem Timing erzeugte die Festplatte in der Maschine ein langes, kratzendes Geräusch. Auf dem Screen leuchtete die unheilvolle Botschaft: "File allocation table bad drive C." 48 | 49 | Poppy starrte auf den Monitor, wo die Fehlermeldung sich standfest weigerte zu verschwinden. "Okay," verkündete sie."Zeit für Blan B." 50 | 51 | 52 | Sie wühlte in ihrem Schulrucksack. Dort, zwischen ihrem Federmäppchen und einer alten Chipstüte eingezwängt, war ihr Raspberry Pi. 53 | 54 | Penny suchte in der Bibliothek herum und fand eine Tastatur. Sie half ihrer Freundin beim Anschließen. 55 | 56 | "Python, unser Helfer in der Not?" 57 | 58 | "Ja, und ich habe schon einen Plan, wie wir es anstellen. Tippst du?" fragte Poppy. 59 | 60 | "Gerne." 61 | 62 | "Also, was wir machen wollen..." Poppy hielt inne, um ihre Gedanken zu sammeln. "Jedes Buch hat eine eindeutige Nummer auf dem Buchrücken, korrekt?" 63 | 64 | "Genau," sagte Penny, "die ISBN. Jedes Buch bekommt eine, wenn es veröffentlicht wird. Aber das ist nur eine Nummer, das sagt uns nichts über das Buch selbst." 65 | 66 | "Ich weiß, aber wir können die ISBN über Internet abrufen und schauen, was uns das verrät." 67 | 68 | "Du meinst über Google?" fragte Penny. 69 | 70 | "So ähnlich. Wenn wir die ISBN in eine Suchmaschine eingäben, bekämen wir eine Menge Informationen, aber die wären schlecht für den Raspberry Pi zu verstehen. Aber glücklicherweise hat Google eine freie JSON API." 71 | 72 | "Jetzt denkst du dir Sachen aus," wehrte Penny ab. "Wer ist Jason und was soll ein API sein?" 73 | 74 | Poppy verbiß sich das Lachen und erklärte: "API ist die Abkürzung für Application Programming Interface, zu deutsch eine Programmierschnittstelle. Es ist eine Möglichkeit von Computern, miteinander zu reden. Ein Computer schickt einen auf bestimmte Art formatierten Code zu einem anderen Computer und dieser zweite antwortet darauf mit einem Code, den der erste lesen kann." 75 | 76 | Pennys Augen leuchteten auf. "Verstehe! Der Code ist also einfach genug für einen Computer zu kapieren. Aber ich begreife immer noch nicht, wer Jason ist...?" 77 | 78 | Poppy lächelte. "Ich werd es dir zeigen. Als erstes müssen wir Python sagen, dass wir mit URLs - Internet-Adressen - arbeiten wollen." 79 | 80 |
 81 | import urllib2
 82 | 
83 | 84 | "Das bedeutet einfach nur, dass wir die URL Library Version 2 verwenden. Jetzt müssen wir herausfinden, wie Googles Bücher-API funktioniert. Manche APIs sind ziemlich kompliziert. Aber diese ist glücklicherweise kinderleicht." 85 | 86 | Poppy stellte sicher, dass sie allein waren, dann kramte sie ihr Notfalltelefon aus der Tasche. Streng genommen waren Telefone in der Schule nicht erlaubt - aber es war so billig, dass es niemand freiwillig stehlen würde und sehr praktisch, wenn sie etwas nachschlagen wollte. Sie suchte nach Infos zu Googles Bücher-API. 87 | 88 | "Das sieht wirklich ziemlich einfach aus, "sagte sie. "Tipp mal ein, bitte." 89 | 90 |
 91 | https://www.googleapis.com/books/v1/volumes?q=isbn:
 92 | 
93 | 94 | "Du musst nur die ISBN nach dem Doppelpunkt ergänzen und es sollte alles zurückgeben, was Google über ein Buch weiß. Probieren wir's aus!" 95 | 96 | Aufs Geradewohl schnappte sich Poppy einen dünnen Band aus dem Englisch-Regal und las sorgfältig die ISBN vor: "014018385X" 97 | 98 | Pennys Finger tanzten über die Tastatur. "Okay, wir wollen Google nach der Info fragen und sie dann auf dem Bildschirm anzeigen." 99 | 100 |
101 | import urllib2
102 | print urllib2.urlopen('https://www.googleapis.com/books/v1/volumes?q=isbn:014018385X').read()
103 | 
104 | 105 | #### Was passiert, wenn du den Code ausführst? 106 | 107 | "Uiii!" rief Penny, "Das ist JSON?" 108 | 109 | "Im Prinzip ja. " sagte Poppy nach einem Blick auf den Text, mit dem sich der Bildschirm gefüllt hatte. "Soll ich es für dich entziffern?" 110 | 111 | Penny nickte stumm, total benommen von den Daten, die ihr Pi aus dem Internet gezogen hatte. 112 | 113 | Poppy fuhr fort, "JSON ist eine Methode, Daten so zu strukturieren, dass sie für Computer einfach zu verstehen sind und für Menschen _einigermaßen_ einfach zu lesen. Ein Weg, um ein Ding, oder eine Gruppe von Dingen zu beschreiben. Schau, das bist du dargestellt in JSON..." 114 | 115 | Sie schob ihre Freundin zur Seite und find an zu tippen. Sie konnte nicht blind tippen wie Penny und brauchte einige Zeit, um das "U" zu finden, das nie da lag, wo sie es gerade brauchte. 116 | 117 |
118 | {
119 | 	"Person": {
120 | 		"Name": "Penny",
121 | 		"Augen": {
122 | 			"Anzahl": 2,
123 | 			"Farbe": "gruen"
124 | 		}
125 | 	}
126 | }
127 | 
128 | 129 | "Bitte sehr, begreifst du, wie es geht?" fragte Poppy. 130 | 131 | Penny las sorgfältig den Code. "Ich denke ja. Jedes Ding hat einen Wert. So 'Name' ist 'Penny'. Aber einige Werte können auch mehr Sachen enthalten. So haben meine 'Augen' gleich mehrere Eigenschaften. Lass mich mal..." 132 | 133 | Jetzt war Penny an der Reihe, ihre Freundin zur Seite zu schieben und zu tippen. 134 | 135 |
136 | {
137 | 	"Person": {
138 | 		"Name": "Penny",
139 | 		"Augen": {
140 | 			"Anzahl": 2,
141 | 			"Farbe": "gruen"
142 | 		},
143 | 		"Lieblingsessen": "Mango",
144 | 		"Haare": {
145 | 			"Laenge": "kurz",
146 | 			"Farbe": "rot"
147 | 		}
148 | 	}
149 | }
150 | 
151 | 152 | "So ungefähr?" 153 | 154 | Poppy schaute über den Code und sagte: "Ich denke du hast es kapiert. Es gibt nur noch eine winzig kleine Komplikation." 155 | 156 | Penny seufzte. Irgendetwas machte ihnen immer einen Strich durch die Rechnung. 157 | 158 | Poppy übernahm wieder das Tippen. "Und so stellst du es in JSON dar, wenn du mehrere Leute hast." 159 | 160 |
161 | {
162 | 	"Leute": [
163 | 		{
164 | 			"Person": {
165 | 				"Name": "Penny",
166 | 				"Augen": {
167 | 					"Anzahl": 2,
168 | 					"Farbe": "gruen"
169 | 				}
170 | 			}
171 | 		},
172 | 		{
173 | 			"Person": {
174 | 				"Name": "Poppy",
175 | 				"Augen": {
176 | 					"Anzahl": 2,
177 | 					"Farbe": "braun"
178 | 				}
179 | 			}
180 | 		}
181 | 	]
182 | }
183 | 
184 | 185 | Als Poppy fertig war mit rumsuchen auf der Tastatur, sagte sie: "So. Richtig. Die eckigen Klammern '[' und ']' bedeuten, dass eine Gruppe ähnlicher Dinge dazwischen steckt. Wir könnten da Millionen Leute reinschreiben. Oder nur einen. Das hängt ganz davon ab, was wir tun wollen." 186 | 187 | Penny dachte einen Augenblick nach. "Das klingt nicht so schlecht. Wo ist der Haken?" 188 | 189 | "Heh", Poppy lachte leise. "Wenn wir den ersten Gegenstand aufrufen wollen, müssen wir die Nummer '0' verwenden. Beim zweiten ist es die '1' und so weiter." 190 | 191 | Penny gähnte und reckte ihre Arme. "Egal. Machen wir weiter." 192 | 193 | Poppy grinste ihr Chsehsire-Katzen-Grinsen. "Also, wir haben unser JSON zurück von der Google-API. Jetzt müssen wir nur noch genau die Informationen bekommen, die wir wollen. Fangen wir damit an, Python zu sagen, dass wir JSON verwenden wollen." 194 | 195 |
196 | import urllib2
197 | import json
198 | 
199 | 200 | "Jetzt schnappen wir uns diese JSON und speichern es in einer Variablen." 201 | 202 |
203 | book_data = json.load(urllib2.urlopen('https://www.googleapis.com/books/v1/volumes?q=isbn:014018385X'))
204 | 
205 | 206 | "Schick," sagte Poppy. "Lassen wir uns 'book_data' mal am Bildschirm anzeigen, ob alles stimmt." 207 | 208 | #### Versuche, book_data auf dem Bildschirm auszugeben 209 | 210 | Penny starrte auf den Monitor, rieb sich die Augen und starrte wieder. "Schon mal etwas. Aber wie bekommen wir aus diesem Salat den Titel heraus?" 211 | 212 | "Das ist einfach," sagte Poppy selbstbewußt. "Jetzt, wo wir die JSON-Daten haben, können wir davon leicht alles, was wir wollen, anzeigen. Dieses JSON hat einen Stapel 'items', wir wollen das erste und dann wollen wir 'volumeInfo', das enthält 'title'." 213 | 214 | "Klar wie Kloßbrühe!" 215 | 216 | "Das geht so:" 217 | 218 |
219 | print book_data['items'][0]['volumeInfo']['title']
220 | 
221 | 222 | Und mit diesen Zeilen erschien der Titel des Buchs auf dem Bildschirm. Die Mädchen jubelten und Poppy kitzelte Raspberry Pis Ohren und bot ihm einen Keks an. 223 | 224 | "Nummer Eins erledigt, "sagte Penny. "Jetzt brauchen wir den Autor und ein Thumbnail-Bild des Covers." 225 | 226 | #### Kannst du dem Programm beibringen, den Autor und die URL für das Bild anzuzeigen? 227 | 228 | Pennys Plan war einfach. Sie würden die ISBN von jedem Buch in der Bibliothek nehmen und mit dem Python-Programm, das sie geschrieben hatten, Autor, Titel und Bild heraussuchen. Dann würden sie eine Seite auf der Schul-Website anlegen, um die Bücher zu verkaufen. 229 | 230 | "Diese Bibliothek ist uralt." erklärte Penny. "Es muss hier hunderte alter Bücher geben, die jemand kaufen will." 231 | 232 | #### Kannst du das Programm so ändern, dass es nach einer ISBN fragt und das Ergebnis ausgibt? 233 | 234 | Es war Stunden her, dass sie angefangen hatten. Sie wechselten sich ab mit Tippen und Büchern aus den Regalen holen. Einige der Bücher erschienen nicht in Googles mächtigem Gedächtnis, diese legten sie fürs erste zur Seite. 235 | 236 | Poppys Rücken fing an zu schmerzen und Pennys Finger taten weh vom vielen Tippen. 237 | 238 | Sie waren nicht einmal halbfertig. Regale hinter Regalen schienen sich bis in weite Ferne zu erstrecken. 239 | 240 | "Ein letztes, dann machen wir Schluß für heute," gähnte Poppy. 241 | 242 | Penny nahm das nächste Buch. Sie sah keine ISBN auf dem Buchrücken, deshalb schlug sie es auf. Dort stand in verblaßter Tinte über der ISBN "With best wishes", gefolgt von einer krakeligen Unterschrift. 243 | 244 | "Pops... Ich glaube, ich weiß, wie wir die Bibliothek retten. Gib mal diese ISBN ein, bitte: 0747532699." 245 | 246 | #### Was ist so besonders an diesem Buch? 247 | 248 | ## In der Zeitung 249 | 250 | In den Nachrichten zu sein war lustig! Als sich ihr irrsinnig seltener Fund herumsprach, fielen die Medien über die Schule her wie eine Heuschreckenplage. 251 | 252 | Der örtliche Parlamentsabgeordnete tauchte auf und versuchte, sich bei den Reporter einzuschleimen. "Ich bin so unfaßbar stolz," verkündete er salbungsvoll, "auf diese zwei bemerkenswerten jungen Damen. Mit dem Verkauf dieses Buches wird die Schulbibliothek für viele Jahre sehr gut ausgestattet sein" Sein Lächeln hatte zuviele Zähne für Poppys Geschmack. 253 | 254 | Ein Reporter hielt sein Mikrophon Poppy unter die Nase. "Wie kam es, dass ihr das Buch gefunden habt?" fragte er. 255 | 256 | "Oh", sagte Poppy zuckersüß. "Als wir hörten, dass die Regierung unsre Schulbibliothek schließen will, mussten wir einfach etwas machen, um sie zu retten." 257 | 258 | Alles wurde still im Saal. Ein einziger Blitz aus einer Kamera erleuchtete das Gesicht des Abgeordneten. Sein Lächeln war erstarrt. 259 | 260 | Penny drehte sich um und fragte in ihrem allerhöflichstem Ton: "Warum wollten sie unsere Bibliothek schließen?" 261 | 262 | Plötzlich begann die versammelte Pressehorde zu schreien: "Minister, Minister, haben sie einen Antwort..." 263 | 264 | Der Abgeordnete schaufelte sich seinen Weg zwischen den Reportern frei und hinaus zu seinem wartenden Auto. 265 | 266 | Frau Hopper ging zu den Mädchen hinüber. 267 | 268 | "Uh, oh" flüsterte Penny. 269 | 270 | "Mädchen", begann sie. "Wenn ich euch nochmal erwische, wie ihr in diesem Ton mit einem Regierungsmitglied redet..." Sie machte eine kleine Pause, "werde sich sehr stolz auf euch sein." 271 | 272 | Und damit, ein seltsames kleines Lächeln auf den Lippen, ging sie ins Innere der Schule zurück. 273 | -------------------------------------------------------------------------------- /Divine a Date.md: -------------------------------------------------------------------------------- 1 | # The Python Pals Divine A Date 2 | 3 | Penny was in a right grouch. Her voice wobbled as she unloaded her woes on to her best friend, Poppy. 4 | 5 | "It's all so _unfair_!" She started to sob. 6 | 7 | "Oh sweetie," said Poppy, "I hate to say this, but you're sounding like a right stereotypical teenager!" 8 | 9 | Penny cracked half a smile and blew her nose. "I know," she said "But it's true. Why _can't_ I date him?" 10 | 11 | Poppy sighed. Penny's mum Ada had banned her from seeing this boy - and it was causing no end of bother. 12 | 13 | "Well, she explained, "they think he's too old for you. He is a *bit* grown-up." 14 | 15 | Penny let out a little growl of rage "But I'm more mature than most grown-ups; half of them can't even _switch on_ a computer - let alone program it as well as I can!" 16 | 17 | This was true. What Penny lacked in years, she more than made up for in talent. No one at her school could even come close to her when it came to Python. Even the teachers turned to her for help when they were stuck. 18 | 19 | "You don't really want to go out with him, do you?" asked Poppy, "I mean, he's got all those zits." 20 | 21 | It was Penny's turn to sigh, "No. No not really. And he does have a really annoying laugh. But I want the _right_ to go out with him. That's what's unfair." 22 | 23 | "Hang on..." Poppy was thinking. What she lacked in coding skills, she made up for in tenacity and lateral thinking. "That **may** be unfair..." 24 | 25 | Penny looked up from her disintegrating tissue. "I _know_," she pleaded. 26 | 27 | "No, I mean literally unfair," Poppy paused, knowing she was getting in to dangerous territory, "What's the exact age difference between your parents?" 28 | 29 | Penny's eyes began to twinkle - not with tears, but the hint of a mischievous idea... "There's one way to find out," she grinned, "Let's get the Raspberry Pi!" 30 | 31 | ## The Difference Engine 32 | 33 | "Ok," said Penny, wiping her nose on her sleeve, "What comes first?" 34 | 35 | They always worked best like this - Penny sat cross-legged in front of the computer plugging away on the code, while Poppy strode around the room pontificating on the problems. 36 | 37 | "First," said Poppy "Does Python have a way to handle dates and times?" 38 | 39 | "Oh yes," and Penny began typing. 40 | 41 |
 42 | from datetime import date
 43 | 
44 | 45 | "Basically, all you need to do is tell Python right at the start what special functions you want to be able to use. In this case, there are some built-in functions called 'datetime' and we just want those ones which deal with dates." 46 | 47 | "Can we test it?" said Poppy cautiously. She always wanted to make sure that everything worked every step of the way. 48 | 49 | "Sure!" 50 | 51 |
 52 | print date.today()
 53 | 
54 | 55 | "That should show us today's date," Penny ran the program and was rewarded with 56 | 57 |
2014-03-24
58 | 59 | "Well," said Poppy "Why is it written in such a funny order?" 60 | 61 | Penny loved explaining to her friend, she relished the look on her face when she finally understood something. 62 | 63 | "So, the Americans write the date with the month first - like 12/11 means December the Eleventh. But to Europeans, it means Twelfth of November. In order not to confuse anyone, most computers represent the data as 'YEAR-MONTH-DAY'." 64 | 65 | "Oh, that makes a funny kind of sense. So, that's today's date - how do we create a specific date?" 66 | 67 | "It's pretty easy," Penny's hands danced over the keyboard, creating a pleasant clattering sound. 68 | 69 |
 70 | my_birthday = date(2000, 02, 29)
 71 | 
72 | 73 | "We create a variable and assign a date object to it. That's all." 74 | 75 | "Alright, so what's zit-boy's birthday." 76 | 77 | "As if I know!" said Penny in a scandalised tone of voice. 78 | 79 | Poppy fixed her with her steely eyes and internally counted to ten. 80 | 81 | Penny cracked by the time Poppy got to 7 and, in a small voice said "January 15th, 1998." 82 | 83 | "Eugh! He was born _last century_!" 84 | 85 | "Shut up." She added the variable. 86 | 87 |
 88 | his_birthday = 
 89 | 
90 | 91 | #### Add in the boy's birthday. 92 | 93 | "So, your birthday, his birthday. How do we tell how much older he is?" 94 | 95 | Penny thought for a second. 96 | 97 | "Python is usually really good at doing maths on things. What happens if we..." She trailed off, lost in code. 98 | 99 | 100 | #### How do you think you would get the difference between two dates? 101 | 102 | The keyboard clattered away under Penny's relentless hammering. 103 | 104 |
105 | difference = my_birthday - his_birthday
106 | print "The difference is " + str(difference.days)
107 | 
108 | 109 | "The difference will be a date object - so we can ask it how many days it has. We also need to tell Python to cast it into a string, so that it will print out properly." 110 | 111 | She ran the program. 112 | 113 | #### What happens when you run the code on your computer? 114 | 115 | Poppy shrieked "He's **ancient**!" 116 | 117 | It was Penny's turn to glare, "It just looks bad because it's written in days. Not in years and months." 118 | 119 | Poppy leant back, and twiddled her hair between her thumbs. "Right, so there are 365 days in a year..." 120 | 121 | "Don't forget leap years," said Penny, who was touchy about the subject. 122 | 123 | "Ok, 365 and a quarter." 124 | 125 | "Well, that's easy enough to calculate. I'll take the number of days and divide it by 365.25." 126 | 127 |
128 | print difference.days / 365.25
129 | 
130 | 131 | Penny had to admit, that didn't make much of a difference. It _still_ looked bad. 132 | 133 | Poppy grinned, "Ok, so let's see what it says about your parents." 134 | 135 | Penny rummaged around in her brain for a bit, "My mum was born on October 17th, 1975 - and dad was born December 24th 1972." 136 | 137 | With a quick whisk round the keyboard she entered in the dates. 138 | 139 | Penny gasped, "My parents are _such_ hypocrites!" Both girls giggled. 140 | 141 | Penny gave the Raspberry Pi a little pat on where she thought its head would be, "That's why we use computers - to make tricky things easy!" 142 | 143 | She turned to her friend, "Let's do your parents next!" 144 | 145 | The atmosphere in the room suddenly changed dramatically. Poppy's memories of her father were still raw and painful. 146 | 147 | "Pops," said Penny gently. 148 | 149 | "No, it's ok," said her friend, with tears in her eyes. "But I think that's enough coding for today." 150 | 151 | ## A Midnight Nightmare 152 | 153 | It was close to midnight when Poppy snuck out of bed. Quickly she flipped her phone on to quiet mode. She strained her ears to see if her mother had heard the alarm. Nothing. 154 | 155 | Poppy silently slid out of bed. She slowly tip-toed over to her Raspberry Pi and switched it on. 156 | 157 | She just _couldn't_ let her mother hear her up at this hour! 158 | 159 | Gently, she tapped her keyboard and subtly altered the Python code that Penny had written earlier. She checked and double-checked her code. There was **no way** she could afford to get this wrong. 160 | 161 | Glancing around furtively, she inputted the today's date - 24th of March, 2014. 162 | 163 | She shut her eyes tight, took a deep breath, and pushed down on the enter key. 164 | 165 | For a full minute, she didn't dare open her eyes. She wanted to _so_ badly, but knew that opening them would unleash more emotions than she knew how to deal with. 166 | 167 | She screwed her courage to the sticking-place, and her eyes flicked open. There, glowing gently on the screen were the words which made everything real - and made her die a little inside. 168 | 169 |
Your father has been missing for: 101 days.
170 | 171 | #### Can **you** work out when Poppy's father disappeared? 172 | -------------------------------------------------------------------------------- /Divine_a_Date_de.md: -------------------------------------------------------------------------------- 1 | # Die Python-Freundinnen erraten ein Datum 2 | 3 | Penny war übelster Laune. Ihre Stimme zitterte, als sie ihrer besten Freundin Poppy ihr Herz ausschüttete. 4 | 5 | "Es ist so unfair!" Sie begann zu schluchzen. 6 | 7 | "Oh Süße", sagte Poppy, "ich mag es ja nicht sagen, aber du klingst wie der stereotype Teenager." 8 | 9 | Penny lächelte schief und putzte sich die Nase. "Ich weiß", sagte sie. "Aber es ist war. Wieso kann ich nicht mit ihm gehen?" 10 | 11 | Poppy seufzte. Pennys Mutter Ada hatte ihr verboten, diesen Jungen zu treffen - und das machte nichts als Ärger. 12 | 13 | "Naja" erklärte sie "sie denkt, er ist zu alt für dich. Er ist schon fast erwachsen." 14 | 15 | Penny rief wütend aus "Aber ich bin reifer als die meisten Erwachsenen. Die Hälfte von ihnen kann nicht mal einen Computer anschalten, geschweige denn ihn so gut programmieren wie ich!" 16 | 17 | Das stimmte. Was Penny an Jahren fehlte, machte sie wett mit Talent. Niemand an ihrer Schule kam auch nur annäherend an sie heran, wenn es um Python ging. Selbst die Lehrer wandten sich an sie um Hilfe, wenn sie nicht mehr weiterkamen. 18 | 19 | "Du willst nicht wirklich mit ihm ausgehen, oder?" fragte Poppy. "Er hat doch lauter Pickel." 20 | 21 | Penny seufzte. "Nein. Nicht wirklich. Und er hat eine wirklich unangenehme Lache. Aber ich will das Recht haben, mit ihm auszugehen. Das ist es, was unfair ist." 22 | 23 | "Warte mal..." Poppy dachte nach. Sie konnte zwar nicht so gut programmieren wie Penny, dafür ###hartnäckigkeit, querdenkerin, unorthodoxe Lösungen### "Das könnte wirklich unfair sein.." 24 | 25 | Penny schaute von ihrem zerkrumpelten Taschentuch auf. "Ich weiß," sagte sie. 26 | 27 | "Nein, ich meine buchstäblich ungerecht." Poppy hielt inne, sie wußte, dass sie sich hier auf gefährliches Pflaster begab. "Was ist der genaue Altersunterschied deiner Eltern?" 28 | 29 | Pennys Augen begannen zu glitzern - nicht vor Tränen, sondern mit der Andeutung einer schelmischen Ideen. "Das finden wir raus," grinste sie. "Holen wir den Raspberry Pi"" 30 | 31 | ## Die Differenz-Maschine 32 | 33 | "Okay", sagte Penny und wischte ihre Nase am Ärmel ab. "Wie fangen wir an?" 34 | 35 | So arbeiteten sie am besten - Penny saß im Schneidersitz vor dem Computer und hackte den Code ein, während Poppy im Zimmer herumwandelte und über ihre Probleme philosophierte. 36 | 37 | "Zuerst" sagte Poppy. "Hat Python eine Möglichkeit, mit Datum und Uhrzeiten umzugehen?" 38 | 39 | "Oh ja." und Penny begann zu tippen. 40 | 41 |
 42 | from datetime import date
 43 | 
44 | 45 | "Im Prinzip alles, was man tun muss, ist Python gleich zu Beginn zu sagen, welche Spezialfunktionen wir benützen wollen. In diesem Fall gibt es eingebaute Funktionen namens 'datetime', aber wir wollen nur die, die mit Datumsangaben zu tun haben. 46 | 47 | "Können wir das testen?" sagte Poppy vorsichtig. Sie wollte immer sicherstellen, dass alles funktionierte. 48 | 49 | "Sicher!" 50 | 51 |
 52 | print date.today()
 53 | 
54 | 55 | "Das sollte uns das Datum von heute anzeigen." Penny startete das Programm und wurde belohnt mit 56 | 57 |
2014-03-24
58 | 59 | "Hm," sagte Poppy. "Wieso ist das in so einer komischen Reihenfolge geschrieben?" 60 | 61 | Penny liebte es, ihrer Freundin etwas zu erklären. Sie genoß ihren Gesichtsausdruck, wenn sie etwas kapierte. 62 | 63 | "Die Amerikaner schreiben bei Daten den Monat zuerst - 12/11 heißt zum Beispiel 11. Dezember. Aber für Europäer bedeutet es 12. November. Um niemanden zu verwirren, schreiben die meisten Computer Daten als 'Jahr-Monat-Tag'." 64 | 65 | "Oh, das klingt logisch. Also, das ist das Datum von heute - wie erzeugen wir ein bestimmtes Datum?" 66 | 67 | "Ganz einfach," Pennys Hände tanzten über die Tastaur und erzeugten ein angenehmes Klackern. 68 | 69 |
 70 | my_birthday = date(2000, 02, 29)
 71 | 
72 | 73 | "Wir erzeugen eine Variable und weisen ihr ein Datumsobjekt zu. Das ist alles." 74 | 75 | "In Ordnung, wann ist der Geburtstag des Pickeljungen?" 76 | 77 | "Als ob ich das wüßte!" rief Penny. 78 | 79 | Poppy schaute sie scharf an und zählte innerlich bis zehn. 80 | 81 | Penny gab auf als Poppy bei 7 war und flüsterte "15. Januar 1998." 82 | 83 | "Urgh! Er ist letztes Jahrhundert geboren!" 84 | 85 | "Halt die Klappe." Sie fügte die Variable hinzu. 86 | 87 |
 88 | his_birthday = 
 89 | 
90 | 91 | #### Ergänze den Geburtstag des Jungen 92 | 93 | "So, dein Geburtsdatum, sein Geburtsdatum. Wie bestimmen wir, wieviel älter er ist?" 94 | 95 | Penny dachte eine Sekunde nach. 96 | 97 | "Python is normalerweise richtig gut in Mathe. Was passiert, wenn wir..." Sie brach ab, versunken im Code. 98 | 99 | #### Wie würdest du die Differenz aus zwei Daten ausrechnen? 100 | 101 | Die Tastatur klackerte unter Pennys schnellem Tippen. 102 | 103 |
104 | difference = my_birthday - his_birthday
105 | print "The difference is " + str(difference.days)
106 | 
107 | 108 | "Der Unterschied ist wieder ein Datumsobjekt - und das können wir fragen, wieviele Tage es hat. Wir müssen Python aber sagen, dass es es in einen String - eine Zeichenkette - verwandeln soll, damit es richtig ausgedruckt wird." 109 | 110 | Sie ließ das Programm laufen. 111 | 112 | #### Was passiert, wenn du das Programm auf deinem Computer laufen lässt? 113 | 114 | Poppy quietschte "Er ist uuuuralt!" 115 | 116 | Penny funkelte sie an: "Es ist nur so schlimm aus, weil es in Tagen ausgegeben wird. Nicht in Jahren und Monaten." 117 | 118 | Poppy lehnte sich zurück und zwirbelte eine Haarsträhne zwischen ihren Daumen. "Hm, ein Jahr hat 365 Tage..." 119 | 120 | "Vergiss nicht die Schaltjahre", sagte Penny, die da sehr pingelig war. 121 | 122 | "Okay, 365 und ein Viertel." 123 | 124 | "Gut, das ist einfach auszurechnen. Ich nehm die Zahl der Tage und teile sie durch 365,25." 125 | 126 |
127 | print difference.days / 365.25
128 | 
129 | 130 | Penny musste zugeben, dass es nicht viel Unterschied machte. Es sah immer noch ziemlich übel aus. 131 | 132 | Poppy grinste. "Okay, dann schauen wir mal, was es zu deinen Eltern sagt." 133 | 134 | Penny zermarterte sich das Hirn. "Meine Mutter wurde am 17. Oktober 1975 geboren - und Papa am 24. Dezember 1972." 135 | 136 | Sie gab die Daten ein. 137 | 138 | Penny schnappte nach Luft. "Meine Eltern sind so scheinheilig!" Die beiden Mädchen kicherten. 139 | 140 | Penny tätschelte ihren Raspberry Pi. "Dafür haben wir Computer - um komplizierte Dinge einfach zu machen!" 141 | 142 | Sie wandte sich an ihre Freundin, "Machen wir deine Eltern als nächstes!" 143 | 144 | Die Stimmung im Zimmer änderte sich sofort dramatisch. Poppys Erinnerungen an ihren Vater waren immer noch frisch und schmerzhaft. 145 | 146 | "Pops", sagte Penny sanft. 147 | 148 | "Nein, schon in Ordnung," sagte ihre Freundin mit Tränen in den Augen. "Aber ich denke, das ist genug programmiert für heute." 149 | 150 | ## Ein Mitternachtsalptraum 151 | 152 | Es war fast Mitternacht, als Poppy aus dem Bett schlüpfte. Schnell schaltete sie ihr Telefon auf lautlos. Sie horchte, ob ihre Mutter den Wecker gehört hatte. Alles ruhig. 153 | 154 | Poppy schlich zu ihrem Raspberry Pi und schaltete ihn an. 155 | 156 | Sanft berührte sie die Tastatur und fügte eine kleine Änderung in den Python Code ein, den Penny vorher geschrieben hatte. Sie prüfte und prüfte nochmal den Code. Auf keinen Fall konnte sie sich Fehler erlauben. 157 | 158 | Verstohlen sich umschauend, gab sie das heutige Datum ein - 24. März 2013. 159 | 160 | Sie schloß die Augen, holte tief Atem und drückte die Enter-Taste. 161 | 162 | Eine ganze Minute lang wagte sie nicht, die Augen zu öffnen. Sie wünschte es sich so dringend, aber wußte, dass das mehr Gefühle wecken würde, als sie verarbeiten konnte. 163 | 164 | Schließlich nahm sie all ihren Mut zusammen und öffnete die Augen. Da, leuchtend auf dem kleinen Bildschirm waren die Worte, die alles real machten - und bei denen etwas in ihr starb. 165 | 166 |
Dein Vater ist verschwunden seit: 101 Tagen.
167 | 168 | #### Kannst du herausfinden, wann Poppys Vater verschwunden ist? 169 | 170 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PythonPals 2 | ========== 3 | 4 | The Python Pals are "Solve Your Own Adventure" stories to help teach young people Python. 5 | 6 | Help! 7 | ===== 8 | 9 | I would love it if you were to contribute a story. Write your own adventure for Penny and Poppy - The Python Pals. 10 | 11 | Submit new stories as a pull request or - if you spot a typo, please raise a bug report. 12 | 13 | Also, if you're an illustrator or graphic artist, these books need your help too! 14 | 15 | Story Parameters 16 | ================ 17 | 18 | * The two protagonists are Poppy and Penny. Two independently minded young women aged 12 - 15ish. 19 | * They have a Raspberry Pi. 20 | * Poppy's mum is Ada. 21 | * Poppy's dad is not around any more. What has happened to him? 22 | * Penny is Poppy's best friend. 23 | * Penny does most of the typing - she has a tendancy to let Poppy push her around. 24 | * Read the rest of the stories to find out more! 25 | * The adventures can only be solved by writing Python on a Raspberry Pi. Readers must not need anything else in order to complete the task. 26 | 27 | Inspiration 28 | =========== 29 | 30 | When I was a kid, I learned BASIC thanks to the Byte Brothers books. The adventures of two young men determined to solve mysteries using only their wits and their micro-computer. 31 | 32 | You can buy copies of the books from Amazon http://bit.ly/ByteBros and many other retailers. 33 | 34 | I am indebted Lois McCoy (Cowan) the author and copyright holder of the Byte Brothers books for giving her kind permission to adapt some of her wonderful stories. 35 | 36 | LICENSE 37 | ======= 38 | 39 | These stories are provided under a Creative Commons Attribution-ShareAlike 3.0 Unported License -- see http://creativecommons.org/licenses/by-sa/3.0/ 40 | 41 | By submitting stories, pull requests, images, code samples, etc. you agree that they will also be so licenced. 42 | -------------------------------------------------------------------------------- /The Letter Logjam.md: -------------------------------------------------------------------------------- 1 | # The Python Pals Write A Wrong 2 | 3 | Poppy's mother, Ada, was **not** happy. 4 | 5 | "I don't care about that," she shouted down the phone, "Why am I always the one who has to pick up the pieces?" 6 | 7 | She carried on ranting. Penny and Poppy were sat in the lounge. Even though the TV volume was quite high, they could still hear snatches of the conversation. 8 | 9 | Suddenly, the door burst open and Ada stormed in, "What are you two doing right now?" she demanded. 10 | 11 | "Just watching TV," said Poppy, "It's the episode where they..." but before she got a chance to finish, her mother interrupted. 12 | 13 | "Nothing then. Good. You can make yourselves useful." 14 | 15 | "But _mum_!" 16 | 17 | "But nothing. I need to go into town to deal with.... just into town. I'm not going to have a chance to type up all these thank you letters. So I need you to do it." 18 | 19 | Penny smiled sweetly at her friend's mother, "Sure thing, Ada, we'd be happy to!" 20 | 21 | "Good. It's settled. And please get them all done by the time I get back. I need to get them sent and I..." she looked close to tears. 22 | 23 | "Don't worry, mum, we'll get them done. I promise!" 24 | 25 | Ada gave a weak smile, kissed her daughter on the forehead, and left without saying a word. 26 | 27 | Penny turned to her pal and said, "Let's get cracking!" 28 | 29 | They ran into Ada's work room. It was plainly furnished, although the desk was a mess of papers. A vase sat in the corner, its flowers long since withered. 30 | 31 | "What's all this about, Poppy?" asked Penny. 32 | 33 | "There's loads of really yucky diseases out there and mum's been raising money to make sure that young women stay healthy. Now, where does my mum keep her pen and paper?" 34 | 35 | Penny fixed Poppy with a steely glare - the one she usually reserved for people who asked if she wanted to take up cross-country running, or some other mad sport. 36 | 37 | "What do you want to write them all by hand for?" She asked. Out of her blue and white school rucksack she pulled her Raspberry Pi and set it up on the desk. 38 | 39 | Poppy passed Penny a USB stick which had been nestling in a collection of newspaper clippings on her mother's desk, "All of the donors should be on there." 40 | 41 | Penny plugged the stick into the Raspberry Pi. She fiddled around until she could see its file system. 42 | 43 | "Aha, there we go!" Penny said triumphantly "Let me just open it up." 44 | 45 | A few clicks later and the text file displayed on the screen. It showed the names of all the donors and how much they'd given. 46 | 47 |
Anna 2.50
 48 | Beth 5.93
 49 | Cath 23.50
 50 | Dom 15
 51 | Erik 3.14
 52 | ...
 53 | 
54 | ([You can download the file to your computer](https://github.com/edent/PythonPals/blob/master/donors.txt)) 55 | 56 | The file kept on scrolling and scrolling. 57 | 58 | Poppy shrieked, "There's well over a hundred names here! We're never going to be able to finish all these letters by the time my mum comes back! Not even you are that fast a typist." 59 | 60 | "Pops," said Penny, "I don't want to be mean - but you're being _incredibly_ thick. So utterly, mind bendingly dense that I'm not sure if we can be friends any more." Very loudly and slowly she said "We have a computer, you prat! We'll get the Pi to do the hard work for us!" 61 | 62 | Poppy looked sheepish, "Oh... Yeah. Wait! I suppose we can use Python to get this done in a few minutes?" 63 | 64 | Penny put on her cheesiest American accent and drawled, "Got it in one, partner." 65 | 66 | When they had both regained their composure, Penny perched on a chair with both legs tucked underneath her. Poppy hopped up on the windowsill so she could see the screen and her friend at the same time. 67 | 68 | Poppy started, "Well, let's break it down in to easy to manage chunks. You've got a file with the names and amounts people have given, right?" 69 | 70 | "Right." 71 | 72 | "Ok, so we want to read in each line of the file, then create a letter based on the information." 73 | 74 | "That should be pretty easy, Pops." Penny stretched out her fingers and started typing. She was most at home when her fingers were flying over the keyboard. As she typed, she spoke. 75 | 76 | "It just takes one line of code to open a file. You just tell Python where the file is, and if you want to read to the file or write to the file." 77 |
 78 | textfile = open('donors.txt', 'r')
 79 | 
80 | "Our file is in the same directory as this Python program, and we only want to read it. To see if we've got the right file, we can tell Python to display its contents." 81 | 82 |
 83 | print textfile.readlines()
 84 | 
85 | She saved the program and ran it. The file displayed itself in the console. 86 | 87 | "Great," said Poppy, grinning, "One task down. Now every line just has the person's name and how much they donated, right?" 88 | 89 | "Yup. So we can read the file a line at a time and do something with each line. Watch." 90 | 91 | She deleted her code and started again 92 |
 93 | textfile = open('donors.txt', 'r')
 94 | lines = textfile.readlines()
 95 | 
 96 | for line in lines:
 97 |    chunks = line.split()
 98 |    donor_name = chunks[0]
 99 |    donor_amount = chunks[1]
100 |    print donor_name
101 | 
102 | 103 | "So," she said methodically, "We open the file. We get every line. We split every line based on spaces. Of those two chunks, we called the first the donor's name, and the second the donor amount. Then we print out the donor's name - just to be sure." 104 | 105 | Her finger hovered over the keyboard. Just as she was about to run the program, Poppy interrupted. 106 | 107 | "Hang on. Why is '`donor_name`' coming from chunk zero? Shouldn't it be from one - and the amount from chunk two?" 108 | 109 | "Ah," said Penny, "In Python - and most other languages - lists always start from zero." 110 | 111 | "That's dumb," said Poppy. 112 | 113 | "You get used to it." 114 | 115 | "But what's the point in that?" 116 | 117 | "Well, think of the years. We're in the 2000s now. The first year of the 2000 was - obviously - 2000. The second year was 2001, the third was 2002 and so on." 118 | 119 | "Oh!" said Poppy, as things clicked into place, "That makes sense... kinda..." 120 | 121 | "As I said, you get used to it. Now, where were we?" 122 | 123 | "Right, we want to say something like 'Dear `donor_name`, Thanks for your donation, blah blah blah, etc'" 124 | 125 | "You are _so eloquent_!" Penny's sarcasm flew out before she could stop herself. She carried on typing, adding the following lines. 126 | 127 |
128 |    letter = "Dear " + donor_name + ", "
129 |    letter += "Thank you for your donation of " + donor_amount
130 |    print letter
131 | 
132 | 133 | "When you add two strings of text together," she explained, "Python just sticks them next to each other - it's called concatenation." 134 | 135 | "But what are those plus-equals symbols for?" 136 | 137 | "If you want to add something on to an existing variable, you could say `whatever = whatever + something` but it's quicker to say `whatever += something`" 138 | 139 | "Ooooh!" gasped Poppy - who was very good at interjecting with what she perceived as helpful comments, "Make it say 'Generous Donation'!" 140 | 141 | "Yeah, but some of the people _weren't_ very generous." 142 | 143 | "Well, that's easy enough to solve. Only say generous if they gave more than £10." 144 | 145 | "Of course!" Said Penny, "Why didn't I think of that?" 146 | 147 | "Hey! That's why we're such good friends - I'm the brains and you're the...." Poppy trailed off. 148 | 149 | "The typist?" Penny said scornfully as she, hammered the keys angrily. 150 | 151 |
152 |    letter += "Thank you for your "
153 |    if float(donor_amount) > 10:
154 | 
155 | 156 | "What does '`float`' mean?" asked Poppy. 157 | 158 | Penny turned in her chair to face her friend, "Ok, our Python code has read a string of text from the file, right?" 159 | 160 | "Yup" 161 | 162 | "So it thinks that '5' is the character '5' not the _number_ '5'." 163 | 164 | "Ah! So if you had '5'+'2' it would be '52'." 165 | 166 | "Bazinga! So, we need to '[cast](http://www.cyberciti.biz/faq/python-convert-string-to-int-functions/)' the character into the number. Think of it like casting a spell on someone to look like a frog." 167 | 168 | The light dawned on Poppy's face. "I see! So when you say '`float(donor_amount)`' you're saying to Python 'Imagine that this is a number'" 169 | 170 | Penny nodded and carried on typing. 171 | 172 |
173 |    letter += "Thank you for your "
174 |    if float(donor_amount) > 10:
175 |       letter += "very generous "
176 |    letter += "donation of " + donor_amount
177 |    print letter
178 | 
179 | 180 | "It's just simple maths. You can use `>` for seeing if something is greater than something else, or `<` for less than." 181 | 182 | Poppy peered at the keyboard. She cleaned her glasses and peered again. "So where's the greater-than-or-equal-to key?" 183 | 184 | "There isn't one. You just have to type the equals key afterwards like `>=` or `<=`" 185 | 186 | "Nifty. Oh, and not to criticise, but you left off the £ symbol on the donor amount." 187 | 188 | Penny deftly changed the line to read: 189 | 190 |
191 |    letter += "donation of £" + donor_amount
192 | 
193 | 194 | But this time, when she ran the program, something unexpected happened. 195 | 196 | #### Now run the code on your computer 197 | 198 |
SyntaxError: Non-ASCII character '\xc2' in file letter.py on line 9, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
199 | 200 | Both girls swore in unison. There always seemed to be something to trip them up. 201 | 202 | "Computers," opined Penny, "Can be really dumb sometimes. They hate it when you use funny symbols." 203 | 204 | Poppy considered this, "So we need to tell it what characters we'll be using?" 205 | 206 | "Pretty much. We just need to pop this line at the top of our code." Penny scrolled to the top and tapped out the magic incantation. 207 | 208 |
209 | # This Python file uses the following encoding: utf-8
210 | 
211 | 212 | "That ought to do it." 213 | 214 | "So, we've got their name, a message, and how much they donated. What else is there? 215 | 216 | "Nothing!" sang Penny, "Let's carry on watching TV." 217 | 218 | "How about," continued Poppy, blithely ignoring her friend, "How about we told them what their donation has bought?" 219 | 220 | "Or, we could _carry on watching TV_!" 221 | 222 | "No, if a job's worth doing, it's worth doing properly." 223 | 224 | "_Fine_!" Penny huffed, "How much did each vaccination cost?" 225 | 226 | Poppy scrabbled around on her mother's desk, studiously ignoring all the legal papers on there, until she found the document she was looking for. 227 | 228 | "£1.34 per vaccine." 229 | 230 | "That's pretty easy," said Penny, "We'll create a variable for the vaccine cost, then divide the amount donated by that. 231 |
232 | vaccine_cost = 1.34
233 | vaccines_bought = float(donor_amount) / vaccine_cost
234 | letter += ".  That enabled us to buy " + str(vaccines_bought) + " vaccines."
235 | 
236 | "Obviously, we'll have to cast the number back to a string so we can stick it in to the letter." said Penny. 237 | 238 | Poppy got a smug grin on her face, "You're going to have a problem there, Pen." 239 | 240 | "What on Earth are you talking about?" 241 | 242 | "Run it and see..." 243 | 244 | #### Run the code on your computer - what happens? 245 | 246 | Penny ran the program. She stared at the output, twitched her nose and shouted "Rattlesnakes!" 247 | 248 | "The problem," Poppy sighed, "is that computers are _really_ precise. Much more so than humans." 249 | 250 | "Stupid humans," muttered Penny. 251 | 252 | "So, can we just round things down using the 'round()' function?" said Poppy, conspicuously ignoring her friend's desire to transcend humanity and become one with the machines. 253 | 254 | Penny typed away. 255 | 256 |
257 | round( float(donor_amount) / vaccine_cost )
258 | 
259 | 260 | "That's pretty much it, isn't it?" asked Penny plaintively. 261 | 262 | "Yup, let's run off these letters and get back to the TV. If we're lucky, we won't have missed too much." 263 | 264 | A short while later, Ada swung into the house, her humour looking much improved. 265 | 266 | "Girls? Where are you?" 267 | 268 | "In here, mum," called Poppy. 269 | 270 | "What on Earth are you doing in the TV room?" Said Ada, suddenly looking furious, "I thought I asked you to..." 271 | 272 | "Chill, mum," her daughter interrupted. "It's all done." 273 | 274 | "What?" Said Ada, clearly baffled, "How could you have done all of those letters? There were at least a hundred." 275 | 276 | "One hundred and twenty-eight," chimed in Penny. 277 | 278 | They handed over a sheaf of paper to a dumbstruck Ada. 279 | 280 | "But this is marvellous!" she cried, "What wonderful letters - and how clever of you to add in the details about how many vaccines their donations were worth!" 281 | 282 | "Oh, it was nothing," said Poppy airily. "Now," she continued, "We're quite exhausted after all our hard work. Perhaps you would be so kind as to bring us some ice cream?" 283 | 284 | "After all this," beamed Ada, "Ice cream floats for the pair of you!" 285 | 286 | "With chocolate sprinkles?" 287 | 288 | "Don't push your luck, young lady!" And with that, she twirled off into the kitchen. 289 | -------------------------------------------------------------------------------- /The Morse Code Mystery.md: -------------------------------------------------------------------------------- 1 | # The Morse Code Mystery 2 | 3 | ## The Beachcombers 4 | 5 | The waves crashed angrily on the beach, and the rain seemed to come in from all directions. A sudden gust blew a thin drizzle straight down Poppy's collar. 6 | 7 | "This weather is _foul_," she spat. 8 | 9 | "Just five more minutes," yelled Penny. 10 | 11 | Poppy collapsed herself until she was eye level with her friend Penny. Even though Penny was the shorter of the two, she was hunched down on the beach anxiously glancing at the shoreline. 12 | 13 | "Penny, what are we doing here? It's cold, wet, and most importantly," she paused for dramatic effect, "We're going to miss the Food Network's 'Marzipan Marathon'!" 14 | 15 | Her friend giggled - neither of them could stand those TV cookery channels - but they had agreed there was something oddly compelling about watching a man trying to eat his body weight in cake. 16 | 17 | "There!" Yelled Penny, pointing a little way down the shoreline. "I told you that interesting stuff gets washed up here when there's a storm. Grab it!" 18 | 19 | Despite being the older of the two friends, Penny was always being treated like Poppy's younger sister - so she relished in the chance to bark orders at her friend for once. 20 | 21 | Poppy scooped up the large square of blue canvas a spun around. "Please can we go in now?" she whined, "It's cold and wet and..." 22 | 23 | Before she could even finish her sentence, Penny exclaimed "Look! Look at the cloth!" 24 | 25 | They both stared at the raggedy square of canvas. It was peppered with strange white marks. 26 | 27 | "Let's get home and work out what this is," said Poppy. With that, she started sprinting away toward her mum's home. 28 | 29 | "That just what _I was about to say_!" yelled Penny, as she slowly trudged up the beach. 30 | 31 | ## Pi Power 32 | 33 | "Will you be wanting some soup, girls?" asked Poppy's mum, Ada. She had gotten used to both of them storming in and expecting to be fed. 34 | 35 | "No time Ada, thanks," said Penny breathlessly, "We've got to work out what this is." 36 | 37 | "Hmmm, looks like old fashioned Morse Code to me," said Ada after a moment's cursory examination of the cloth. "Where on Earth did you find..." 38 | 39 | "Can we get some soup and croutons, and maybe some cheese. Do we have any crisps?" yelled Poppy as the girls bolted upstairs. 40 | 41 | They flipped on the Raspberry Pi. It had been a birthday present from Poppy's dad. One of the few things she had left to remember him by. Just as Linux was booting up, there was an enormous crack of thunder which shook the whole house. The lights flickered, then went off. 42 | 43 | A second later they were back on, but there was a strange burning smell in the room. 44 | 45 | "Oh no! Cried Penny, "The router!" 46 | 47 | Where their Internet router had been was a smouldering lump of twisted plastic and metal. 48 | 49 | Poppy said a word which, if her mother had overheard her would have meant an instant reduction in pocket money. "How will we find out what 'Morse Code' is if we can't get online?" 50 | 51 | The girls sat, stunned. No Internet. No Facebook. More importantly, no Wikipedia! 52 | 53 | They looked at their phones but, as always they didn't have any credit. 54 | 55 | Poppy looked around the room forlornly. The little study had been Poppy's dad's work area before he... Well. Just before. She scanned the shelves looking for something which might help - even an old fashioned dial-up modem would do. 56 | 57 | Penny suddenly chirruped, "Pops, what's the 'Encyclopaedia Britannica'?" Pointing to an imposing set of books on a shelf. 58 | 59 | "No idea," sniffed Poppy despondently. 60 | 61 | "Only, and I know it was your dad's, but 'encyclopaedia' sounds a bit like 'Wikipedia' and I wondered..." 62 | 63 | [![Ad Encyclopaedia-Britannica 05-1913](https://upload.wikimedia.org/wikipedia/commons/d/d8/Ad_Encyclopaedia-Britannica_05-1913.jpg)](http://commons.wikimedia.org/wiki/File:Ad_Encyclopaedia-Britannica_05-1913.jpg "By Encyclopedia Britannica (scanned by Infrogmation, published on en WP) [Public domain], via Wikimedia Commons") 64 | 65 | "Of course! Quick, which one of the books has 'M' printed on the side?" Poppy smiled. It was rare to see her smile these days, and Penny relished the chance to cheer up her friend. 66 | 67 | They plucked the dusty volume from the shelf and opened it up more-or-less at random. Its musty smell filled the room and they began flicking through the yellowing pages. 68 | 69 | "Where's the search function on this thing?" joked Penny. 70 | 71 | "Möbius, Moomins, Mormon, Morse!" 72 | 73 | There, laid out in the pages of the encyclopedia was a guide to Morse Code. 74 | 75 | [![International Morse Code](http://upload.wikimedia.org/wikipedia/commons/e/e9/International_Morse_code.png)](http://commons.wikimedia.org/wiki/File:International_Morse_code.png "By Rhey T. Snodgrass and Victor F. Camp [Public domain], via Wikimedia Commons") 76 | 77 | The girls flipped on the Pi and waited for it to boot. 78 | 79 | "The most important thing to consider when programming," said Poppy doing her very best imitation of Mr Cartwright their inexcusably dull IT teacher. 80 | 81 | "...is planning!" said Penny, finishing her friend's sentence. 82 | 83 | "We want a program which will let us type in each letter of Morse Code and then show us the English translation." 84 | 85 | Penny grabbed the keyboard. She knew that she would be doing most of the typing while Poppy did most of the pointing out of her mistakes. Apparently, this was known as "pair programming" according to Mr Cartwright. She didn't think much of it. 86 | 87 | "First," said Poppy, "Let's define a [dictionary](http://docs.python.org/2/tutorial/datastructures.html#dictionaries) of English and Morse Code." 88 | 89 | Penny's hands darted across the keyboard as her friend read the dots and dashes from the book. 90 | 91 |
 92 | morse = {  '.-'  :'A', '-...':'B', '-.-.':'C', '-..' :'D', '.'   :'E',
 93 |            '..-.':'F', '--.' :'G', '....':'H', '..'  :'I', '.---':'J'
 94 |         }
 95 | 
96 | 97 | #### Can you program the rest of the dictionary yourself? 98 | 99 | Poppy sighed, "Why on Earth have you bothered to line up all the letters?" 100 | 101 | "It helps me think," said Penny defensively, "I like it neat. Besides, Python doesn't care how I space things like that." 102 | 103 | "Fine! Let's do a quick test to see if it works," said Poppy. 104 | 105 | "Right, I'll ask the user to type a single letter of Morse Code. Then it's really easy to look it up in the dictionary," said Penny. 106 | 107 |
108 | get_code = raw_input('Type a Morse Character ')
109 | print morse[get_code]
110 | 
111 | 112 | She ran the program. As expected it asked her to type in a code. She hit the sequence ".-" and hit enter. 113 | 114 | She was rewarded with a glowing "A" on the screen. 115 | 116 | #### Does your program does the same? 117 | 118 | "Good start," said Poppy. "But it's going to be a pain to type in every Morse character, then hit enter, then try to string the letters together." 119 | 120 | Penny thought for a moment. She knew she could come up with something clever. 121 | 122 | "How about we read in the string we type in, then break it up based on where the spaces are?" She said tentatively. 123 | Poppy looked confused, "What do you mean, Pen?" 124 | 125 | Penny ran Python directly so she could show what she was thinking 126 | 127 |
128 | Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
129 | [GCC 4.6.3] on linux2
130 | Type "help", "copyright", "credits" or "license" for more information.
131 | >>> 
132 | 133 | At the prompt she typed 134 | 135 |
136 | >>> "This is a test".split()
137 | 
138 | 139 | "If I'm right, the [.split()](http://docs.python.org/2/library/string.html#string.split) function will give us a list of all the word." 140 | 141 | She gently thumbed the enter key. 142 | 143 |
['This', 'is', 'a', 'test']
144 | 145 | "Yes!" Shouted both girls simultaneously. 146 | 147 | "So now we will have each Morse Letter in a list, then we want to look it up in the dictionary." 148 | 149 | "That's right. Luckily it's super simple to do something on every item in a list - look," 150 | 151 | Penny typed directly into Python again. 152 | 153 |
154 | >>> list_of_words = "This is a test".split()
155 | >>> for item in list_of_words:
156 | ... print item
157 | 
158 | 159 | But when she hit enter, an error message popped up! 160 | 161 |
162 |   File "<stdin>", line 2
163 |     print item
164 |         ^
165 | IndentationError: expected an indented block
166 | 
167 | 168 | Now it was Penny's turn to swear. 169 | 170 | #### Can you work out where Penny and Poppy have gone wrong? 171 | 172 | "Wipe that grin off your face! Yes, [spacing and indentation are important in Python](http://www.secnetix.de/olli/Python/block_indentation.hawk) . But generally it's fine if I line up my code just how I like it." 173 | 174 | She tried again and hit the spacebar as hard as she thought it could stand. 175 | 176 |
177 | >>> list_of_words = "This is a test".split()
178 | >>> for item in list_of_words:
179 | ...    print item
180 | 
181 | 182 | This time, the result was a lot more satisfactory. 183 | 184 |
185 | ... 
186 | This
187 | is
188 | a
189 | test
190 | >>> 
191 | 
192 | 193 | "Getting there," muttered Poppy under her breath. They swiftly changed the last few lines of their code to read: 194 | 195 |
196 | get_code = raw_input('Type in the Morse Code ')
197 | 
198 | code_list = get_code.split()
199 | 
200 | for item in code_list:
201 |    print morse[item]
202 | 
203 | 204 | "Ok," said Penny, "Let's see what this mysterious blue cloth says on it..." 205 | 206 |
...  ---  ...  -...  ---  .- -  ...  .-  -.  -.-  ---  -.  ..  ...  .-..  .-  -.  -..
207 | 208 | They checked and double checked what they had typed in. 209 | 210 | "Ready?" said Poppy. 211 | 212 | "Let's do it!" said Penny as she hit the enter key. 213 | 214 | #### Can *you* solve the Morse Code Mystery? 215 | 216 | ## Saving The Day 217 | 218 | "Well done you two," said the Mayor. "Without your help, that sailor would have been stuck for long time." 219 | 220 | Penny and Poppy both grinned. It had all been such a rush, they'd gone from running their code, to screaming for Poppy's mum, to calling the coastguard, to seeing a shipwrecked sailor being rescued. They had been interviewed for TV and the clip had _hundreds_ of YouTube likes. 221 | 222 | There was even a Facebook page dedicated to thanking them! 223 | 224 | The Mayor had decided that such fine upstanding citizens deserved a reward - and had invited them to a special event in the town hall. 225 | 226 | "Who would have thought two girls would know Morse Code?" They Mayor said, "Did you learn that at school?" 227 | 228 | Penny fixed him with a steely glare, "No. We taught ourselves." 229 | 230 | The Mayor peered over his glasses, "But surely you had help from someone? An older brother perhaps?" 231 | 232 | Poppy knew the look on Penny's face and answered before her friend lost her temper, "I think you'll find," she said sweetly, "that **women** don't need help from **boys**!" 233 | 234 | Poppy felt Penny's hand slip in to hers. She started to chuckle as her hand was squeezed in firm but measured bursts. 235 | 236 |
-  ....  .-  -  ...  .--  ....  -.--  -.--  ---  ..-  .-  .-.  .  --  -.--  -...  .  ...  -  ..-.  .-.  ..  .  -.  -..
237 | 238 | THE END 239 | -------------------------------------------------------------------------------- /The_Letter_Logjam_de.md: -------------------------------------------------------------------------------- 1 | # Die Python-Freundinnen und der Briefstau 2 | 3 | Poppys Mutter Ada war **nicht** erfreut. 4 | 5 | "Das interessiert mich nicht," schrie sie ins Telefon, "Wieso ist es immer ich, die es wieder einrenken muss?" 6 | 7 | Sie tobte weiter. Penny und Poppy saßen im Wohnzimmer. Obwohl der Fernseher auf voller Lautstärke lief, konnten sie Teile der Unterhaltung mithören. 8 | 9 | Plötzlich ging die Tür auf und Ada stürmte herein. "Was macht ihr zwei gerade?" fragte sie. 10 | 11 | "Nur Fernsehen," sagte Poppy. "Es ist die Folge, wo sie..." aber bevor sie ihren Satz noch zu Ende bringen konnte, unterbrach ihre Mutter sie. 12 | 13 | "Nichts also. Dann könnt ihr euch nützlich machen." 14 | 15 | "Aber Mama!" 16 | 17 | "Aber nix. Ich muss in die Stadt um... also in die Stadt. Ich habe keine Zeit, diese ganzen Dankesbriefe zu tippen. Das müsst ihr machen." 18 | 19 | Penny strahlte die Mutter ihrer Freundin an. "Kein Problem, machen wir doch gerne!" 20 | 21 | "Gut, das wäre also geklärt. Und bitte habt sie fertig, bis ich wieder da bin. Ich muss sie losschicken und ich..." sie sah aus als wäre sie den Tränen nahe. 22 | 23 | "Keine Sorge, Mama, wir machen sie fertig. Versprochen!" 24 | 25 | Ada versuchte zu lächeln, küsste ihre Tochter auf die Stirn und ging, ohne noch etwas zu sagen. 26 | 27 | Penny wandte sich an ihre Freundin und sagte: "Dann mal los!" 28 | 29 | Sie liefen in Adas Arbeitszimmer. Es war spärlich eingerichtet, aber auf dem Schreibtisch stapelten sich die Papiere. An der Ecke stand eine Vase voll lang vertrockneter Blumen. 30 | 31 | "Worum geht es eigentlich, Poppy?" fragte Penny. 32 | 33 | "Es gibt richtig eklige Krankheiten und Mama sammelt Spenden damit junge Frauen gesund bleiben. Also, wo hat meine Mutter Stifte und Papier?" 34 | 35 | Penny sah Poppy scharf an, als ob sie sie gerade zu einem Marathon oder einer ähnlich sinnfreien Sportart aufgefordert hätte. 36 | 37 | "Du willst das nicht etwa alles von Hand schreiben?" fragte sie. Sie holte den Raspberry Pi aus ihrem weißblauen Schulrucksack und legte ihn auf den Tisch. 38 | 39 | Poppy reichte Penny einen USB-Stick, der sich unter einem Stapel Zeitungsauschnitte versteckt hatte. "Die Spender sollten alle hier drauf sein." 40 | 41 | Penny steckte den USB-Stick in den Raspberry Pi und klickte herum, bis sie die Datei gefunden hatte. 42 | 43 | "Aha, da ist sie ja!" sagte sie triumphierend. "Lass sie mich schnell öffnen." 44 | 45 | Einen Moment später erschien der Text auf dem Bildschirm. Er enthielt die Namen aller Spender und wieviel sie gespendet hatten. 46 | 47 |
Anna 2.50
 48 | Beth 5.93
 49 | Cath 23.50
 50 | Dom 15
 51 | Erik 3.14
 52 | ...
 53 | 
54 | ([Du kannst diese Datei hier herunter laden](https://github.com/edent/PythonPals/blob/master/donors.txt)) 55 | 56 | Die Datei scrollte und scrollte. 57 | 58 | Poppy kreischte: "Das sind ja hunderte! Das haben wir nie im Leben fertig, bis meine Mutter zurück kommt. Nicht mal du tippst so schnell." 59 | 60 | "Poppy," sagte Penny. "Ich will ja nicht gemein sein, aber manchmal stehst du echt auf der Leitung." Laut und ganz langsam sagte sie "Wir haben einen Computer, du Nuß! Wir lassen den Pi für uns die Arbeit erledigen." 61 | 62 | Poppy schaute bedröppelt. "Oh...ja. Vermutlich können wir das mit Python in ein paar Minuten schaffen." 63 | 64 | "Miss Poppy, damit haben Sie den Nagel auf den Kopf getroffen," sagte Penny geziert. 65 | 66 | Als sie fertig gelacht hatten, hockte sich Penny mit angezogenen Beinen auf einen Stuhl. Poppy kletterte aufs Fensterbrett, um gleichzeitig auf den Bildschirm und ihre Freundin zu sehen. 67 | 68 | Poppy fing an: "Nun, teilen wir das Ganze mal auf in leicht handlebare Teile. Wir haben eine Datei mit Namen und Geldbeträgen, korrekt?" 69 | 70 | "Korrekt." 71 | 72 | "Okay, dann wollen wir jeweils eine Zeile dieser Datei lesen und dann einen Brief mit diesen Infos erzeugen." 73 | 74 | "Das sollte ziemlich einfach sein, Poppy." Penny knackte mit den Fingern und fing an zu tippen. Sie fühlte sich am besten, wenn ihre Finger über eine Tastatur flogen. Während sie tippte, redete sie. 75 | 76 | "Eine Datei zu öffnen braucht nur eine Zeile Code. Du sagst Python, wo die Datei ist und ob wir sie lesen oder reinschreiben wollen." 77 | 78 |
 79 | textfile = open('donors.txt', 'r')
 80 | 
81 | 82 | Sie speicherte das Programm und starte es. Die Datei erschien in der Konsole. 83 | 84 | "Großartig," grinste Poppy. "Teil Eins erledigt. Jede Zeile enthält nur den Namen der Person und wieviel sie gespendet hat, richtig?" 85 | 86 | "Yup. Also können wir die Datei Zeile für Zeile lesen und dann mit jeder Zeile etwas tun. Schau!" 87 | 88 | Sie löschte ihren Code und fing nochmal an 89 | 90 |
 91 | textfile = open('donors.txt', 'r')
 92 | lines = textfile.readlines()
 93 | 
 94 | for line in lines:
 95 |    chunks = line.split()
 96 |    spender_name = chunks[0]
 97 |    spender_betrag = chunks[1]
 98 |    print spender_name
 99 | 
100 | 101 | 102 | 103 | "So", sagte sie. "Wir öffnen die Datei. Wir holen uns jede Zeile. Wir teilen die Zeile am Leerzeichen auf. Von diesen zwei Teilen nennen wir den ersten spender_name und den zweiten spender_betrag. Nur um sicherzugehen, drucken wir noch den Namen des Spenders." 104 | 105 | Ihre Finger schwebten über der Tastatur. Gerade als sie wieder das Programm starten wollte, unterbrach Poppy sie. 106 | 107 | "Halt mal. Warum ist spender_name Teil Null? Sollte das nicht Eins - und der Betrag Zwei sein?" 108 | 109 | "Ja," sagte Penny. "In Python - und den meisten anderen Programmiersprachen - fangen Listen bei 0 an." 110 | 111 | "Das ist ja doof," sagte Poppy. 112 | 113 | "Man gewöhnt sich dran." 114 | 115 | "Aber was ist der Witz dran?" 116 | 117 | "Denk mal an Jahre. Wir sind jetzt im zweiten Jahrtausend. Das erste Jahr des zweiten Jahrtausend war 2000. Das zweite Jahr war 2001, das dritte 2002 und so weiter." 118 | 119 | "Oh!" sagte Poppy, als ihr ein Licht aufging. "Das ergibt Sinn..." 120 | 121 | "Wie ich sagte, man gewöhnt sich dran. Jetzt, wo waren wir?" 122 | 123 | "Wir wollten so etwas schreiben wie "Liebe(r) spender_name, danke für deine Spende, bla blah blah etc." 124 | 125 | "Du bist soo beredt!" kommentierte Penny sarkastisch. Sie tippte weiter und ergänzte die folgenden Zeilen. 126 | 127 |
128 |    letter = "Liebe(r) " + spender_name + ", "
129 |    letter += "danke für deine Spende von " + spender_betrag
130 |    print letter
131 | 
132 | 133 | "Wenn du die zwei Zeichenketten addierst," erklärte sie "hängt Python sie einfach aneinander - das nennt man Concatenation." 134 | 135 | "Aber wozu sind diese plus-istgleich-Zeichen da?" 136 | 137 | "Wenn du einer existierenden Variable etwas hinzufügen willst, könntest du sagen 'wasauchimmer = wasauchimmer + etwas' aber es geht viel schneller zu sagen 'wasauchimmer += etwas'" 138 | 139 | "Oooooooh!" schnaufte Poppy - die sehr gut darin war, aus ihrer Sicht hilfreiche Kommentare einzuwerfen. "Schreib 'großzügige Spende'!" 140 | 141 | "Aber manche Leute waren nicht besonders großzügig." 142 | 143 | "Das lässt sich leicht lösen. Wir schreiben nur großzügig, wenn sie mehr als 20 Euro gespendet haben." 144 | 145 | "Natürlich!" rief Penny. "Warum hab ich nicht daran gedacht?" 146 | 147 | "Hey, deshalb sind wir so gute Freundinnen - ich bin das Hirn und du bist..." Poppy brach ab. 148 | 149 | "die Tipse?" sagte Penny verächtlich, als sie wütend in die Tasten haute. 150 | 151 |
152 |    letter += "Danke für deine "
153 |    if float(spender_betrag) > 10:
154 | 
155 | 156 | "Was bedeutet 'float'?" fragte Poppy. 157 | 158 | Penny drehte sich, so dass sie ihrer Freundin ins Gesicht schaute. "Ok, unser Python-code hat einen String Text aus der Datei gelesen, korrekt?" 159 | 160 | "Yup." 161 | 162 | "Also denkt es, dass '5' das Zeichen '5' ist und nicht die _Zahl_ '5'." 163 | 164 | "Ah! Wenn du also '5'+'2' addierst, ergäbe das '52'." 165 | 166 | "Bingo! Deshalb müssten wir das Zeichen in eine Zahl 'casten'. Stell dir das als Zauberspruch vor, mit dem du jemand in einen Frosch verwandelst." 167 | 168 | Poppy ging ein Licht auf. "Kapiert! Wenn du also '`float(spender_betrag)`' schreibst, sagst du Python 'Stell dir das als Zahl vor'" 169 | 170 | Penny nickte und tippte weiter. 171 | 172 |
173 |    letter += "Danke für deine "
174 |    if float(spender_betrag) > 10:
175 |       letter += "großzügige "
176 |    letter += "Spende von " + spender_betrag
177 |    print letter
178 | 
179 | 180 | "Es ist ganz einfache Mathe. Du kannst > benützen wenn etwas größer als etwas anderes ist und < für weniger als." 181 | 182 | Poppy lugte auf die Tastatur. Sie putzte ihre Brille und schaute nochmal. "Und wo ist das Größer-Gleich-Zeichen?" 183 | 184 | "Das gibt es nicht. Du musst einfach `=` danach tippen, wie `>=` oder `<=`" 185 | 186 | "Schick. Upps, keine Kritik, aber du hast das Euro-Zeichen nach dem Betrag vergessen." 187 | 188 | Flink änderte Penny die Zeile zu: 189 | 190 |
191 |    letter += "Spende von " + spender_betrag + " €"
192 | 
193 | 194 | Aber als sie diesmal das Programm starteten, passierte etwas unerwartetes. 195 | 196 | #### Lass den Code auf deinem Computer laufen 197 | 198 |
SyntaxError: Non-ASCII character '\xc2' in file letter.py on line 9, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
199 | 200 | Die Mädchen fluchten im Chor. Immmer kam ihnen so etwas dazwischen. 201 | 202 | "Computer" meinte Penny, "sind manchmal richtig dämlich. Sie hassen es, wenn man komische Zeichen benutzt." 203 | 204 | Poppy dachte darüber nach: "Dann müssen wir ihm also verraten, welche Zeichen wir benützen?" 205 | 206 | "Sehr richtig. Wir brauchen genau eine Zeile am Anfang unseres Programms." Penny scrollte hoch und tippte die magische Formel. 207 | 208 |
209 | # This Python file uses the following encoding: utf-8
210 | 
211 | 212 | "Das sollte es erschlagen." 213 | 214 | "Also, wir haben den Namen, wieviel sie gespendet haben und eine Nachricht. Was brauchen wir noch?" 215 | 216 | "Nichts!" sang Penny. "Schauen wir weiter fern." 217 | 218 | "Wie wäre es," fuhr Poppy fort, ihre Freundin ignorierend, "wenn wir ihnen noch sagen, was mit ihrer Spende gekauft wurde?" 219 | 220 | "Oder wir könnten weiter fernsehen!" 221 | 222 | "Nein. Wenn schon, dann machen wir es richtig." 223 | 224 | "Ookay!" schnupfte Penny. "Wieviel kostet eine Impfung?" 225 | 226 | Poppy kramte auf dem Schreibtisch ihrer Mutter, alle juristischen Sachen dort gezielt ignorierend, bis sie das Blatt fand, nach dem sie gesucht hatte. 227 | 228 | "1,34 Euro pro Impfung." 229 | 230 | "Das ist einfach," sagte Penny. "Wir erzeugen eine Variabe für die Impfkosten und teilen den Spendenbetrag dadurch. 231 | 232 |
233 | impfung_kosten = 1.34
234 | impfungen_gekauft = float(spender_betrag) / impfung_kosten
235 | letter += ".  Damit konnten wir " + str(impfungen_gekauft) + " Impfungen bezahlen."
236 | 
237 | 238 | "Jetzt müssen wir noch diese Zahl zurück in einen String casten, den wir dann in den Brief setzen." sagte Penny. 239 | 240 | Poppy grinste süffisant. "Da wirst du ein Problem kriegen, Pen." 241 | 242 | "Was zur Hölle meinst du damit?" 243 | 244 | "Starte den Code und schau..." 245 | 246 | #### Lass den Code auf deinem Computer laufen - was passiert? 247 | 248 | Penny startete das Programm. Sie starrte auf die Ausgabe, rümpfte ihre Nase und rief "Scheibenkleister!" 249 | 250 | "Das Problem ist," seufzte Poppy, "dass Computer _wirklich_ genau sind. Viel genauer als Menschen." 251 | 252 | "Dumme Menschen," murmelte Penny. 253 | 254 | "Also, können wir das nicht einfach abrunden mit der 'round()'-Funktion?" sagte Poppy, während sie die Sehnsucht ihrer Freundin, die Menschheit zu transzendieren und mit den Maschinen eins zu werden, demonstrativ ignorierte. 255 | 256 | Penny tippte los. 257 | 258 |
259 | round( float(spender_betrag) / impfung_kosten )
260 | 
261 | 262 | "Damit haben wir alles, oder?" fragte Penny traurig. 263 | 264 | "Yup. Lass uns die Briefe ausdrucken und weiter fernsehen. Wenn wir Glück haben, haben wir nicht viel verpasst." 265 | 266 | Ein Weilchen später kam Ada zur Tür herein, in sichtlich besserer Laune. 267 | 268 | "Mädels, wo seid ihr?" 269 | 270 | "Hier, Mama," rief Poppy. 271 | 272 | "Was zum Teufel macht ihr vor dem Fernseher?" schimpfte Ada. "Ich dachte ich hätte euch gebeten..." 273 | 274 | "Chill mal, Mama," unterbrach sie ihre Tochter. "Alles erledigt." 275 | 276 | "Was?" rief Ada verblüfft. "Wie könnt ihr in dieser Zeit die ganzen Briefe geschafft haben? Das waren mindestens hundert." 277 | 278 | "128." fiel Penny ein. 279 | 280 | Sie überreichten der sprachlosen Ada einen Stapel Papier. 281 | 282 | "Aber das ist fantastisch!" rief sie, "was für tolle Briefe - und wie schlau von euch, noch einzufügen, wieviele Impfstoffe es für die Spende gibt." 283 | 284 | "Ach, nicht der Rede wert." sagte Poppy lässig. "Jetzt sind wir aber völlig k.o. von der ganzen Arbeit. Können wir ein Eis haben?" 285 | 286 | Ada strahlte. "Nach dieser Aktion gibt es Rieseneisbecher für euch beide!" 287 | 288 | "Mit Schokoladenstreuseln?" 289 | 290 | "Übertreib mal nicht, junge Dame!" Und damit wirbelte sie in die Küche. 291 | -------------------------------------------------------------------------------- /The_Morse_Code_Mystery_de.md: -------------------------------------------------------------------------------- 1 | # Das Geheimnis des Morse-Codes 2 | 3 | ## Strandgut 4 | 5 | Die Wellen krachten wie wütend auf den Strand und der Regen schien von allen Seiten gleichzeitig zu kommen. Eine plötzliche Böe blies einen dünnes Rinnsal direkt in Poppys Kragen. 6 | 7 | "Dieses Wetter ist beschissen" fauchte sie. 8 | 9 | "Nur noch fünf Minuten," schrie Penny. 10 | 11 | Poppy machte sich kleiner, so dass sie auf Augenhöhe mit ihrer Freundin Penny war. Penny war eh schon die kleinere der zwei, aber jetzt starrte sie gebückt in die Brandung. 12 | 13 | "Penny, was machst du da? Es ist kalt, naß, und viel wichtiger noch" sie machte eine dramatische Pause, um ihren Worten mehr Nachdruck zu verleihen: "wir verpassen den "Marzipan Marathon" im Fernsehen!" 14 | 15 | Ihre Freundin kicherte - sie konnten beide die Kochsendungen im Fernsehen nicht ausstehen, aber sie waren sich einig darin, dass etwas unheimlich zwanghaftes darin lag, einen Mann sein Körpergewicht in Kuchen essen zu sehen. 16 | 17 | "Da" rief Penny und deutete auf ein Stück Ufer etwas entfernt. "Ich hab dir erzählt, dass hier lauter interessante Sachen angespült werden, wenn es stürmt. Pack es!" 18 | 19 | Obwohl sie die ältere der zwei Freundinnen war, wurde Penny immer für Poppys jüngere Schwester gehalten. Daher genoß sie die Gelegenheit, einmal ihrer Freundin Befehle zuzubrüllen. 20 | 21 | Poppy fischte das große blaue Tuch aus dem Wasser und drehte sich um. "Können wir jetzt bitte reingehen?" jammerte sie. "Es ist kalt und nass..." 22 | 23 | Bevor sie den Satz zuende bringen konnte, rief Penny: "Schau, schau dir das Tuch an!" 24 | 25 | Sie starrten beide das abgenutzten Leintuch an. Es war übersäht mit seltsamen weißen Zeichen. 26 | 27 | "Gehen wir nach Hause und finden heraus, was das ist," sagte Poppy. Und damit fing sie an, zum Haus ihrer Mutter zu rennen. 28 | 29 | "Genau das wollte ich gerade sagen!" rief Penny, als sie ihr langsam den Strand entlang hinterhertrottete. 30 | 31 | ## Pi Power 32 | 33 | "Mögt ihr eine Suppe, Mädels?" fragte Poppys Mutter Ada. Sie war es gewöhnt, dass die beiden hereinstürmten und Hunger hatten. 34 | 35 | "Keine Zeit, Ada, danke," sagte Penny atemlos. "Wir müssen herausfinden, was das ist." 36 | 37 | "Hmmm, das sieht mir wie guter altmodischer Morse-Code aus," sagte Ada nach einem kurzen Blick auf das Tuch. "Wo um alles in der Welt habt ihr das gefunden?" 38 | 39 | "Könnten wir ein bißchen Suppe mit Croutons haben? Und vielleicht Käse? Haben wir noch Chips?" rief Poppy als die Mädchen die Treppe raufsausten. 40 | 41 | Sie schalteten den Raspberry Pi ein. Er war ein Geburtstagsgeschenk von Poppys Vater. Eines der wenigen Dinge, die ihr geblieben waren, um sich an ihn zu erinnern. Gerade als das Linux bootete, ließ ein Donnerschlag das ganze Hause erbeben. Die Lampen flackerten und gingen aus. 42 | 43 | Eine Sekunde später gingen sie wieder an, was blieb, war ein seltsam verbrannter Geruch im Zimmer. 44 | 45 | "Oh nein," stöhnte Penny, "der Router!" 46 | 47 | Wo der Internet-Router gewesen war, befand sich nur noch ein Klumpen geschmolzenes Plastik und Metall. 48 | 49 | Poppy sagte ein Wort, das, wenn es ihre Mutter gehört hätte, sofortigen Taschengeldentzug hervorgerufen hätte. "Wie sollen wir herausfinden, was Morse-Code ist, wenn wir nicht Online kommen?" 50 | 51 | Die Mädchen saßen benommen da. Kein Internet. Kein Facebook. Und viel wichtiger noch, keine Wikipedia! 52 | 53 | Sie schauten auf ihre Telefone, aber wie üblich war darauf kein Cent Guthaben mehr. 54 | 55 | Poppy sah sich verlassen im Zimmer um. Der kleine Raum war das Arbeitszimmer von Poppys Vater gewesen, bevor er... Bevor eben. Sie ließ ihren Blick schweifen über die Regale, auf der Suche nach etwas, das helfen würde - selbst ein altmodisches Wählmodem wäre genug. 56 | 57 | Penny zirpte plötzlich: "Pops, was ist diese Brockhaus Enyzklopädie?" Sie deutete auf eine eindrucksvolle Sammlung ledergebundener dicker Bände im Regal. 58 | 59 | "Keine Ahnung," schniefte Poppy niedergeschlagen. 60 | 61 | "Naja, ich weiß, die gehört deinem Vater, aber Enzyklopädie klingt ein bißchen wie Wikipedia und ich frage mich..." 62 | 63 | "Na klar! Schnell, welcher der Bände hat ein 'M' aufgedruckt?" Poppy lächelte. Man sah sie selten lächeln zur Zeit und Penny genoß die Gelegenheit, ihre Freudin aufzumuntern. 64 | 65 | Sie zogen den staubigen Band aus dem Regal und öffneten ihn auf gut Glück. 66 | 67 | "Wo ist die Suchfunktion bei diesem Ding?" witzelte Penny. 68 | 69 | "Möbius, Mormon, Morse!" 70 | 71 | Dort, auf einer ganzen Seite der Enzyklopädie, fanden sie eine Anleitung für Morse-Code. 72 | 73 | Die Mädchen schalteten den Raspberry ein und warteten darauf, dass er bootete. 74 | 75 | "Die wichtigste Sache, die es beim Programmieren zu beachten gilt," imitierte Poppy Herrn Schneider, ihren unfassbar langweiligen Informatiklehrer. 76 | 77 | "...ist Planung!" vollendete Penny den Satz ihrer Freundin. 78 | 79 | "Wir wollen ein Programm, das uns jeweils einen Morse-Code-Buchstaben eintippen lässt und uns die deutsche Übersetzung zeigt." 80 | 81 | Penny griff zur Tastatur. Ihr war schon klar, dass sie den Hauptteil der Tipperei erledigen würde während Poppy auf ihre Fehler hinwies. Herrn Schneider zufolge war dieses Konzept als "Pair Programming" bekannt. Sie hielt nicht viel davon. 82 | 83 | "Als erstes", sagte Poppy, "lass uns ein Wörterbuch Deutsch/Morse Code anlegen." 84 | 85 | Pennys Hände flogen über die Tastatur, als ihre Freundin ihr die Punkte und Striche aus dem Buch vorlas. 86 | 87 |
 88 | morse = {  '.-'  :'A', '-...':'B', '-.-.':'C', '-..' :'D', '.'   :'E',
 89 |            '..-.':'F', '--.' :'G', '....':'H', '..'  :'I', '.---':'J'
 90 |         }
 91 | 
92 | 93 | #### Kannst du den Rest des Wörterbuchs selbst programmieren? 94 | 95 | Poppy seufzte: "Warum zum Teufel richtest du die ganzen Buchstaben gleich aus?" 96 | 97 | "Es hilft mir beim Denken," verteidigte sich Penny."Ich hab's nun ma gern ordentlich. Und Python kümmert es nicht, wie ich die Sachen formatiere." 98 | 99 | "Okay, testen wir schnell, ob es funktioniert," sagte Poppy. 100 | 101 | "Wir fordern den Benutzer auf, ein Morsezeichen einzugeben. Dann lässt es sich einfach im Wörterbuch nachschlagen, " sagte Penny. 102 | 103 |
104 | get_code = raw_input('Gib ein Morse-Zeichen ein ')
105 | print morse[get_code]
106 | 
107 | 108 | Sie ließ das Programm laufen. Wie erwartet, fragte es sie nach einem Zeichen. Sie gab die Folge ".-", drückte Enter und wurde mit einem leuchtenden "A" auf dem Bildschirm belohnt. 109 | 110 | #### Macht dein Programm das Gleiche? 111 | 112 | "Guter Anfang, " sagte Poppy. "Aber es wird eine Heidenarbeit, immer ein Morsezeichen einzutippen, dann Enter zu drücken und danach alle Buchstaben wieder zusammenzusetzen." 113 | 114 | Penny dachte einen Moment nach. Sie wußte, dass ihr etwas besseres einfallen würde. 115 | 116 | "Wie wäre es, wenn wir alle Zeichen einlesen und dann anhand der Leerzeichen aufteilen?" schlug sie vor. 117 | 118 | Poppy blickte verwirrt. "Was meinst du genau, Pen?" 119 | 120 | Penny rief direkt Python auf um zu zeigen, was ihr vorschwebte. 121 | 122 |
123 | Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
124 | [GCC 4.6.3] on linux2
125 | Type "help", "copyright", "credits" or "license" for more information.
126 | >>> 
127 | 128 | Am Prompt tippte sie: 129 | 130 |
131 | >>> "Das ist ein Test".split()
132 | 
133 | 134 | "Wenn ich damit richtig liege, gibt uns die Split-Funktion eine Liste aller Wörter." 135 | 136 | Sanft drückte sie auf Enter. 137 | 138 |
['Das', 'ist', 'ein', 'Test']
139 | 140 | "Yeah!" riefen die beiden Mädchen simultan. 141 | 142 | "Damit haben wir also jedes Morsezeichen in einer Liste, dann wollen wir sie im Wörterbuch nachschlagen." 143 | 144 | "Korrekt. Schickerweise ist es supereinfach etwas für jede Sache in einer Liste zu machen - schau," 145 | 146 | Penny tippte wieder direkt in die Python-Konsole. 147 | 148 |
149 | >>> list_of_words = "Dies ist ein Test".split()
150 | >>> for item in list_of_words:
151 | ... print item
152 | 
153 | 154 | Aber diesmal, als sie Enter drückte, erschien eine Fehlermeldung! 155 | 156 |
157 |   File "<stdin>", line 2
158 |     print item
159 |         ^
160 | IndentationError: expected an indented block
161 | 
162 | 163 | Diesmal war Penny mit Fluchen an der Reihe. 164 | 165 | #### Kannst du herausfinden, was Penny und Poppy falsch gemacht haben? 166 | 167 | "Wisch dir dieses Grinsen vom Gesicht! Ja, Leerzeichen und Einrückung sind wichtig in Python. Aber generell ist es in Ordnung, wenn ich meinen Code so formatiere, wie ich es mag." 168 | 169 | Sie versuchte es nochmal und haute dabei so hart auf die Leertaste, wie diese es gerade noch aushielt. 170 | 171 |
172 | >>> list_of_words = "Dies ist ein Test".split()
173 | >>> for item in list_of_words:
174 | ...    print item
175 | 
176 | 177 | Diesal war das Ergebnis deutlich zufriedenstellender. 178 |
179 | ... 
180 | Dies
181 | ist
182 | ein
183 | Test
184 | >>> 
185 | 
186 | 187 | "Geht ja." murmelte Poppy vor sich hin. Schnell tauschten sie die letzten Zeilen ihres Programms aus, sodass da nun folgendes stand: 188 | 189 |
190 | get_code = raw_input('Gib ein Morse-Zeichen ein ')
191 | 
192 | code_list = get_code.split()
193 | 
194 | for item in code_list:
195 |    print morse[item]
196 | 
197 | 198 | "Ok," sagte Penny, "jetzt lass mal sehen, was dieses mysteriöse blaue Tuch zu bedeuten hat..." 199 | 200 |
... --- ... ... -.-. .... .. ..-. ..-. -... .-. ..- . -.-. .... .. --. . .-. .- ..- ..-. .. -. ... . .-..
201 | 202 | Sie prüften nochmal und nochmal, was sie eingetippt hatten. 203 | 204 | "Bereit?" fragte Poppy. 205 | 206 | "Los," sagte Penny und drückte Enter. 207 | 208 | #### Kannst du das Morse-Code-Räsel lösen? 209 | 210 | ## Die Retterinen des Tages 211 | 212 | "Gut gemacht," sagte die Bürgermeisterin. "Ohne eure Hilfe wäre dieser Segler ganz schön lange festgesessen." 213 | 214 | Penny und Poppy grinsten. Alles war in so einem Rausch passiert, von Laufenlassen ihres Programms zum Herbeischreiben von Poppys Mutter, zum Alarmieren der Küstenwache, bis zum Verfolgen wie der schiffbrüchige Segler gerettet wurde. Sie waren vom Fernsehen interviewt worden und der Clip hatte hunderte von YouTube-Likes. 215 | 216 | Es gab sogar eine Facebook-Seite mit Danksagungen! 217 | 218 | Die Bürgermeisterin hatte beschlossen, dass solche feinen engagierten Bürgerinnen eine Belohnung verdienten - und hatte sie ins Rathaus eingeladen. 219 | 220 | "Wer hätte gedacht, dass zwei Mädchen Morse-Code kennen?" sagte die Bürgermeisterin. "Habt ihr das in der Schule gelernt?" 221 | 222 | Poppy fixierte sie mit eisernem Blick. "Nein. Das haben wir uns selbst beigebracht." 223 | 224 | Die Bürgermeisterin linste über ihre Brille. "Aber ihr hattet doch sicher Hilfe von jemand? Einem älteren Bruder vielleicht?" 225 | 226 | Poppy kannte Pennys Gesichtsausdruck und antwortete, bevor ihre Freundin die Beherrschung verlor. "Sie werden feststellen," sagte sie zuckersüß, "dass **Frauen** keine Hilfe von **Jungen** brauchen! 227 | 228 | Poppy fühlte, dass Penny ihre Hand nahm. Sie fing an zu glucksen, als ihre Hand in festen, aber genauen Abständen gedrückt wurde. 229 | 230 |
-.. .- .-. ..- -- -... .. ... - -.. ..- -- . .. -. . -... . ... - . ..-. .-. . ..- -. -.. .. -.
231 | 232 | -------------------------------------------------------------------------------- /donors.txt: -------------------------------------------------------------------------------- 1 | Anna 2.5 2 | Beth 5.93 3 | Cath 23.5 4 | Dom 15 5 | Erik 3.14 6 | Les 11.31 7 | Blair 0.68 8 | Sheilah 24.16 9 | Wally 29.47 10 | Delfina 37.1 11 | Arlene 15.24 12 | Rafael 28.42 13 | Mickie 26.96 14 | Elva 21.19 15 | Benton 25.23 16 | Damion 43.69 17 | Mariette 28.92 18 | Effie 45.01 19 | Christie 17.09 20 | Reagan 8.19 21 | Shery 46.96 22 | Aaron 39.51 23 | Chrystal 40.36 24 | Walter 19.35 25 | Loria 44.62 26 | Garrett 17.49 27 | Malka 47.83 28 | Charley 39.93 29 | Ema 29.46 30 | Coreen 33.3 31 | Barbra 36.24 32 | Joelle 12.43 33 | Laveta 8.55 34 | Eldon 6.08 35 | Ara 40.19 36 | Trish 34.84 37 | Theda 7 38 | Lucie 1.01 39 | Debbi 21 40 | Carter 39.78 41 | Aida 15.31 42 | Eleni 28.43 43 | Bernetta 26.16 44 | Lexie 49.69 45 | Georgina 39.27 46 | Tobie 22.75 47 | Aura 18.17 48 | Meri 36.43 49 | Cinderella 41.08 50 | Danna 48.95 51 | Loyce 39.85 52 | Oliver 5.95 53 | Mittie 46.8 54 | Ozell 16.73 55 | Ronny 7.73 56 | Maurita 19.78 57 | Annamaria 23.4 58 | Kenny 29.27 59 | Latesha 1.95 60 | Aron 22.42 61 | Edwin 32.17 62 | Emeline 22.4 63 | Caitlin 5.11 64 | Kandra 27.13 65 | Loura 28.48 66 | Betsy 45.31 67 | Helen 11.97 68 | Sammy 35.47 69 | Aura 46.31 70 | Cuc 32.97 71 | Arnoldo 25.26 72 | Evelyne 11.62 73 | Wei 11.41 74 | Jacalyn 1.41 75 | Gudrun 36.91 76 | Lloyd 25.12 77 | Mavis 29.79 78 | Epifania 19.75 79 | Phillis 9.2 80 | Argentina 42.35 81 | Crissy 31.37 82 | Cherise 38.27 83 | Maryellen 33.68 84 | Tonja 42.05 85 | Sherika 35.63 86 | Rolf 7.5 87 | Shila 39.89 88 | Donnetta 0.92 89 | Joanne 10.81 90 | Merideth 36.16 91 | Rudolph 32.79 92 | Ali 14.3 93 | Liberty 7.43 94 | Frieda 36.37 95 | Arline 34.38 96 | Diann 10.84 97 | Collin 46.59 98 | Melani 18.48 99 | Lavinia 47.16 100 | Nam 18.33 101 | Penny 30.78 102 | Mae 3.43 103 | Star 14.87 104 | Akiko 47.85 105 | Denis 26.88 106 | Judith 1.78 107 | Conrad 22.97 108 | Aletha 6.67 109 | Evelynn 21.54 110 | Alecia 32.17 111 | Matilda 49.02 112 | Nadene 2.9 113 | Jennifer 20.45 114 | Tamra 32.7 115 | Vanna 44.95 116 | Caridad 21.27 117 | Dovie 3.59 118 | Delbert 20.08 119 | Shavon 3.4 120 | Mora 10.21 121 | Eugena 34.1 122 | Alma 36.32 123 | Kip 21.75 124 | Ashly 12.3 125 | Mireille 6.27 126 | Abdul 46.54 127 | Marica 17.07 128 | Glennis 23.45 --------------------------------------------------------------------------------