I've written a program to parse info about an object from a file, but it consistently won't read the last item in the file list and I'm not sure why.
The object's compute method (the one that's having issues) depends on the following dictionary (abridged for minimality):
S_dict = {'Advanced Saucecrafting': 5, 'The Way of Sauce': 3}
The object itself is
class Character(object):
def __init__(self, name, idnum):
self.name = name
self.idnum = idnum
self.path = 'C:\\Users\\Bit\\Desktop\\Mafia\\data\\BtS_' + name + ' (#' + idnum + ')' + '.txt'
self.SC = 0
self.TT = 0
self.P = 0
self.S = 0
self.DB = 0
self.AT = 0
self.skills = [self.SC, self.TT, self.P, self.S, self.DB, self.AT]
self.SC_skills = []
self.TT_skills = []
self.P_skills = []
self.S_skills = []
self.P_skills = []
self.DB_skills = []
self.AT_skills = []
def compute(self):
self.SC = 0
self.TT = 0
self.P = 0
self.S = 0
self.DB = 0
self.AT = 0
self.SC_skills = []
self.TT_skills = []
self.P_skills = []
self.S_skills = []
self.P_skills = []
self.DB_skills = []
self.AT_skills = []
with open(self.path, 'r') as f:
text = f.read()
perms = text.split(', ')
for p in perms:
try:
self.SC += SC_dict[p]
self.SC_skills.append(p)
except KeyError:
pass
try:
self.TT += TT_dict[p]
self.TT_skills.append(p)
except KeyError:
pass
try:
self.P += P_dict[p]
self.P_skills.append(p)
except KeyError:
pass
try:
self.S += S_dict[p]
self.S_skills.append(p)
except KeyError:
pass
try:
self.DB += DB_dict[p]
self.DB_skills.append(p)
except KeyError:
pass
try:
self.AT += AT_dict[p]
self.AT_skills.append(p)
except KeyError:
pass
self.skills = [self.SC, self.TT, self.P, self.S, self.DB, self.AT]
When I run compute it consistently, and robustly fails to read the last element in the .txt file I read from, a sample file for which it fails is below
Perfect Freeze, The Long View, Wisdom of the Elder Tortoises, Ambidextrous Funkslinging, Pulverize, Snokebomb, Subtle and Quick to Anger, The Way of Sauce, Torso Awaregness, Advanced Cocktailcrafting, Kung Fu Hustler, Cannelloni Cocoon, The Ode to Booze, Transcendental Noodlecraft, Master of the Surprising Fist, Spirit of Ravioli, Pastamastery, Advanced Saucecrafting
in this case the vector self.skills returns
[4, 8, 10, 3, 9, 6]
but self.skills[3] is a 3 when if you look in the S_dict you see that it possesses the keys 'Advanced Saucecrafting' and 'The Way of Sauce' which should give a combined total of 3 + 5 = 8. Additionally the attribute self.S_skills is simply ['Advanced Saucecrafting'] indicating that this part of the for loop never runs in the compute method which is more evidence to make me suspect the offending issue is hidden within.
text = f.read()-->text = f.read().strip()