2

I'm very new to programming so please help me with this!

I wish to print all the values of roman_numeral_map in the command line, but I get lots of errors.

Here is my code:

class roman1:

    roman_numeral_map = (('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))

    def to_roman():
        '''convert integer to roman numeral'''
        for numeral, integer in roman_numeral_map:
            print(numeral, integer)


    if __name__ == '__main__':
        self.roman = roman1()
        roman.to_roman

UPDATE:

This is the traceback I get (thanks)!

Traceback (most recent call last):
  File "/Users/michaelmatos/PycharmProjects/diveintopython3/roman1.py", line 4, in <module>
    class roman1:
  File "/Users/michaelmatos/PycharmProjects/diveintopython3/roman1.py", line 27, in roman1
    self.roman = roman1()
NameError: name 'roman1' is not defined
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
matos416
  • 85
  • 4

4 Answers4

3

You have a few issues with your code, I've fixed them below with comments inline.

Importantly, leading whitespace is significant in Python and you need to use self to reference instance variables and methods.

class roman1:
    roman_numeral_map = (('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))

    def to_roman(self): # Instance methods take `self`
        '''convert integer to roman numeral'''
        for numeral, integer in self.roman_numeral_map: # Note: `self`
            print(numeral, integer)


if __name__ == '__main__': # Note: Indent to same level as `class` above
    roman = roman1() # `roman` is global, no need for `self`
    roman.to_roman() # Use `()` to *call* `to_roman()`

See it run!

Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
0

If you are just looking to iterate through the tuple and print out each selection than you can try this.

>>> i = 0
>>> while i < len(m):
...     print m[i]
...     i += 1
... 
('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)

It would be great to see an example of your desired output.

Declare your class like so just to get started.

class roman1:
    pass

Then instantiate an instance of roman1 class.

roman = roman1()
Jesus Noland
  • 197
  • 2
  • 11
0

The error message occurs because the if block is IN the class deceleration. 'roman1' has not been made yet. Also, you should declare an attribute of the class (self.roman) in the constructor, which is a method named __init__

class roman1:
    def __init__():
        self.roman = self

Though in this case, it is useless and redundant. If you want to make the roman variable outside of the class, omit the 'self' part

Assuming your data structure is correct (Nested tuples to create essentially a table with two columns), you can print each grouping with this:

for group in roman_numeral_map:
    print group

To print each value, use a nested for loop:

for group in roman_numeral_map:
    for value in group:
        print value
Timidger
  • 1,199
  • 11
  • 15
0

An altered class:

class RomanNumeral():
    values = [
        ('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)
    ]

    def __init__(self, i):
        self.i = i

    def __str__(self):
        num = []
        n = self.i
        for ch,val in RomanNumeral.values:
            while n >= val:
                num.append(ch)
                n -= val
        return ''.join(num)

if __name__ == '__main__':
    print(RomanNumeral(4))
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99