GIDForums  

Go Back   GIDForums > Computer Programming Forums > Python Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Apr-2009, 17:12
marcoshid marcoshid is offline
New Member
 
Join Date: Apr 2009
Posts: 3
marcoshid is on a distinguished road

Roman numeral to decimal


I have a project in which I have to convert roman numerals to arabic characters. I can only get it to read the numeral on the right and it won't loop anyone have any way of helping me fix this.


Python Code:
##Roman Numeral to Arabic form Conveter


##Problem:
##Trying to convert Roman Numerals(between 1 and 3,999) into Arabic form.


##Requirements:
##1. Should only convert between the numbers 1 - 3,999.
##2. All input should be read from the keyboard.
##3. All output should be printed to the screen.
##4. Should only accept valid Roman Numerals.
##5. Invalid data shpuld give appropiate error message and not print out the converted value
##6. If there are multiple errors only the first error should be reported.


##Algorithm:
##1. set the initial variables
##2. start with the last numeral by getting the length of the roman numeral minus 1
##3. set i to start the roman numeral at the last value
##4.If the roman numeral is equal to "M" and "D" and "C" and "L" and "X" and "V" and "I" repeat the following:
##    - Prompt the user to enter the roman numeral they which to have converted to Arabic. Make the input into all capitals
##    - start with the last numeral by getting the length of the roman numeral minus 1. Set i to start the roman numeral at the last value
##    - if i is equal to "M" then the now value is equal to 1000
##    - if i is equal to "D" then the now value is equal to 500
##    - if i is equal to "C" then the now value is equal to 100
##    - if i is equal to "L" then the now value is equal to 50
##    - if i is equal to "X" then the now value is equal to 10
##    - if i is equal to "V" then the now value is equal to 5
##    - if i is equal to "I" then the now value is equal to 1
##5. If the previous value is greater than or equal to the now value, subtract the now from the previous value
##6. If the previous value is less than the now value add the previous value and the now value
##7. Print the roman numeral and the arabic value to the screen




def Roman_numeral_to_Arabic_form ():

    import string


    print"Welcome to the Roman Numeral to Arabic Form Converter.\n"


    roman_numeral = "I"
    previous = 0
    now = 0
    total = 0

    length = len(roman_numeral) - 1
    i = roman_numeral[length]
               
    if roman_numeral == ("M" and "D" and "C" and "L" and "X" and "V" and "I") :

        roman_numeral = string.upper(raw_input("Enter the Roman Numeral to convert to Arabic: "))
        length = len(roman_numeral) - 1
        i = roman_numeral[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 - previous
        print"The roman numeral",roman_numeral," is equivalent to",total,"\n"
        
    if previous < now:
        total += now + previous
        print"The roman numeral",roman_numeral," is equivalent to",total,"\n"

    
      

   

Roman_numeral_to_Arabic_form ()
 

Thanks
Last edited by admin : 03-Apr-2009 at 17:32. Reason: Please insert your example Python codes between [PY] and [/PY] tags
  #2  
Old 03-Apr-2009, 23:20
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,628
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Roman numeral to decimal


It's not looping because you don't have any loop statements in it. All you have are ifs, which are only processed one time unless enclosed within a loop.

You also have length and i being created and assigned values twice. Did you mean to do this on purpose?

I haven't ran the code but fixing the loop is the biggest thing I found. Without a loop, the program will run once then quit. This is probably why it only does the rightmost numeral.

You may want to slice the Roman number and add up the values that way. Looping through the number, getting a different slice value each time, may be easier to work with.

Just a suggestion.
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
  #3  
Old 04-Apr-2009, 11:55
marcoshid marcoshid is offline
New Member
 
Join Date: Apr 2009
Posts: 3
marcoshid is on a distinguished road

Re: Roman numeral to decimal


I hadn't put the while loop bacause I'm trying to get it to work correctly first before putting the loop in it. Yes I did mean to have the lenght twice, should I not have it, would there be any problems in getting the code to run properly like that. How do I do the slicing part and how do I get it to add the old value ot the new value like:

IV <--- how do I get it to subtract 1 from 5

VI <--- how do I get it to add 1 to 5


Thanks,
I'm pretty new and still trying to learn how to program
  #4  
Old 04-Apr-2009, 17:26
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,628
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Roman numeral to decimal


Unless you have to write your program using if statements, I would recommend using regular expressions. You can write a reasonably short regex expression that will parse through the Roman number and give you the values of each number.

Take a look at Dive Into Python; it has a chapter talking about regular expressions and it also talks about Roman numeral conversion.
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
  #5  
Old 05-Apr-2009, 14:56
marcoshid marcoshid is offline
New Member
 
Join Date: Apr 2009
Posts: 3
marcoshid is on a distinguished road

Re: Roman numeral to decimal


Well this is what I've done so far. I looked at he dive into python book but I got lost and have no clue what to do. I tried indexing it the comments are in there but i couldn't get it work. With the way it is now it at least gives me some basic values like 1 , 5 etc but that's about it.


These are the rules I must follow:
The valid letters and rules for creating and
reading valid Roman Numerals (up to 3999) are as follows:
I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1000
1) A letter repeats its value that many times (XXX = 30, CC = 200, etc.). A letter can only be
repeated three times.
2) If one or more letters are placed after another letter of greater value, add that amount.
Examples:
a. VI = 6 (5 + 1 = 6)
b. LXX = 70 (50 + 10 + 10 = 70)
c. MCC = 1200 (1000 + 100 + 100 = 1200)
3) If a letter is placed before a letter of greater values, subtract that amount.
Further Rules:
a. Subtract only powers of ten, such as I, X, or C. Writing VL for 45 is not allowed: write
XLV instead.
b. Subtract only a single letter from a single numeral. Write VIII for 8, not IIX; 19 is XIX,
not IXX.
c. Don't subtract a letter from another letter more than ten times greater. This means
that you can only subtract I from V or X, and X from L or C, so MIM is illegal.

Python Code:
##Problem:
##Trying to convert Roman Numerals(between 1 and 3,999) into Arabic form.


##Requirements:
##1. Should only convert between the numbers 1 - 3,999.
##2. All input should be read from the keyboard.
##3. All output should be printed to the screen.
##4. Should only accept valid Roman Numerals.
##5. Invalid data shpuld give appropiate error message and not print out the converted value
##6. If there are multiple errors only the first error should be reported.


##Algorithm:
##1. set the initial variables
##2. start with the last numeral by getting the length of the roman numeral minus 1
##4.If the roman numeral is equal to "M" and "D" and "C" and "L" and "X" and "V" and "I" repeat the following:
##    - Prompt the user to enter the roman numeral they which to have converted to Arabic. Make the input into all capitals
##    - start with the last numeral by getting the length of the roman numeral minus 1. Set i to start the roman numeral at the last value
##    - if i is equal to "M" then the now value is equal to 1000
##    - if i is equal to "D" then the now value is equal to 500
##    - if i is equal to "C" then the now value is equal to 100
##    - if i is equal to "L" then the now value is equal to 50
##    - if i is equal to "X" then the now value is equal to 10
##    - if i is equal to "V" then the now value is equal to 5
##    - if i is equal to "I" then the now value is equal to 1
##5. If the previous value is greater than or equal to the now value, subtract the now from the previous value
##6. If the previous value is less than the now value add the previous value and the now value
##7. Print the roman numeral and the arabic value to the screen




def Roman_numeral_to_Arabic_form ():

    import string


    print"Welcome to the Roman Numeral to Arabic Form Converter.\n"

##1. set the initial variables
    roman_numeral = "I"
    previous = 0
    now = 0
    total = 0

##2. start with the last numeral by getting the length of the roman numeral minus 1

    length = len(roman_numeral) - 1
   

##4.If the roman numeral is equal to "M" and "D" and "C" and "L" and "X" and "V" and "I" repeat the following:    
    while roman_numeral == ("M" and "D" and "C" and "L" and "X" and "V" and "I") :

##    - Prompt the user to enter the roman numeral they which to have converted to Arabic. Make the input into all capitals
        roman_numeral = string.upper(raw_input("Enter the Roman Numeral to convert to Arabic: "))

##    - start with the last numeral by getting the length of the roman numeral minus 1. Set i to start the roman numeral at the last value        
        length = len(roman_numeral) - 1


##        roman_numeral = ["I","V","X","L","C","D","M"]
##        arabic_number = ["1", "5", "10", 50, 100, 500, 1000]
##        if roman_numeral[0]:
##            roman_numeral[0] = arabic_number[0]
##
##        if roman_numeral[1]:
##            arabic_number[1] = arabic_number[1]
##
##        if roman_numeral[2]:
##            arabic_number[2] = arabic_number[2]
##
##        if roman_numeral[3]:
##            arabic_number[3] = arabic_number[3]
##
##        if roman_numeral[4]:
##            arabic_number[4] = arabic_number[4]
##
##        if roman_numeral[5]:
##            arabic_number[5] = arabic_number[5]
##
##        if roman_numeral[6]:
##            arabic_number[6] = arabic_number[6]

                    





##    - if i is equal to "M" then the arabic number value is equal to 1000
        if roman_numeral[:]:
            if roman_numeral == "M":
                arabic_number = 1000

##    - if i is equal to "D" then the arabic number value is equal to 500
            if roman_numeral == "D":
                arabic_number = 500

##    - if i is equal to "C" then the arabic number value is equal to 100
            if roman_numeral == "C":
                arabic_number = 100

##    - if i is equal to "L" then the arabic number value is equal to 50
            if roman_numeral == "L":
                arabic_number = 50

##    - if i is equal to "XL" then the arabic value is equal to 40      
            if roman_numeral == "XL":
                arabic_number = 40

##    - if i is equal to "IX" then the arabic value is equal to 10
            if roman_numeral == "X":
                arabic_number = 10

##    - if i is equal to "I" then the arabic value is equal to 9      
            if roman_numeral == "IX":
                arabic_number = 9

##    - if i is equal to "V" then the arabic number value is equal to 5
            if roman_numeral == "V":
                arabic_number = 5

##    - if i is equal to "IV" then the arabic value is equal to 4      
            if roman_numeral == "IV":
                arabic_number = 4

##    - if i is equal to "I" then the arabic value is equal to 1      
            if roman_numeral == "I":
                arabic_number = 1


##5. If the previous value is greater than or equal to the now value, subtract the now from the previous value
        if previous >= arabic_number:
            total -= arabic_number - previous

##7. Print the roman numeral and the arabic value to the screen
            print"The roman numeral",roman_numeral," is equivalent to",total,"\n"

##6. If the previous value is less than the now value add the previous value and the now value
        if previous < arabic_number:
            total = arabic_number + previous

##7. Print the roman numeral and the arabic value to the screen
            print"The roman numeral",roman_numeral," is equivalent to",total,"\n"

    
        roman_numeral = string.upper(raw_input("Enter the Roman Numeral to convert to Arabic: "))
        
        ##5. If the previous value is greater than or equal to the now value, subtract the now from the previous value
        if previous >= arabic_number:
            total -= arabic_number - previous

##7. Print the roman numeral and the arabic value to the screen
            print"The roman numeral",roman_numeral," is equivalent to",total,"\n"

##6. If the previous value is less than the now value add the previous value and the now value
        if previous < arabic_number:
            total = arabic_number + previous

##7. Print the roman numeral and the arabic value to the screen
            print"The roman numeral",roman_numeral," is equivalent to",total,"\n"
          

   

Roman_numeral_to_Arabic_form ()




Thanks
Last edited by LuciWiz : 05-Apr-2009 at 15:11. Reason: Please insert your Python code between [py] & [/py] tags
  #6  
Old 05-Apr-2009, 18:47
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,628
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Roman numeral to decimal


Well, you need to read in the whole value, instead of just looking at the rightmost. If you just start with the rightmost number, you won't know if it's part of a larger value, e.g. VI instead of just I.

Also, starting from the left would make more sense because Roman numbers subtract from the left, e.g. IV =4. Starting from the right won't catch those cases.

You should consider making a mapping of valid roman numerals to their respective decimal values, like this:
Python Code:
romanNumeralMap = (('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))
That should greatly help you when looking for valid combinations. Dive Into Python has a section using this idea.

I have a suspicion that, continuing on with your methodology, it would devolve into creating an if statement for every single possible combination.
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Another Roman numeral conversion problem. hoodedpython C++ Forum 19 10-Dec-2007 02:18
Roman numerals to decimal ShamanKingCpp C++ Forum 8 14-Dec-2006 07:25
help writing a roman numeral to decimal program Ichigo C++ Forum 3 23-Mar-2006 15:05
Frustration...Roman Numeral Program SpyD3r C++ Forum 14 13-Nov-2005 21:02
Roman to decimal to roman SpudNuts C++ Forum 2 16-Feb-2005 20:44

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:21.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.