# Creating a roman numeral calculator

Page 1 of 1

## 13 Replies - 6080 Views - Last Post: 01 October 2011 - 06:41 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=248823&amp;s=e76b94543d941e660bee0752b57fb16e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bleed.green14

Reputation: 0
• Posts: 11
• Joined: 22-September 11

# Creating a roman numeral calculator

Posted 27 September 2011 - 01:39 PM

Ok so for my intro class I have to make a roman numeral calculator using mostly if, for, and elif statements (i.e. no lists, dictionaries) so the problem I am running into is converting a string into an integer value my code so far is below:

```#Roman numeral calculator

first_str = raw_input('Enter the first Roman Number (with no spaces):')
first_str = first_str.upper()

print 'Print you entered', first_str,'for the first number.'

second_str = raw_input('Enter the second Roman Number (with no spaces):')
second_str = second_str.upper()

print 'Print you entered', second_str,'for the second number.'

I = 1
V = 5
X =10
L = 50
C = 100
```

so when I use first_str = int(first_str)
it doesnt work so it doesnt recognize it as a number, so if you could offer a start as how to get around this, i'd really appreciate it.

Is This A Good Question/Topic? 0

## Replies To: Creating a roman numeral calculator

### #2 izthrower

Reputation: 13
• Posts: 233
• Joined: 11-February 09

## Re: Creating a roman numeral calculator

Posted 27 September 2011 - 02:10 PM

Have you tryed doing and if elif statment like:

if first_str == I:
X = 1

so on and so forth?
x is just a random var name in cause ur wondering.

### #3 Motoma

Reputation: 452
• Posts: 797
• Joined: 08-June 10

## Re: Creating a roman numeral calculator

Posted 27 September 2011 - 03:20 PM

Are you converting from Roman Numerals to Decimal, or Decimal to Roman Numerals?

### #4 bleed.green14

Reputation: 0
• Posts: 11
• Joined: 22-September 11

## Re: Creating a roman numeral calculator

Posted 27 September 2011 - 06:08 PM

I am converting roman numermal to integers, which is where the problem arises.

### #5 Simown

• Blue Sprat

Reputation: 321
• Posts: 650
• Joined: 20-May 10

## Re: Creating a roman numeral calculator

Posted 27 September 2011 - 06:40 PM

I think I can see a possible problem, you have I = 1, and when the user enters the letter "I" it's a string, not a variable, I != "I".

If you can't use lists or dictionaries, try doing it like this:

```if first_str == "I":
number = 1
else if first_str == "V":
number = 5
.....

```

And then you could repeat that for the second string, a function would work here to take a string, and output the value of that string.

Remember don't check against the variable I (you don't even need it). Check against the string.

This post has been edited by Simown: 27 September 2011 - 06:46 PM

### #6 Motoma

Reputation: 452
• Posts: 797
• Joined: 08-June 10

## Re: Creating a roman numeral calculator

Posted 28 September 2011 - 06:35 AM

The basic principle: for each character in the input, add that character's value to the grand total.

You'll need to write your code to:
• Create a variable for the total, set to 0.
• Iterate over each character in the input.
• Add that character's value to the total.

### #7 bleed.green14

Reputation: 0
• Posts: 11
• Joined: 22-September 11

## Re: Creating a roman numeral calculator

Posted 28 September 2011 - 05:28 PM

so what I tried doing below is not working, plus another problem I am not sure how to approach is if the input is say:
first_str = VI... and not just one letter.
```#Roman numeral calculator

first_str = raw_input('Enter the first Roman Number (with no spaces):')
first_str = first_str.upper()

print 'You entered', first_str,'for the first number.'

second_str = raw_input('Enter the second Roman Number (with no spaces):')
second_str = second_str.upper()

print 'You entered', second_str, 'for the second number.'

if first_str == 'I':
number = 1
elif first_str == 'V':
number = 5
elif first_str == 'X':
number = 10
elif first_str == 'L':
number = 50
elif first_str == 'C':
number = 100
print first_str
```

### #8 Motoma

Reputation: 452
• Posts: 797
• Joined: 08-June 10

## Re: Creating a roman numeral calculator

Posted 28 September 2011 - 07:09 PM

A for loop will allow you to iterate over each character in the input string:
```for character in string:
# Do stuff with character

```

### #9 bleed.green14

Reputation: 0
• Posts: 11
• Joined: 22-September 11

## Re: Creating a roman numeral calculator

Posted 29 September 2011 - 07:44 AM

So this is what I tried to do and the one thing I am not sure of is why it isn't printing anything as far a number wise.
```#Roman numeral calculator

first_str = raw_input('Enter the first Roman Number (with no spaces):')
first_str = first_str.upper()

print 'You entered', first_str,'for the first number.'

second_str = raw_input('Enter the second Roman Number (with no spaces):')
second_str = second_str.upper()

print 'You entered', second_str, 'for the second number.'

while first_str == True:
if 'I' == True:
for i in first_str:
print 1
if 'V' == True:
for v in first_str:
print 5
if 'X' == True:
for x in first_str:
print 10
if 'L' == True:
for l in first_str:
print 50
if 'C' == True:
for c in first_str:
print 100

```

### #10 baavgai

• Dreaming Coder

Reputation: 6608
• Posts: 13,948
• Joined: 16-October 07

## Re: Creating a roman numeral calculator

Posted 29 September 2011 - 07:51 AM

What does this mean?
```    if 'I' == True:

```

A letter compared to boolean True is never true:
```>>> 'I'==True
False
>>>

```

So, you never enter the loop, whatever it's supposed to do.

### #11 Motoma

Reputation: 452
• Posts: 797
• Joined: 08-June 10

## Re: Creating a roman numeral calculator

Posted 29 September 2011 - 07:52 AM

Try turning this psedocode into real code:
```save a string from user input
set total to 0
for character in string:
if character is I:
if character is V:
if character is X:
if character is L:
if character is C:

```

This post has been edited by Motoma: 29 September 2011 - 07:52 AM

### #12 bleed.green14

Reputation: 0
• Posts: 11
• Joined: 22-September 11

## Re: Creating a roman numeral calculator

Posted 29 September 2011 - 08:08 AM

Ok thank you that worked very well.

### #13 kevinja

Reputation: 0
• Posts: 32
• Joined: 29-September 10

## Re: Creating a roman numeral calculator

Posted 30 September 2011 - 10:31 PM

Hello there. I've taken my own spin on this. Unfortunately, I've done a rather clunky job of it but I want to see what can be done to improve it. For example, doing IV would, in theory, make a result of 6, but I checked for some of these, like IV and also IX. However there are others. Is there a much easier way of doing this without checking for each and every single combination? Maybe string.find()? Any help would be appreciated. Thank you!

```s=raw_input("Please input the roman numerals you'd like calculated. No spaces please!")
while s == "":
s=raw_input()
s=s.upper()
total=0
for c in s:
if c == "I":
total+=1
elif c == "V":
total+=5
elif c =="X":
total+=10
elif c =="L":
total+=50
elif c=="C":
total+=100
if s=="IX" or s=="IV" or s=="IL":
total -=1
print 'After entering ', s, ', the result is: ', total

```

And there's my problem again, I try to show indents and they did not come through. Using a screen reader so I didn't realize this till after I had posted it. Can't exactly figure out how to paste into the editor

This post has been edited by baavgai: 01 October 2011 - 06:26 AM
Reason for edit:: tagged

### #14 baavgai

• Dreaming Coder

Reputation: 6608
• Posts: 13,948
• Joined: 16-October 07

## Re: Creating a roman numeral calculator

Posted 01 October 2011 - 06:41 AM

kevinja, on 01 October 2011 - 01:31 AM, said:

Is there a much easier way of doing this without checking for each and every single combination?

Yes and now want to consider the edge cases. The problem is, you're looking at one letter at time, so pairs become a special case. You want to loop through the index of the string. Also, you want to start at M and work you way back.

You would do well to have a list that looks something like:
```symbols = [ ('CM',900),('M',1000), ... ]

```

And loop through it.

Note, IL does not make sense. 49 reads XLIX.

kevinja, on 01 October 2011 - 01:31 AM, said:

And there's my problem again, I try to show indents and they did not come through.