Python :: Aufgabe #310 :: Lösung #2

5 Lösungen Lösungen öffentlich
#310

Berechnung Kfz-Steuer (Deutschland 2021)

Anfänger - Python von JKooP - 15.12.2020 um 19:35 Uhr
Schreibe eine Methode/Funktion, mit der es möglich ist, die Kfz- Steuer sowohl für Benzin-, als auch Dieselfahrzeuge
ab dem 01.01.2021 für Deutschland zu berechnen.

Übergeben werden sollen die Parameter: Otto- o. Dieselfahrzeug, Hubraum in ccm, CO2 in g.

Als Ergebnis soll die Gesamtsteuer in Euro fürs Jahr ausgegeben werden.

Eine ausführliche Berechnungshilfe findet man hier!

Viel Spaß
#2
2 Kommentare
1x
vote_ok
von felixTheC (1200 Punkte) - 30.12.2020 um 20:45 Uhr
Quellcode ausblenden Python-Code
import math
from functools import lru_cache
from typing import Literal

# to prevent wrong parameters
# pip install strongtyping
from strongtyping.strong_typing import match_typing


# Config dict for price per started 100 cc
pp_ccm = {
    'Otto': 2.00,
    'Diesel': 9.50
}

# Config dict for co2 price in different sectors
price_co2 = {
    (96, 115): 2.00,
    (116, 135): 2.20,
    (136, 155): 2.50,
    (156, 175): 2.90,
    (176, 195): 3.40,
    (195, -1): 4.00,  # -1 used as no upper limit
}


@lru_cache
@match_typing
def calc_kfz_tax(kfz_type: Literal['Diesel', 'Otto'], ccm: int, co2_g: int) -> float:
    """
    :return: total tax for a year in Euro
    """
    price_cc = math.ceil(ccm / 100) * pp_ccm[kfz_type]

    if co2_g <= 95:
        total_tax = price_cc
    elif co2_g > 195:
        total_co2 = sum(((k[1] - k[0]) + 1) * v
                        if k[0] < 195
                        else
                        (co2_g - k[0]) * v
                        for k, v in price_co2.items())
        total_tax = price_cc + total_co2
    else:
        total_co2 = 0
        for k, v in price_co2.items():
            if co2_g <= k[1]:
                total_co2 += ((co2_g - k[0]) + 1) * v
                break
            elif k[1] < co2_g:
                total_co2 += ((k[1] - k[0]) + 1) * v
        total_tax = price_cc + total_co2

    return total_tax


Kommentare:

Gelöschte Person

Punkte: 0



2 Kommentare

#1
01.01.2021 um 21:24 Uhr
Wenn man einen negativen Hubraum eingibt, wird dafür ein negativer Steueranteil berechnet.
post_arrow
664 0

felixTheC

Punkte: 1200


23 Lösungen
4 Kommentare

#2
01.01.2021 um 22:06 Uhr
Danke, daran habe ich wahrlich nicht gedacht.
post_arrow
665 0
Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben