Commit 3e7f4ef3 authored by r.grosso's avatar r.grosso
Browse files

Add notebook with solutions to Python exercises.

parent 8bc3f4b2
%% Cell type:markdown id: tags:
### Aufgabe 1 b)
%% Cell type:code id: tags:
``` python
name = input("Please enter your name:")
age = int(input("Hallo ", name, ". In welchem Jahr bist du geboren? "))
import datetime
now = datetime.datetime.now()
age = now.year - age
print("Du bist ", age, " Jahre alt (oder wirst dieses Jahr werden)")
```
%% Output
Please enter your name:FA
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-12-7cf67f3e360a> in <module>()
----> 1 name = input("Please enter your name:")
2
3 age = int(input("Hallo ", name, ". In welchem Jahr bist du geboren? "))
4
5 import datetime
/home/rgrosso/.local/lib/python2.7/site-packages/ipykernel/ipkernel.pyc in <lambda>(prompt)
162 self._sys_eval_input = builtin_mod.input
163 builtin_mod.raw_input = self.raw_input
--> 164 builtin_mod.input = lambda prompt='': eval(self.raw_input(prompt))
165 self._save_getpass = getpass.getpass
166 getpass.getpass = self.getpass
/home/rgrosso/.local/lib/python2.7/site-packages/ipykernel/ipkernel.pyc in <module>()
NameError: name 'FA' is not defined
%% Cell type:markdown id: tags:
### Aufgabe 2 a)
%% Cell type:code id: tags:
``` python
liste = []
for _ in range(10):
liste.append(int(input()))
gerade_liste = [x for x in liste if x%2==0 ]
str(gerade_liste)
```
%% Cell type:markdown id: tags:
### Aufgabe 2 b)
%% Cell type:code id: tags:
``` python
sentence = input("Please write your sentence:")
revsentence = ' '.join(sentence.split()[::-1])
print(revsentence)
```
%% Output
Please write your sentence:"a b c d e"
e d c b a
%% Cell type:markdown id: tags:
### Aufgabe 2 c)
%% Cell type:code id: tags:
``` python
sentence = input("Please write your sentence:")
alpha_ordered = sorted(sentence.split())
rev_alpha_ordered = ' '.join(alpha_ordered[::-1])
print(rev_alpha_ordered)
```
%% Output
Please write your sentence:"aa ab ba df fa el la id un nu"
un nu la id fa el df ba ab aa
%% Cell type:markdown id: tags:
### Aufgabe 2 d)
%% Cell type:code id: tags:
``` python
birthMonths = {"Einstein":3, "Hawking":1, "Newton":1, "Franklin":1, "Darwin":2, "Curie":11, "Pasteur":12,
"Copernicus":1, "Fleming":8, "Nobel":10, "Mendelev":2, "Bohr":10, "Mendel":7, "Faraday":9,
"Watson":4, "Hubble":11, "Feynman":5, "Rutherford":8}
month = input("Choose a name:")
print(birthMonths[month])
```
%% Output
Choose a name:"Nobel"
10
%% Cell type:markdown id: tags:
### Aufgabe 3 a)
%% Cell type:code id: tags:
``` python
import random
print("Rate eine Zahl zwischen 0 und 9. Schreibe \"exit\" um zu stoppen.")
for _ in range(10):
rndnum = random.randint(0,9)
usernum = (input("Rate mal: "))
if usernum == "exit" or usernum == "Exit":
print("Bis nächstes Mal ...")
break
try:
usernum = int(usernum)
except ValueError:
print(usernum + " ist keine Zahl!")
if usernum < 0 or usernum > 9:
print("Gültige Zahlen sind zwischen 0 und 9!")
elif rndnum == usernum:
print("************** Gut geraten! ************")
break
else:
print("Sorry, die Zahl war: " + str(rndnum))
print("Versuch nochmal!")
if _ == 9:
print("Zuviele Versuche! Bis nächstes Mal ...")
```
%% Output
Rate eine Zahl zwischen 0 und 9. Schreibe "exit" um zu stoppen.
Rate mal: 3
Sorry, die Zahl war: 2
Versuch nochmal!