LU12.L02 - Hofladen

def input_int(prompt):
    """
    reads an integer input from the user
    :param text: the prompt to be shown
    :return: the integer number
    """
    pass
 
def input_float(prompt):
    """
    reads a decimal number input from the user
    :param text: the prompt to be shown
    :return: the decimal number
    """
    pass
 
def find_article(list, name):
    """
    finds an article in the article list
    :param list: the article list
    :param name: the article name to be found
    :return: article or None=not found
    """
    pass
 
  • Schritt 1
  • Schritt 2
  • Schritt 3
  1. Erstelle eine leere Liste für die Artikel
  2. Eingabe des Artikelnamens
  3. Solange Artikelname nicht Exit ist
    1. Suche den Artikel in der Liste
    2. Falls keine Artikel gefunden wurde
      1. Erstelle ein neues Artikel-Objekt und speichere es in der Liste
      2. Speichere den Artikelnamen
      3. Eingabe des Preises
    3. Sonst
      1. Ausgabe des aktuellen Bestands
    4. Eingabe der Menge
    5. Addiere die Menge zum Bestand
    6. Eingabe des Artikelnamens
  4. Gib die Artikelliste als Returnwert zurück
article.py
from dataclasses import dataclass
 
 
@dataclass
class Article:
    """
    an article in the farmshop
    """
    name: str
    price: float
    stock: int
 
 
if __name__ == '__main__':
    pass
shop.py
from article import Article
 
 
def main():
    article_list = []
    article_name = input('Artikelname > ')
    while article_name != 'Exit':
 
        article = find_article(article_list, article_name)
        if article is None:
            article = Article(article_name, 0.0, 0)
            article_list.append(article)
            article.price = input_float('Preis       > ')
        else:
            print('Bestand     : ' + str(article.stock))
 
        amount = input_int('Menge       > ')
        article.stock = (article.stock + amount)
        article_name = input('Artikelname > ')
 
    return article_list
 
 
def input_int(prompt):
    """
    reads an integer input from the user
    :param prompt: the prompt to be shown
    :return: the integer number
    """
    number = None
    while number is None:
        try:
            number = int(input(prompt))
        except ValueError:
            print("Please, enter a whole number!")
    return number
 
 
def input_float(prompt):
    """
    reads a decimal number input from the user
    :param prompt: the prompt to be shown
    :return: the decimal number
    """
    number = None
    while number is None:
        try:
            number = float(input(prompt))
        except ValueError:
            print("Please, enter a real number!")
    return number
 
 
def find_article(article_list, name):
    """
    finds an article in the article list
    :param article_list: the article list
    :param name: the article name to be found
    :return: article or None=not found
    """
    for article in article_list:
        if article.name == name:
            return article
    return None
 
 
if __name__ == '__main__':
    main()
  • modul/m319/learningunits/lu12/loesungen/hofladen.txt
  • Zuletzt geändert: 2024/03/28 14:07
  • von 127.0.0.1