Skip to content

Instantly share code, notes, and snippets.

@needToRoll
Last active January 24, 2022 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save needToRoll/ce9f0f6bf3496926767e0f250b8cfc0d to your computer and use it in GitHub Desktop.
Save needToRoll/ce9f0f6bf3496926767e0f250b8cfc0d to your computer and use it in GitHub Desktop.
Python learning example for teaching eipr
day = 153
month = 17
year = 2023
## Funktioniert für jeden Input
def structureDate(day, month, year):
if len(str(day)) == 1:
day = "0"+str(day)
# Das gehört eigentlich global deklariert, das lässt das Beispiel aber nicht zu.
lengthOfMonth = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
while month > 12:
month = month - 12
year = year + 1
while int(day) > lengthOfMonth.get(month):
day = int(day) - lengthOfMonth.get(month)
month = month + 1
while month > 12:
month = month - 12
year = year + 1
if len(str(day)) == 1:
day = "0" + str(day)
if len(str(month)) == 1:
month = "0"+str(month)
return str(day) + "." + str(month) + "." + str(year)
daysToAdd = 7941 # Achtung: Anzahl Tage plus 1
for i in range(1, daysToAdd):
currentDay = day + i
print(structureDate(currentDay, month, year))
day = 16
month = 6
year = 2020
# Zusatzfrage: In welchen Fällen gibt diese implementation falsche Ergebnisse zurück
def structureDate(day, month, year):
if len(str(day)) == 1:
day = "0"+str(day)
if int(day) > 30:
day = int(day) - 30
month = month + 1
if len(str(day)) == 1:
day = "0"+str(day)
if len(str(month)) == 1:
month = "0"+str(month)
return str(day) + "." + str(month) + "." + str(year)
daysToAdd = 21 # Achtung: Anzahl Tage plus 1
for i in range(1, daysToAdd):
currentDay = day + i
print(structureDate(currentDay, month, year))
# ## Output:
# 17.06.2020
# 18.06.2020
# 19.06.2020
# 20.06.2020
# 21.06.2020
# 22.06.2020
# 23.06.2020
# 24.06.2020
# 25.06.2020
# 26.06.2020
# 27.06.2020
# 28.06.2020
# 29.06.2020
# 30.06.2020
# 01.07.2020
# 02.07.2020
# 03.07.2020
# 04.07.2020
# 05.07.2020
# 06.07.2020
workdays = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag"]
weekend = ["Samstag", "Sonntag"]
lineBrake = "\n"
exitWord = "Beenden"
while True:
userInput = input("Bitte geben Sie einen Wochentag ein: " + lineBrake + "oder '" + exitWord + "' um die Eingabe abzubrechen" + lineBrake)
if userInput == exitWord:
break
if userInput in workdays:
print(userInput + " ist ein Arbeitstag")
elif userInput in weekend:
print(userInput + " ist Teil des Wochenendes")
else:
print(userInput + "wurde nicht als Wochentag erkannt!" + lineBrake + "(Achten Sie auf Gross-/ Kleinschreibung)")
# ## Output
# Montag
# Montag ist ein Arbeitstag
# Bitte geben Sie einen Wochentag ein:
# oder 'Beenden' um die Eingabe abzubrechen
# sAmsTag
# sAmsTagwurde nicht als Wochentag erkannt!
# (Achten Sie auf Gross-/ Kleinschreibung)
# Bitte geben Sie einen Wochentag ein:
# oder 'Beenden' um die Eingabe abzubrechen
# Samstag
# Samstag ist Teil des Wochenendes
# Bitte geben Sie einen Wochentag ein:
# oder 'Beenden' um die Eingabe abzubrechen
# Beenden
import random
veggies = ["Pea", "Carrot", "Salad", "Celery"]
fruits = ["Apple", "Orange", "Banana", "Pineapple"]
def returnFoodType(foodItem):
# In kann verwendet werdem um in Listen zu suchen. Rückgabewert: boolean (true/false)
if foodItem in veggies:
# Return: Python Keyword, bedeutet das die Funktion nicht nur etwas tut sondern ein Wert zurück gibt
return "vegetable"
# elif (gelsesen: else if) nur nach if zulässig, nur wenn vorheriges if falsch aber diese Bedingung wahr ist
# wird der Codeblock ausgeführt
elif foodItem in fruits :
return "fruit"
else:
return "unknown"
foodList = veggies + fruits
random.shuffle(foodList)
# len(liste) gibt die Anzahl Elemente zurück
# In diesem Beispiel gilt: len(foodList) == 8 and len(veggies) == 4
for foodIndex in range(len(foodList)):
currentFoodItem = foodList[foodIndex]
currentFoodType = returnFoodType(currentFoodItem)
print(currentFoodItem + " is of type: " + currentFoodType)
# ### Prints the following statements in random order: ###
# Orange is of type: fruit
# Carrot is of type: vegetable
# Apple is of type: fruit
# Salad is of type: vegetable
# Celery is of type: vegetable
# Banana is of type: fruit
# Pineapple is of type: fruit
# Pea is of type: vegetable
athletes = ["Roger Federer", "Stephan Liechtensteiner", "Kobe Bryant", "Matthias Zbinden", "Tiger Woods", "Beat Feuz"]
sports = ["Tennis Spieler", "Fussballer", "Baskettball Spieler", "Python-GM", "Golfer", "Ski Fahrer"]
def printCombination(name, sport):
print(name + " ist ein: " + sport)
for i in range(len(athletes)):
currentSport = sports[i]
currentAthlete = athletes[i]
printCombination(currentAthlete, currentSport)
def berechneZinsen(a, b, c):
k0 = 1345500
p = 0.25
n = [a, b, c]
for i in range(3):
d = n[i]
print(k0*(p/100+1)**d)
## print("Zinssatz nach " + str(d) + " Jahre ist: " + str(k0*(p/100+1)**d))
berechneZinsen(5, 10, 15)
kontostand = 1345500
zinsatz = 0.25
def berechneZinsesZins(k0, p, n):
return k0 * (p/100+1)**n
print(berechneZinsesZins(kontostand, zinsatz, 5))
print(berechneZinsesZins(kontostand, zinsatz, 10))
print(berechneZinsesZins(kontostand, zinsatz, 15))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment