[Tutor] Objects & Classes...

Jack Cruzan jcruzan at gmail.com
Mon Jan 17 19:36:42 CET 2005


Hello!

I am writing (or at least attempting) to write a Character generation
utility for Shadowrun in Python of course! After reading about other
attempts to make and RPG dealing with with character generation it looks
like OOP is the best way to go. I have no experiance with OOP and its
kinda throwing me for a loop.

Here is what I mean...

I make a class

class Character:
	def __init__(self, name = ' ', race = 'Human', magic = 'None'):
		self.name=name
		self.race=race
		self.magic=magic

Great, now I need some from the user to create the character. This is
what I have.

def createChar(book):
	name = raw_input("Enter your character's name. ")
	race = int(raw_input("What race? (1: Human, 2: Elf, 3: Ork, 4: Troll,
5: Dwarf,)"))
	book[name] = race

What am I doing wrong? 

Oh and one other thing I can't load my data. I can create a new file,
but not load it. Here is the code for that...

def loadChar(book):
	import os
	filename = 'SRchargen.dat'
	if os.path.exists(filename):
		store = open(filename,'r')
		while store:
			name = store.readline().strip()
			race = store.readline().strip()
			book[name] = race
		else:
			store = open(filename, 'w')
		store.close

Any help would be greatly appreciated!







More information about the Tutor mailing list