Dezimal- in Lateinzahlen
Verfasst: Dienstag 16. Dezember 2003, 18:18
Hallo,
Ich habe mal eine kleine Funktion geschrieben, um lateinische Zahlen in dezimale Zahlen zu konvertieren und umgekehrt.
Ich bitte um Begutachtung und evtl. Fehlerfindung.
Danke!
Ich habe mal eine kleine Funktion geschrieben, um lateinische Zahlen in dezimale Zahlen zu konvertieren und umgekehrt.
Ich bitte um Begutachtung und evtl. Fehlerfindung.
Danke!
Code: Alles auswählen
def DecLat(zahl):
convertzahl=zahl
if zahl != "" and zahl.isalpha() == 1:
lat=['CM', 'M', 'CD', 'D', 'XC', 'C', 'XL', 'L', 'IX', 'X', 'IV', 'V', 'I']
dez=[900, 1000, 400, 500, 90, 100, 40, 50, 9, 10, 4, 5, 1]
convertzahl=0
for i,j in zip(lat,dez):convertzahl += zahl.count(i)*j; zahl=zahl.replace(i,"")
convertzahl = str(convertzahl).zfill(4)
elif zahl != "" and zahl.isdigit() == 1:
lat=['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
dez=[1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
convertzahl=""
for i in range(100):
for i,j in zip(lat,dez):
if int(zahl)-j >= 0:
zahl=int(zahl)-j
convertzahl += i
break
if zahl == 0:break
convertzahl = str(convertzahl)
return convertzahl