The problem is with your regex, but there is no particular need for regex here - using the capabilities provided by file objects, all of this (corrected) code:
string = f.read()
finder = re.compile('\n')
stats = finder.split(string)
can be reduced to the one-line:
stats = f.readlines()
Or, to have it without the trailing newlines:
f.read().splitlines()
Also, note that you're not actually closing the file - you need to call the close method, not just mention it. But the preferred way to do this is a with
statement:
with open(stg) as f:
stats = f.readlines()
This ensures that the file gets closed as soon as you're done with it, even if something goes wrong while reading it.
Your code also has another bug that will bite you sooner rather than later:
health = int(stats[0])
creates a new function-local variable called health
- it doesn't assign it to the one you've set to None
up in the class body. You can set that one by assigning to Character.health
, but you almost certainly want self.health
instead. This will bite you in your adj*
methods, which will, as written, result in an error.
You would also end up removing those variables from the class body - they don't behave like C++ or Java attribute declarations (which Python doesn't require or, without metaclass magic, even support), and don't end up being used.