Re: Kochbuch
Verfasst: Mittwoch 7. Oktober 2015, 09:47
@all: Vielen Dank habs geschafft nur noch löschen ist das Problem... Könntet ihr mir nochmal helfen
?

Seit 2002 Diskussionen rund um die Programmiersprache Python
https://www.python-forum.de/
Code: Alles auswählen
from collections import defaultdict as DefaultDict
import json
def remove(recipes):
if recipes:
category = get_category(
"Aus welcher Kategorie sollen Rezepte gelöscht werden? ",recipes
)
for recipe in recipes[category]:
print "\n{}\n***\n"
else:
print "Keine Rezepte vorhanden!\n"
recipes[category].remove(raw_input("Welches Rezept willst du löschen?"))
def load():
try:
with open("recipes.txt", "w") as file_:
recipes = DefaultDict(list, json.load(file_))
except IOError:
recipes = DefaultDict(list)
return recipes
def save(recipes):
with open ("recipes.txt", "w") as file_:
json.dump(recipes, file_)
def get_category(prompt, recipes):
categories = recipes.keys()
for number, name in enumerate(categories):
print "{:2d}: {}".format(number, name)
category = raw_input(prompt)
try:
category = categories[int(category)]
except (ValueError, IndexError):
pass
return category
def add(recipes):
try:
recipe = raw_input("Ihr Rezepttext(Ctrl-C beendet):")
except KeyboardInterrupt:
return
category = get_category(
"In welche Kategorie soll das Rezept hizugefügt werden? ",recipes
)
recipes[category].append(recipe)
def show(recipes):
if recipes:
category = get_category(
"Aus welcher Kategorie sollen Rezepte gezeigt werden? ", recipes
)
for recipe in recipes[category]:
print "\n{}\n***\n".format(recipe)
else:
print "Keine Rezepte vorhanden!\n"
def run():
menu = [
("Neues Rezept", add),
("Zeige Rezepte", show)
("Lösche Rezept", remove)
]
recipes = load()
while True:
print
for item_number, item in enumerate(menu):
print "{:2d}: {}".format(item_number, item[0])
try:
choice = int(raw_input("Ihre Wahl (Ctrl-C beendet): "))
except (ValueError, IndexError):
print "Bitte gültige Nummer eingeben...",
except KeyboardInterrupt:
break
print
menu[choice][2](recipes)
save(recipes)
run()
Code: Alles auswählen
from collections import defaultdict as DefaultDict
import json
def remove(recipes):
if recipes:
category = get_category(
"Aus welcher Kategorie sollen Rezepte gelöscht werden? ",recipes
)
for recipe in recipes[category]:
print "\n{}\n***\n"
else:
print "Keine Rezepte vorhanden!\n"
recipes[category].remove(raw_input("Welches Rezept willst du löschen?"))
def load():
try:
with open("recipes.txt", "w") as file_:
recipes = DefaultDict(list, json.load(file_))
except IOError:
recipes = DefaultDict(list)
return recipes
def save(recipes):
with open ("recipes.txt", "w") as file_:
json.dump(recipes, file_)
def get_category(prompt, recipes):
categories = recipes.keys()
for number, name in enumerate(categories):
print "{:2d}: {}".format(number, name)
category = raw_input(prompt)
try:
category = categories[int(category)]
except (ValueError, IndexError):
pass
return category
def add(recipes):
try:
recipe = raw_input("Ihr Rezepttext(Ctrl-C beendet):")
except KeyboardInterrupt:
return
category = get_category(
"In welche Kategorie soll das Rezept hizugefügt werden? ",recipes
)
recipes[category].append(recipe)
def show(recipes):
if recipes:
category = get_category(
"Aus welcher Kategorie sollen Rezepte gezeigt werden? ", recipes
)
for recipe in recipes[category]:
print "\n{}\n***\n".format(recipe)
else:
print "Keine Rezepte vorhanden!\n"
def run():
menu = [
("Neues Rezept", add),
("Zeige Rezepte", show),
("Lösche Rezept", remove)
]
recipes = load()
while True:
print
for item_number, item in enumerate(menu):
print "{:2d}: {}".format(item_number, item[0])
try:
choice = int(raw_input("Ihre Wahl (Ctrl-C beendet): "))
except (ValueError, IndexError):
print "Bitte gültige Nummer eingeben...",
except KeyboardInterrupt:
break
print
menu[choice][2](recipes)
save(recipes)
run()
Code: Alles auswählen
>>> (1, 2, 3, 4, 5)[0]
1
>>> (1, 2, 3, 4, 5)[4]
5
>>> (1, 2, 3, 4, 5)[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
Code: Alles auswählen
menu = [
('Neues Rezept', add),
('Zeige Rezepte', show),
('Lösche Rezept', remove)
]
Code: Alles auswählen
menu[choice][2](recipes)