我在使用简单列表时遇到问题。
我想把它们分开。
但是,调试器告诉我index is out of range
...
def __init__(self,num, price):
self.num = num
self.price = price
def to_pay(self)
y = self.num # y = 'XY 70842'
x = y.split(' ') # x = ['XY', '70842']
letters = list(x[0]) # ['X', 'Y']
numbers = list(x[1]) # ['7', '0', '8', '4', '2']
预期结果:
letters = ['X', 'Y']
numbers = ['7', '0', '8', '4', '2']
实际结果:
numbers = list(x[1]) # ['7', '0', '8', '4', '2']
IndexError: list index out of range