Python 我想用命令行打印所有的值
我对编程非常陌生,所以请帮我解决这个问题 我希望在命令行中打印Python 我想用命令行打印所有的值,python,Python,我对编程非常陌生,所以请帮我解决这个问题 我希望在命令行中打印roman\u numeric\u map的所有值,但是我得到了很多错误 这是我的密码: class roman1: roman_numeral_map = (('M', 1000), ('CM', 900), ('D', 500), ('CD', 400),
roman\u numeric\u 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
更新:
这是我得到的回溯(谢谢)
回溯(最近一次呼叫最后一次):
文件“/Users/michaelmatos/PycharmProjects/diveintopython3/roman1.py”,第4行,在
第1类:
文件“/Users/michaelmatos/PycharmProjects/diveintopython3/roman1.py”,roman1中的第27行
self.roman=roman1()
NameError:未定义名称“roman1”
如果您只是希望遍历元组并打印出每个选择,那么您可以尝试此操作
>>> 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)
然后实例化roman1类的一个实例
roman = roman1()
出现错误消息是因为if块在类中。”《罗马书1》还没有出版。另外,您应该在构造函数中声明类(self.roman)的一个属性,它是一个名为
\uuuu init\uuu
class roman1:
def __init__():
self.roman = self
虽然在这种情况下,它是无用的和多余的。如果要使roman变量位于类之外,请省略“self”部分
假设您的数据结构正确(嵌套元组基本上创建了一个包含两列的表),您可以使用以下内容打印每个分组:
for group in roman_numeral_map:
print group
要打印每个值,请使用嵌套的for循环:
for group in roman_numeral_map:
for value in group:
print value
您的代码有一些问题,我在下面用内联注释修复了它们 重要的是,您需要引用实例变量和方法
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()`
! 一个经过修改的类:
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))
请更正缩进并发布完整的回溯。您的问题似乎不在打印中。。。但在其他语法中。罗马数字地图应该是什么样的数据类型?非常感谢!!!我真的很欣赏这个行业的社区意识
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()`
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))