19
Апр
2021

При выводе в списке появляется пустая строка, из-за которой программа работает некорректно

class VoltaicPile:
    def __init__(self, sp):
        self.sp, self.pos, self.i = sp[:], {'Cu': 'cloth', 'cloth': 'ZnCu', 'Zn': 'cloth'}, -1 
        
    def __str__(self):
        return f'{(self.sp.count("Cu") + self.sp.count("Zn")) // 2 * 1.1} V'
    
    def __next__(self):
        self.i += 1
        return self.sp[self.i]
        
    def __len__(self):
        return len(self.sp)
    
    def __iter__(self):
        return iter(self.sp)
        
    def __getitem__(self, item):
        return self.sp[item]
        
    def __setitem__(self, key, item):
        self.sp[key] = item
        
    def append(self, el):
        self.mt = ''.join(self.sp).split('cloth')
        self.sp.append(el) if el in self.pos[self.sp[-1]] and el != self.mt[-1] else None
        
        
        
vp = VoltaicPile(['Cu', 'cloth', 'Zn'])
for item in ['Cu', 'Zn', 'cloth', 'Cu', 'Zn', 'cloth', 'Cu', 'Zn', 'cloth']:
    vp.append(item)
    print(''.join(vp.sp).split('cloth'))
print(*vp)
print(vp)
print(vp.mt)

Источник: https://ru.stackoverflow.com/questions/1272055/%D0%9F%D1%80%D0%B8-%D0%B2%D1%8B%D0%B2%D0%BE%D0%B4%D0%B5-%D0%B2-%D1%81%D0%BF%D0%B8%D1%81%D0%BA%D0%B5-%D0%BF%D0%BE%D1%8F%D0%B2%D0%BB%D1%8F%D0%B5%D1%82%D1%81%D1%8F-%D0%BF%D1%83%D1%81%D1%82%D0%B0%D1%8F-%D1%81%D1%82%D1%80%D0%BE%D0%BA%D0%B0-%D0%B8%D0%B7-%D0%B7%D0%B0-%D0%BA%D0%BE%D1%82%D0%BE%D1%80%D0%BE%D0%B9-%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%BC%D0%B0-%D1%80%D0%B0%D0%B1%D0%BE%D1%82%D0%B0%D0%B5%D1%82-%D0%BD

Тебе может это понравится...

Добавить комментарий