| | |
***Help! Code not working out-Roman Numeral Converter
![]() |
•
•
Join Date: Apr 2009
Posts: 1
Reputation:
Solved Threads: 0
This is a project I'm working on for a class. I'm pretty new to CS and python...and I found that I am totally lost. I'm suppose to write a program to convert Roman numerals to arabic numbers. It is also suppose to handle invalid input.
Here is what I have so far:
def roman_converter():
R=str.upper(raw_input("Enter a Roman numeral to convert to Arabic: "))
d={'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
s=0
for c in range(len(R)):
while R[0]<R[1]:
x=R[1]-R[0]
s=s+x
else:
x=R[0]+R[1]
s=s+x
R=R[2:]
else:
return s
print R, '=', s
roman_converter()
I'm getting all sorts of errors with this, and I don't know what to do about the handling of invalid input... help please!
Here is what I have so far:
def roman_converter():
R=str.upper(raw_input("Enter a Roman numeral to convert to Arabic: "))
d={'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
s=0
for c in range(len(R)):
while R[0]<R[1]:
x=R[1]-R[0]
s=s+x
else:
x=R[0]+R[1]
s=s+x
R=R[2:]
else:
return s
print R, '=', s
roman_converter()
I'm getting all sorts of errors with this, and I don't know what to do about the handling of invalid input... help please!
Well, there's two types of validation... you'll need to first verify that the numerals are in the right order, ie, you can't have IC or something, but you could have XC, or IX, but not VX or IL, etc. That's one validation.
The other validation that you would need is to make sure that ONLY characters used for roman numerals are input. If you're just working with I,V,X,L,C,D,and M, then any other character is invalid.
Does that make sense?
The other validation that you would need is to make sure that ONLY characters used for roman numerals are input. If you're just working with I,V,X,L,C,D,and M, then any other character is invalid.
Does that make sense?
Alex Cavnar, aka alc6379
•
•
Join Date: Dec 2006
Posts: 1,008
Reputation:
Solved Threads: 285
You might start with code tags (use the "#" icon) and then explain what the program you posted does, as it doesn't appear to make sense. Once you reach this line
while R[0]<R[1]:
it appears to be an infinite loop, but it's difficult to tell without proper indents.
Basically, you want
1) the dictionary that you have named "d" and
2) test that all input letters are in the dictionary, with a test:
3) if the value of this letter is less than the value of the next letter then subtract the value. Otherwise, add the value. Post some code with comments as to what it does if you want more help.
while R[0]<R[1]:
it appears to be an infinite loop, but it's difficult to tell without proper indents.
Basically, you want
1) the dictionary that you have named "d" and
2) test that all input letters are in the dictionary, with a test:
3) if the value of this letter is less than the value of the next letter then subtract the value. Otherwise, add the value. Post some code with comments as to what it does if you want more help.
Last edited by woooee; Apr 9th, 2009 at 11:57 pm.
![]() |
Other Threads in the Python Forum
- Previous Thread: Error msg, when doing a function
- Next Thread: Moving data from one Tkinter GUI to another
| Thread Tools | Search this Thread |
abrupt accessdenied anti apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionaries dictionary dynamic edit enter examples file float format function gui heads homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl python random recursion redirect remote reverse scrolledtext session simple software sprite statictext statistics string strings syntax table terminal text textarea threading time tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython






