def satuan(a):
if a == 0:
return "nol"
elif a == 1:
return "satu"
elif a == 2:
return "dua"
elif a == 3:
return "tiga"
elif a == 4:
return "empat"
elif a == 5:
return "lima"
elif a == 6:
return "enam"
elif a == 7:
return "tujuh"
elif a == 8:
return "delapan"
elif a == 9:
return "sembilan"
elif a == 10:
return "sepuluh"
elif a == 11:
return "sebelas"
def terbilang(a):
t = str(a)
te = []
for i in t:
te.append(int(i))
####
if a > 0 and a < 12:
h = satuan(a)
elif a > 11 and a < 20:
h = satuan(te[1])+" "+"belas"
elif a > 19 and a < 100:
if te[1] == 0:
h = satuan(te[0])+" puluh"
else:
h = satuan(te[0])+" puluh "+satuan(te[1])
elif a > 99 and a < 1000:
if te[0] == 1:
rat = "seratus"
else:
rat = satuan(te[0])+" ratus"
if a%100 == 0:
h = rat
elif te[1] == 0:
h = rat+" "+satuan(te[2])
elif te[1] == 1:
if te[2] == 0 or te[2] == 1:
h = rat+" "+satuan(int(str(te[1])+str(te[2])))
else:
h = rat+" "+satuan(te[2])+" belas"
elif te[1] > 1:
pul = satuan(te[1])+" puluh"
if te[2] == 0:
h = rat+" "+pul
else:
h = rat+" "+pul+" "+satuan(te[2])
return h
==
jalankan program python terbilang diatas dengan menggunakan python versi 3.
berikut contoh untuk menjalankan programnya:
terbilang(120)
maka outputannya : seratus dua puluh
==