So I have been playing with classes, seeing that they are more efficient than globals, and I wrote this hopefully simple test program to see if I properly reverse engineered tetlaw's simple rpg. For the life of me I cannot figure out what is wrong with it. If an expert on classes here could tell me why that would be lovely.

import random
import time



class items:
    def __init__(self):
        self.nametext = ['health potion', 'dagger', '5 gold']
        self.name = self.nametext[self._name()]
        self.healthpotions = 0
        self.daggers = 0
        self.gold = 0
        self.newitem = 0

    def potgain(self):
        if self.newitem == 1:
            items.healthpotions = items.healthpotions + 1
            self.newitem = 0
    def daggain(self):
        if self.newitem == 2:
            items.daggers += 1
            self.newitem = 0
    def goldgain(self):
        if self.newitem == 3:
            items.gold += 5
            self.newitem = 0
    def _name(self):
        return random.randrange(3)+1

def playerinv():
    it = items()
    print "Inventory:"
    print "==================="
    print "Gold:            ",it.gold
    print "Daggers:         ",it.daggers
    print "Healing Potions: ",it.healthpotions
    invact()

def invact():
    inv = input('>> ')
    if inv == 1:
        exit
    if inv == 2:
        randitem()
    else:
        print "Invalid. 1 for exit, 2 for an item."
        playerinv()


def randitem():
    global it
    it = items()
    
    
 
    if it._name == 1:
        it.healthpotions += 1
    elif it._name == 2:
        it.daggers +=  1
    elif it._name == 3:
        it.gold += 5
    else:
        print "Something has gone horribly wrong"
        
        
    print "A faint magical glow illuminates the dark corridor..."
    print "...and",it.nametext,"appears into your hands. It is"
    print "dark once more..."
    time.sleep(2)
    playerinv()


def additems():
    items.newitem = 1
    playerinv()

You have got to give a sample input and a the related output that you are expecting.

Put a main section i.e.

if __name__  ==  '__main__':
    # initialize some vars
    # execute the function with sample parameters
    # show the expected output as remarks

This will be a great starting point for people going through your code to figure out why the required output is not coming.

Well, if you ran what you posted, nothing will happen. You defined these functions and classes but you haven't actually done anything with them. None of these get called during the execution of the script.

This may sound dumb but umm....how do I call them?

Thanks a bunch!

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.