我想使用命令行打印所有值

2024-10-01 13:33:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我对编程非常陌生,所以请帮助我!你知道吗

我希望在命令行中打印roman_numeral_map的所有值,但是我得到了很多错误。你知道吗

这是我的密码:

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

更新:

这是我得到的回溯(谢谢)!你知道吗

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

Tags: tonameinselfmapintegerusersclass
3条回答

如果您只是想遍历元组并打印出每个选择,那么您可以尝试这样做。你知道吗

>>> 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)

很高兴能看到一个你想要的输出的例子。你知道吗

像这样声明你的类只是为了开始。你知道吗

class roman1:
    pass

然后实例化roman1类的一个实例。你知道吗

roman = roman1()

你的代码有一些问题,我在下面用内联注释修复了它们。你知道吗

重要的是,leading whitespace is significant in Python,您需要use ^{}来引用实例变量和方法。你知道吗

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!你知道吗

一个改变了的班级:

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))

相关问题 更多 >