-2

I'm writing a roman numeral to integers program and was testing some preexisting code with a few modifications I made.

list1={'I':1,'IV':4,'V':5,'IX':9,'X':10,'XL':40,'L':50,'XC':90,'C':100,'CD':400,'D':500,'CM':900,'M':1000}
def romanint(str):
    result=0
    count=0
    while (count < len(str)):
      value1 = list1[str[count]]
      if (count + 1 < len(str)):
        value2 = list1[str[count + 1]]
        if (value1 >= value2):
          result = result + value1
          count = count + 1
        else:
          result = result + value2 - value1
          count = count + 2
      else:
        result = result + value1
        count = count + 1
    return result
x=input("Please enter a Roman numeral: ")
print(romanint(x))

It works fine but I feel like there's a way to shorten it. I've tried to delete lines I've felt were unnecessary but errors always pop up. Is there a way to modify it or is it fine the way it is?

1 Answers1

-1

Check out the roman library, pip install roman the info is here on their gitgub. Assuming that you are simply trying to optimize a roman numeral converter.

Also, check twitter, you'll find help there with regards to recommendations for help on your code. I'm learning that StackOverflow has a set of rules that discourages asking for help, on a site built around... asking for help..

  • @mozway Yeah i'm learning that, slowly, I shouldnt have said it that way. – Joseph A. M. Nov 28 '22 at 12:49
  • @mozway Minimizing code and looking for alternative implementations without a review of the code provided are not things CR deals with, so please do not recommend that. – Mast Nov 29 '22 at 03:44