So I'm making a python Module to create and save data for an in-game character. the the class is Character and goes as follows:
#!/usr/bin/python
import os
import re
class Character:
storage = None
health = None
strength = None
xp = None
def __init__(self,stg):
os.chdir('/Users/alex/Desktop/Python/Support/Character')
storage = stg
f = open(storage)
#health index = 0
#strength index = 1
#xp index = 2
string = f.read()
finder = re.compile('/n')
stats = finder.split(string)
health = int(stats[0])
strength = int(stats[1])
xp = int(stats[2])
f.close
def adjHealth(self,amount):
health += amount
def adjStrength(self,amount):
strength += amount
def adjXp(self,amount):
xp += amount
def save(self):
os.chdir('/Users/alex/Desktop/Python/Support/Character')
stats = [str(health),str(strength),str(xp)]
f = open(storage,'w')
f.writelines(stats)
Whenever I do this command from the python interpreter:
>>> import character as ch
>>> ch.Character('jimmy')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/alex/Desktop/Python/Modules/character.py", line 22, in __init__
health = int(stats[0])
ValueError: invalid literal for int() with base 10: '10\n10\n0\n'
It comes up with that ValueError.
It should be splitting the string returned by f.read() into ['10','10','0',''] right?
So why can't I convert '10' into an int?
I'm kind of new to reqular expressions.
\nand/n.stats = string.split('\n')