def classCreate(self):
        self.classChoose = Toplevel(root)
        self.classChoose.title('Choose your Class')
        self.classChoose.geometry('300x200+300+400')
        self.labelChoose = Label(self.classChoose, text="Select the class you wish to be")
        self.labelChoose.grid(row=0)
        self.listClass = Listbox(self.classChoose, height=4)
        self.listClass.grid(row=1)
        self.listClass.insert(END,"Warrior")
        self.listClass.insert(END,"Brute")
        self.confirm = Button(self.classChoose, text="Confirm", command=self.confirm)
        self.confirm.grid(row=2)
    def confirm(self):
        fileClass = open('ClassData.txt','r')
        for line in fileClass:
            classInfo=line.split(',')
            print "Name is:", self.Name
            print "This class's stats are:",classInfo
            if classInfo[0]==self.listClass.curselection()[0]:
                className=classInfo[0]
                self.CharHpMax = classInfo[1]
                self.CharHpCurrent = classInfo[2]
                self.CharMpMax = classInfo[3]
                self.CharMpCurrent = classInfo[4]
                self.CharStr = classInfo[5]
                self.CharDef = classInfo[6]
                self.CharMAtt = classInfo[7]
                self.CharMDef = classInfo[8]
                self.Currentexp=0
                self.Requiredexp=10
                print "The name of the class is: ",className
                print "The Character's max HP is: ",self.CharHpMax
                print "The Character's Current Hp is: ",self.CharHpCurrent
                print "The Character's max Mp is: ",self.CharMpMax            
                print "The Character's current Mp is: ",self.CharMpCurrent
                print "The Character's Strength is: ",self.CharStr
                print "The Character's Defence is: ", self.CharDef
                print "The Character's Magic Power is: ", self.CharMAtt
                print "The Character's Magic Resistance is: ", self.CharMDef
                fileChar = open('Character Data\ '+self.Name+'.txt','a')
                fileChar.writelines(className+','+self.CharHpMax+','+self.CharHpCurrent+','+self.CharMpMax+','+self.CharMpCurrent+','+self.CharStr+','+self.CharDef+','+self.CharMAtt+','+self.CharMDef+','+str(self.Currentexp)+','+str(self.Requiredexp))
        self.mainGame()
        self.classChoose.destroy()

I'm trying to work in a second class, but it won't work. I want it to read a line, see if the first thing in the line matches the selected class (Warrior or Brute) and give stats accordingly, but it does both!
The stats are on the next line of the text file:

Warrior,250,250,20,20,25,30,10,5
Brute,300,300,0,0,30,20,5,2

I've fixed my code based on Ene Uran's instructions, but I can't get it to load seperate stats for each class, it just runs them all into one file when I try. Should I just make a seperate text file for each Class? Here's the code, I'm trying to seperate the info at the ; and then split each item further into seperate stats at the commas, so it can read the first data and compare it with the class selected in the list.

#Role Playing Form.py
from Tkinter import *
#Create the Start window
class character(object):
    def __init__(self, master):
        torch=PhotoImage(file="C:\Python24\Python Progs\Role Playing Form\Images\Title Screen Flames.gif")
        grave=PhotoImage(file="C:\Python24\Python Progs\Role Playing Form\Images\TitleScreenGraves.gif")
        ankh=PhotoImage(file="C:\Python24\Python Progs\Role Playing Form\Images\TitleScreenAnkh.gif")
        self.master = master
        self.master.title('Role Playing Form V1.0')
        self.master.geometry('250x250+350+450')
        self.torchLabelL=Label(self.master, image=torch)
        self.torchLabelL.grid(row=0, column=0)
        self.cmdCreate = Button(self.master, text='Create Character', command=self.nameCreate)
        self.cmdCreate.grid(row=0, column=1)
        self.torchLabelR=Label(self.master, image=torch)
        self.torchLabelR.grid(row=0, column=2)
        self.graveLabelL=Label(self.master, image=grave)
        self.graveLabelL.grid(row=1, column=0)
        self.cmdDelete = Button(self.master, text='Delete Character', command=self.delete)
        self.cmdDelete.grid(row=1, column=1)
        self.graveLabelR=Label(self.master, image=grave)
        self.graveLabelR.grid(row=1, column=2)
        self.ankhLabelL=Label(self.master, image=ankh)
        self.ankhLabelL.grid(row=2, column=0)
        self.cmdLoad = Button(self.master, text='Load Character', command=self.load)
        self.cmdLoad.grid(row=2, column=1)
        self.ankhLabelR=Label(self.master, image=ankh)
        self.ankhLabelR.grid(row=2, column=2)
        self.master.mainloop()
    def nameCreate(self):
        self.nameInput = Toplevel(root)
        self.nameInput.title('Name of Character?')
        self.nameInput.geometry('300x100+350+450')
        self.lblName = Label(self.nameInput, text='Enter Name Here')
        self.lblName.grid(row=0)
        self.entName = Entry(self.nameInput)
        self.entName.grid(row=0, column=1)
        self.cmdName = Button(self.nameInput, text='Enter', command=self.enter)
        self.cmdName.grid(row=1)
        self.entName.focus()
    def enter(self):
        self.Name = self.entName.get()
        print "Character Name:",self.Name
        fileChar = open('Character Data\ '+self.Name+'.txt',"w")
        self.classCreate()
        self.nameInput.destroy()
    def classCreate(self):
        self.classChoose = Toplevel(root)
        self.classChoose.title('Choose your Class')
        self.classChoose.geometry('300x200+300+400')
        self.labelChoose = Label(self.classChoose, text="Select the class you wish to be")
        self.labelChoose.grid(row=0)
        self.listClass = Listbox(self.classChoose, height=4)
        self.listClass.grid(row=1)
        self.listClass.insert(END,"Warrior")
        self.listClass.insert(END,"Brute")
        self.confirm = Button(self.classChoose, text="Confirm", command=self.confirm)
        self.confirm.grid(row=2)
    def confirm(self):
        fileClass = open('ClassData.txt','r')
        for line in fileClass:
            classInfo=line.split(';')
            for i in classInfo:
                classSpec=str(classInfo).split(',')
                print "Name is: ", self.Name
                print "This class's stats are:",classSpec
                if classSpec[0]==self.listClass.curselection()[0]:
                    className=classSpec[0]
                    self.CharHpMax = classSpec[1]
                    self.CharHpCurrent = classSpec[2]
                    self.CharMpMax = classSpec[3]
                    self.CharMpCurrent = classSpec[4]
                    self.CharStr = classSpec[5]
                    self.CharDef = classSpec[6]
                    self.CharMAtt = classSpec[7]
                    self.CharMDef = classSpec[8]
                    self.Currentexp=0
                    self.Requiredexp=10
                    print "The name of the class is: ",className
                    print "The Character's max HP is: ",self.CharHpMax
                    print "The Character's Current Hp is: ",self.CharHpCurrent
                    print "The Character's max Mp is: ",self.CharMpMax            
                    print "The Character's current Mp is: ",self.CharMpCurrent
                    print "The Character's Strength is: ",self.CharStr
                    print "The Character's Defence is: ", self.CharDef
                    print "The Character's Magic Power is: ", self.CharMAtt
                    print "The Character's Magic Resistance is: ", self.CharMDef
                    fileChar = open('Character Data\ '+self.Name+'.txt','a')
                    fileChar.writelines(className+','+self.CharHpMax+','+self.CharHpCurrent+','+self.CharMpMax+','+self.CharMpCurrent+','+self.CharStr+','+self.CharDef+','+self.CharMAtt+','+self.CharMDef+','+str(self.Currentexp)+','+str(self.Requiredexp))
                else:
                    break
            
        self.mainGame()
        self.classChoose.destroy()
    def delete(self):
        pass
    def load(self):
        self.nameLoad()
    def nameLoad(self):
        self.nameInputLoad = Toplevel(root)
        self.nameInputLoad.title('Name of Character?')
        self.nameInputLoad.geometry('300x100+350+450')
        self.lblLoadName = Label(self.nameInputLoad, text='Enter Name Here')
        self.lblLoadName.grid(row=0)
        self.entLoadName = Entry(self.nameInputLoad)
        self.entLoadName.grid(row=0, column=1)
        self.cmdLoadName = Button(self.nameInputLoad, text='Load', command=self.loadChar)
        self.cmdLoadName.grid(row=1)
        self.entLoadName.focus()
#Brings up a dialog box for putting the name in.
#The enter button creates a file with name.txt for the title.
#This will be used to store data for that character
        
    def loadChar(self):
        self.Name = self.entLoadName.get()
        fileChar = open('Character Data\ '+self.Name+'.txt','r')
        for line in fileChar: 
            classInfo=line.split(',')
            self.CharHpMax = classInfo[1]
            self.CharHpCurrent = classInfo[2]
            self.CharMpMax = classInfo[3]
            self.CharMpCurrent = classInfo[4]
            self.CharStr = classInfo[5]
            self.CharDef = classInfo[6]
            self.CharMAtt = classInfo[7]
            self.CharMDef = classInfo[8]
            for line in fileChar:
                expinfo=line.split(',')
                self.Currentexp = expinfo[0]
                self.Requiredexp = expinfo[1]
        self.mainGame()
            
#Adds the stats based on what you selected in the form
    def mainGame(self):
        self.gameMain = Toplevel(root)
        self.gameMain.title('Role Playing Form V 1.0')
        self.gameMain.geometry('400x500+350+400')
        self.mainLabel = Label(self.gameMain, text="CHARACTER INFO:")
        self.mainLabel.grid(row=0, column=0)
        self.nameLabel = Label(self.gameMain, text="Name: "+self.Name)
        self.nameLabel.grid(row=1, column=0)
        self.HpLabel = Label(self.gameMain, text="HP: "+self.CharHpMax+"\ "+self.CharHpCurrent)
        self.HpLabel.grid(row=2, column=0)
        self.MpLabel = Label(self.gameMain, text="MP: "+self.CharMpMax+"\ "+self.CharMpCurrent)
        self.MpLabel.grid(row=3, column=0)
        self.StrLabel = Label(self.gameMain, text="Strength: "+self.CharStr)
        self.StrLabel.grid(row=4, column=0)
        self.DefLabel = Label(self.gameMain, text="Defence: "+self.CharDef)
        self.DefLabel.grid(row=5, column=0)
        self.MAttLabel = Label(self.gameMain, text="Magic Power: "+self.CharMAtt)
        self.MAttLabel.grid(row=6, column=0)
        self.MDefLabel = Label(self.gameMain, text="Magic Resistance: "+self.CharMDef)
        self.MDefLabel.grid(row=7, column=0)
        self.expLabel = Label(self.gameMain, text="Exp Obtained: "+str(self.Currentexp)+" Exp Needed: "+str(self.Requiredexp))
        self.expLabel.grid(row=8, column=0)
        self.exitButton = Button(self.gameMain, text="Quit?", command=self.quitGame)
        self.exitButton.grid(row=0, column=1)
        self.fightButton = Button(self.gameMain, text="Fight?", command=self.fight)
        self.fightButton.grid(row=1, column=1)
    def quitGame(self):
        pass
    def fight(self):
        self.fightWin()
    def fightWin(self):
        self.fightScreen = Toplevel(root)
        self.fightScreen.title('Select an opponent')
        self.fightScreen.geometry('400x300+350+350')
        self.enemyLabel = Label(self.fightScreen, text="Select enemy")
        self.enemyLabel.grid(row=0)
        self.enemyList = Listbox(self.fightScreen, height=4)
        self.enemyList.grid(row=1)
        self.enemyList.insert(END,"Frog Guru")
        self.enemyButton = Button(self.fightScreen, text="Fight!", command=self.attack)
        self.enemyButton.grid(row=2)
    def attack(self):
        enemySel=self.enemyList.curselection()[0]
        print "Enemy: ",enemySel
        fileEn = open('EnemyData\Enemies.txt',"r")
        for line in fileEn:
            enemyData = line.split(',')
            print enemyData
            if enemyData[0] == enemySel:
                self.enemyName = enemyData[1]
                self.enemyHPCurrent=enemyData[2]
                self.enemyHP=enemyData[2]
                self.enemyMPCurrent=enemyData[3]
                self.enemyMP=enemyData[3]
                self.enemyStr=enemyData[4]
                self.enemyDef=enemyData[5]
                self.enemyMagAtt=enemyData[6]
                self.enemyMagDef=enemyData[7]
                self.enemyExp=enemyData[8]
            self.fightZone()
    
    def fightZone(self):
        enemyPic=PhotoImage(file=r".\Images\_"+self.enemyName+".gif")
        print "_"+self.enemyName+".gif"
        self.fightZone = Toplevel(root)
        self.fightZone.title('FIGHT!!')
        self.fightZone.geometry('400x300+350+350')
        self.lblenemyName=Label(self.fightZone, text="Enemy: "+self.enemyName)
        self.lblenemyName.grid(row=0, column=0)
        self.enemyPic=Label(self.fightZone, image=enemyPic)
        self.enemyPic.grid(row=0, column=1)
        self.lblenemyHP=Label(self.fightZone, text="HP: "+self.enemyHPCurrent+"/"+self.enemyHP)
        self.lblenemyHP.grid(row=1, column=0)
        self.lblEnemyMP=Label(self.fightZone, text="MP: "+self.enemyMPCurrent+"/"+self.enemyMP)
        self.lblEnemyMP.grid(row=2, column=0)
    
root = Tk()
main=character(root)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.