# coding: utf-8
def Roman_units():
'''罗马数字单位'''
units = [
['M', 1000],
['CM', 900],
['D', 500],
['CD', 400],
['C', 100],
['XC', 90],
['L', 50],
['XL', 40],
['X', 10],
['IX', 9],
['V', 5],
['IV', 4],
['I', 1]
] #罗马数字单位列表。
print(f'\n\n\n{"罗马数字构造元素":_^34}\n\n\n')
print(f'{" ":8}{"字符":^10}|{"整数值":^10}\n')
for Roman_s, value in units:
print(f'{" ":12}{Roman_s:8}|{value:>8}\n')
input(f'\n\n{"The End":_^42}\n')
Roman_units()
while True:
try:
integer = int(input(f'\n\n{" ":4}输入整数:'))
if integer < 1 or integer > 3999:
print(f'\n\n\n{"请输入1~3999的整数!":.^35}\n\n\n')
else:
break
except Exception as error:
print(f'\n\n\n{error}\n{"输入错误!请重试。":.^33}\n\n')
# coding: utf-8
def integer_to_Roman(integer):
'''整数转罗马数字'''
units = [
['M', 1000],
['CM', 900],
['D', 500],
['CD', 400],
['C', 100],
['XC', 90],
['L', 50],
['XL', 40],
['X', 10],
['IX', 9],
['V', 5],
['IV', 4],
['I', 1]
] #罗马数字单位列表。
Roman = [] #初始化罗马数字字符列表。
k = 1
while integer > 0:
#print(f'\n\n整数:{integer}\n循环{k}次。\n\n{Roman}')
k += 1
for Roman_s, value in units:
if integer >= value:
Roman.append(Roman_s)
integer -= value
break
if integer < 1:
break
return ''.join(Roman)
print(f'\n\n\n{"整数转罗马数字":_^35}\n')
while True: #输入整数排错。
try:
integer = int(input(f'\n\n{" ":4}输入整数:'))
if integer < 1 or integer > 3999:
print(f'\n\n\n{"请输入1~3999的整数!":.^35}\n\n\n')
else:
break
except Exception as error:
print(f'\n\n\n{error}\n{"输入错误!请重试。":.^33}\n\n')
print(f'\n\n\n{" ":4}整数{integer}转罗马数字是:\
{integer_to_Roman(integer)}\n\n\n{"The End":_^42}\n')
# coding: utf-8
def show_Roman(n):
s = f"罗马数字(1~{n})"
print(f'\n\n{s:_^38}\n\n\n')
for i in range(1, n + 1):
print(f'{i:>3}:{integer_to_Roman(i):>8}', end = ' ')
if (i)%3 == 0: print()
print(f'\n\n\n{"The End":_^42}\n')
show_Roman(100)
# coding: utf-8
def integer_to_Roman(integer):
'''整数转罗马数字'''
units = [
['M', 1000],
['CM', 900],
['D', 500],
['CD', 400],
['C', 100],
['XC', 90],
['L', 50],
['XL', 40],
['X', 10],
['IX', 9],
['V', 5],
['IV', 4],
['I', 1]
] #罗马数字单位列表。
Roman = [] #初始化罗马数字字符列表。
k = 1
while integer > 0:
#print(f'\n\n整数:{integer}\n循环{k}次。\n\n{Roman}')
k += 1
for Roman_s, value in units:
if integer >= value:
Roman.append(Roman_s)
integer -= value
break
if integer < 1:
break
return ''.join(Roman)
print(f'\n\n\n{"整数转罗马数字":_^35}\n')
integer = int(input(f'\n\n{" ":4}输入整数:'))
print(f'\n\n\n{" ":4}整数{integer}转罗马数字是:\
{integer_to_Roman(integer)}\n\n\n{"The End":_^42}\n')
def show_Roman(n):
s = f"罗马数字(1~{n})"
print(f'\n\n{s:_^38}\n\n\n')
for i in range(1, n + 1):
print(f'{i:>3}:{integer_to_Roman(i):>8}', end = ' ')
if (i)%3 == 0: print()
print(f'\n\n\n{"The End":_^42}\n')
show_Roman(100)
input()
def Roman_units():
'''罗马数字单位'''
units = [
['M', 1000],
['CM', 900],
['D', 500],
['CD', 400],
['C', 100],
['XC', 90],
['L', 50],
['XL', 40],
['X', 10],
['IX', 9],
['V', 5],
['IV', 4],
['I', 1]
] #罗马数字单位列表。
print(f'\n\n\n{"罗马数字构造元素":_^34}\n\n\n')
print(f'{" ":8}{"字符":^10}|{"整数值":^10}\n')
for Roman_s, value in units:
print(f'{" ":12}{Roman_s:8}|{value:>8}\n')
input(f'\n\n{"The End":_^42}\n')
Roman_units()
上一篇: 练习:罗马数字转整数
下一篇:
来源:老齐教室