python - Printing my Class attributes -
these 2 parts of misbehaving text based adventure code i'm trying learn how youtube tutorial. believe of problems arise because tutorial i'm pretty sure using python2.something , i'm using 3. problem i'm trying list "player" class attributes on start screen, have done playername, i'm having trouble getting numbers listed self.attack , self.health etc print. downloaded python first time 5 days ago, bear noob if can, please. let me know can change, , thank in advance!
class player: def _init_(self, name): self.name = name self.maxhealth = 100 self.health = self.health self.attack = 10 self.gold = 0 self.bandages = 0 def start1(): os.system("cls") print("name: %s" % (playerig)) print("attack: {}".format (playerig.attack)) print('health: {}/{}'.format(playerig.health, playerig.maxhealth)) print("gold: %i") % playerig.gold print("bandages: %i") % playerig.bandages
a few things immediately:
def _init_
needsdef __init__
(note double underscores).self.health = self.health
doesn't make sense - whatever on right side of=
needs exist before can assign other variable it, ,self.health
doesn't exist until line.- the variable
playerig
never assigned anything, none of code instart1
work (unless done somewhere else, haven't included in question).
a working version of have in question like
class player: def __init__(self, name, maxhealth, health, attack, gold, bandages): self.name = name self.maxhealth = maxhealth self.health = health self.attack = attack self.gold = gold self.bandages = bandages def start1(): playerig = player('foo', 100, 50, 10, 0, 0) print('name: {}'.format(playerig.name)) print('health: {}/{}'.format(playerig.health, playerig.maxhealth)) print('attack: {}'.format(playerig.attack)) print('gold: {}'.format(playerig.gold)) print('bandages: {}'.format(playerig.bandages)) start1()
there many improvements make there, defining __str__
function player
or using keyword arguments in __init__
method.
Comments
Post a Comment