def number2german( num):
"""Convert a number to a German string
Convert a number to a German string, as it would be
spoken aloud. Valid range: 0 - 99
"""
specials = {0:'null',1:'eins',11:'elf',12:u'zwölf',
16:'sechzehn',17:'siebzehn'}
tens = ['','zehn','undzwanzig',u'unddreißig','undvierzig',
u'undfünfzig','undsechzig','undsiebzig',
'undachtzig','undneunzig']
ones = ['null','ein','zwei','drei','vier',u'fünf',
'sechs','sieben','acht','neun']
if num in specials.keys():
return specials[num]
if num < 0 or num > 99:
return ''
if num % 10 == 0:
if tens[num/10][:3] == 'und':
return tens[int(num/10)][3:]
else:
return tens[int(num/10)]
return u'%s%s' % ( ones[num%10], tens[int(num/10)], )
This function takes a number in the range 0-99 and returns a string representation of it, as it would be spoken in German (the main audience of PERLI.NET is German-speaking). Now, I'm displaying the returned string (for example "einunddreißig") on the guestbook entry form and also include a hidden field that has the answer encoded (I'm using the simple method of multiplying the number by two, but you might chose a more sophisticated algorithm). I'm also displaying a text entry box where the user types in the number.
The guestbook entry is only saved when the number is keyed in correctly, if not, the entry is silently ignored. That should save you some guestbook-entry-deleting work and make your visitors happy! You can view the resulting guestbook entry form here.
No comments:
Post a Comment