Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

It's your regular expression. The following will look for the literal character / followed by n:

finder = re.compile('/n')

This on the other hand will look for newlines:

finder = re.compile('\n')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top