|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Nov 2005
Città: TO
Messaggi: 5206
|
[Python] Primo programmino
Ho realizzato il mio primo programmino in Python. Serve per convertire un numero dalla numerazione decimale alla numerazione romana.
Codice:
#! /usr/bin/env python class BadNumberException(Exception): def __init__ (self, value): self.value = value def __str__ (self): return repr (self.value) def format_roman_digit (digit, roman1, roman5, roman10): if digit >= 1 and digit <= 3: return roman1 * digit elif digit == 4: return roman1 + roman5 elif digit >= 5 and digit <= 8: return roman5 + roman1 * (digit-5) elif digit == 9: return roman1 + roman10 else: return "" def roman_number (num): if num < 1 or num > 3999: raise BadNumberException ("bad number " + str(num)) roman = format_roman_digit (num/1000, "M", "", "") num %= 1000 roman += format_roman_digit (num/100, "C", "D", "M") num %= 100 roman += format_roman_digit (num/10, "X", "L", "C") num %= 10 roman += format_roman_digit (num, "I", "V", "X") return roman if __name__ == '__main__': import sys if len (sys.argv) == 2: strnum = sys.argv[1] try: r = roman_number (int (strnum)) print "roman =", r except ValueError: print "invalid input: " + strnum except BadNumberException, err: print err Grazie. ![]()
__________________
Andrea, SCJP 5 (91%) - SCWCD 5 (94%) |
![]() |
![]() |
![]() |
#2 | |
Senior Member
Iscritto dal: Nov 2005
Città: TO
Messaggi: 5206
|
Quote:
![]() ![]()
__________________
Andrea, SCJP 5 (91%) - SCWCD 5 (94%) |
|
![]() |
![]() |
![]() |
#3 | |
Senior Member
Iscritto dal: Dec 2005
Messaggi: 611
|
Quote:
|
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 11:53.