13 Replies - 3003 Views - Last Post: 01 October 2011 - 06:41 AM Rate Topic: -----

#1 bleed.green14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 bleed.green14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • 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.

Was This Post Helpful? 0
  • +
  • -

#7 bleed.green14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#8 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • 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


Was This Post Helpful? 0
  • +
  • -

#9 bleed.green14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
    

Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5760
  • View blog
  • Posts: 12,573
  • 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.
Was This Post Helpful? 0
  • +
  • -

#11 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • 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:
        add 1 to total
    if character is V:
        add 5 to total
    if character is X:
        add 10 to total
    if character is L:
        add 50 to total
    if character is C:
        add 100 to total


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

Was This Post Helpful? 1
  • +
  • -

#12 bleed.green14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#13 kevinja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5760
  • View blog
  • Posts: 12,573
  • Joined: 16-October 07

Re: Creating a roman numeral calculator

Posted 01 October 2011 - 06:41 AM

View Postkevinja, 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.

View Postkevinja, 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.


Code tags, add them always.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1