[Tutor] roman to arabic

Walter Prins wprins at gmail.com
Mon Feb 27 01:09:29 CET 2012

Hi ,

On 26 February 2012 23:29, Sukhpreet Sdhu <sukhpreet2294sidhu at ymail.com> wrote:
> Hi
> I just wrote python code to convert roman to arabic numerals, but its not working.
How exactly is it not working?  Please don't make us guess or work
more than neccesary trying to help you...

> Can you just check where the problem is and way to correct that.
> So here is my python code
> import string
> print "Welcome to the numeric conversion program"
> print "Please enter command"
> data=raw_input()
> now = 0
> previous = 0
> total = 0
> if data == "r":
>     print "Enter roman numeric to convert in arabic"
>     roman_numeric=string.swapcase(raw_input("Enter the Roman Numeral to convert to arabic"))
>  if roman_numeric == ("M" or "D" or "L" or "C" or "L" or "X" or "V" or "I"):
>      Length = len(roman_numeric) - 1
>      i = roman_numeric[Length]
>      if i == "M":
>          now = 1000
>          if i == "D":
>              now = 500
>              if i == "C":
>                  now = 100
>                  if i == "L":
>                      now = 50
>                      if i == "X":
>                          now = 10
>                          if i == "V":
>                              now = 5
>                              if i == "I":
>                                  now = 1
>                                  acc = now
>                                  if (previous >= now):
>                                      total += acc-prvious
>                                      print "The total is",total
>                                      if (previous <= now):
>                                          total += acc-prevous
>                                          print "The total is",total
>                                          else :
>                                              if data == "a" :
>                                                  print "Arabic number to convert"
>        thanks
> sukhpreet sidhu

Is your code really indented like that?  A quote worth mentioning here
is:  "If you need more than 3 levels of indentation, you're screwed
anyway, and should fix your program." --  Linus Torvalds

Now he was writing w.r.t. C/C++ but the principle holds for Python
also in general -- very highly nested levels of indentation are
indicative of some kind of program problem, and will likely cause you
to conceptually lose control of what the code's supposed to be doing.

Can you please explain in english (pseudocode) your algorithm for
converting a roman numeral string to arabic numbers, with a more
direct explanation of what you've tried and how your solution is not
working from what you're expecting.  Then we'll be able to help you
better and will not be left having to guess at how

All that said, apart from the indentation weirdness, a few more
offhand observations: I can see 3 different spellings for "previous"
in the code, which will obviously cause problems.  The logic to deal
with smaller numbers preceding larger numbers seem broken (though I've
not looked too closely).  The conditions both include equality (>= and
<=) which is likely wrong, the indentation is wrong and both
conditions seem to be doing the same thing, which must likewise be
wrong.  (You'd expect there to be some difference in handling the case
when the previous number is smaller that the current number vs when
it's larger...)



More information about the Tutor mailing list