-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrencyCalculator.py
More file actions
executable file
·67 lines (48 loc) · 2.01 KB
/
currencyCalculator.py
File metadata and controls
executable file
·67 lines (48 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
"""
Währungsrechner mit Kommandozeilen-Oberfläche.
@author: Christian Wichmann
"""
import sys
def currency_calculator():
"""Gibt Benutzermeldungen aus und errechnet anschließend das Ergebnis."""
# Lese Ausgangsbetrag ein
print("===== "+app_name+" =====")
print("Bitte umzurechnenden Betrag in einer der möglichen Währungen eingeben: " + ", ".join(currencies))
source_input = str(input("Betrag: ")).upper()
# Finde mögliche Zielwährungen
for currency in currencies:
if currency in source_input:
source_currency = currency
break
available_destination_currencies = [c for c in currencies if c is not source_currency]
# Zielwährung abfragen
print("Bitte eine der möglichen Zielwährungen eingeben: " + ", ".join(available_destination_currencies))
dest_input = str(input("Zielwährung: ")).upper()
# Setze Zielwährung und berechne Umrechnungsfaktor
for currency in currencies:
if currency in dest_input:
dest_currency = currency
break
# Berechne Zielbetrag
source_value = float(source_input[:source_input.find(source_currency)])
dest_value = source_value * calculateFactor(source_currency, dest_currency)
# Gib Zielbetrag in Zielwährung aus
print("Zielbetrag: " + "%0.2f" % dest_value + " " + dest_currency)
def calculateFactor(source_currency, destination_currency):
"""Berechnet den Umrechnungsfaktor für die ausgewählten Währungen"""
factor = 1
# zuerst Umrechnung in EUR
factor *= factors[source_currency]
# dann Umrechnung in Zielwährung
factor /= factors[destination_currency]
return factor
if __name__ == "__main__":
# Konstanten für das Programm
app_name = "Währungsrechner"
# Liste mit allen Währungen und Umrechnungsfaktoren in EUR
currencies = ("EUR", "USD", "YEN")
factors = {"EUR": 1,
"USD": 0.751540658,
"YEN": 0.00774763265}
currency_calculator()