[Corso Python3] Ecco, così funziona... (è quello del sito, molto bello. Il programma intendo)
alessandro medici
alexxandro.medici a gmail.com
Mar 31 Maggio 2016 18:23:13 CEST
def int_to_roman(input):
""" Convert an integer to a Roman numeral. """
#if not isinstance(input, type(1)):
#raise TypeError, "expected integer, got %s" % type(input)
#if not 0 < input < 4000:
#raise ValueError, "Argument must be between 1 and 3999"
ints = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)
nums = ('M', 'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I')
result = []
for i in range(len(ints)):
count = int(input / ints[i])
result.append(nums[i] * count)
input -= ints[i] * count
return ''.join(result)
def roman_to_int(input):
""" Convert a Roman numeral to an integer. """
#if not isinstance(input, type("")):
#raise TypeError, "expected string, got %s" % type(input)
input = input.upper( )
nums = {'M':1000, 'D':500, 'C':100, 'L':50, 'X':10, 'V':5, 'I':1}
sum = 0
for i in range(len(input)):
try:
value = nums[input[i]]
# If the next place holds a larger number, this value is
negative
if i+1 < len(input) and nums[input[i+1]] > value:
sum -= value
else: sum += value
except KeyError:
pass
# easiest test for validity...
print('vado a romano con ', sum)
if int_to_roman(sum) == input:
print(sum)
else:
print('errore')
while True:
a = input('Romano:')
if a == 'e':
break
roman_to_int(a)
-------------- parte successiva --------------
Un allegato HTML è stato rimosso...
URL: <http://lists.fsugpadova.org/pipermail/fsug-corso-python3/attachments/20160531/fffcb112/attachment.html>
Maggiori informazioni sulla lista
fsug-corso-python3