Skip to content

Instantly share code, notes, and snippets.

@christiancost47
Created May 11, 2020 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christiancost47/7e0949165a474e101a70bbe71609c001 to your computer and use it in GitHub Desktop.
Save christiancost47/7e0949165a474e101a70bbe71609c001 to your computer and use it in GitHub Desktop.
Roman Number converter
def Romanos(lista):
valores = { "0": 0, "I":1 , "V":5, "X":10, "L":50,"C":100,"M":1000 }
contador_geral = 0
minimo = "0"
parcial = 0
try: #BLOCO QUE PODE CAUSAR EXCEÇÃO
for algarismo in lista:
if (algarismo == minimo):
parcial += valores[algarismo]
else:
if (valores[algarismo]>valores[minimo]):
contador_geral -= parcial
else:
contador_geral += parcial
parcial = valores[algarismo]
minimo = algarismo
contador_geral += parcial
return contador_geral
except KeyError: #BLOCO PARA TRATAR A EXCEÇÃO
print("Valor inválido!")
while True:
resposta = str(input("Deseja digitar um novo número romano [s/n]?: "))
if (resposta == "s"):
algarismo = str(input("Digite um número romano: "))
if (algarismo == ""):
break
print(Romanos(algarismo))
if (resposta == "n"):
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment