Python- Check List for items?

Discussion in 'Mac Programming' started by Nsutton, May 3, 2010.

  1. Nsutton macrumors member

    Nsutton

    Joined:
    Dec 29, 2009
    Location:
    6 Feet Under
    #1
    In a function like this how can you find out if a variable (inv) contains the necessary quest items and put that into an if statement. I've tried a few ideas but none work...

    Code:
    #Tavern Class
    class tavern():
        def taver_n(self):
        #if quest is in progress or finished
            if currnt_quest=="Chef's Helper":
                done=raw_input("Chef Bill: You finish the quest yet? [yes/no] ")
                if done=="yes":
                    if inv #IF INV CONTAINS QUEST ITEMS
                        global gold
                        gold+=500
                        print "Chef Bill: Great Job! Here's $500 for your troble..."
                        print "Reward of 100xp!"
                        global exp
                        global level
                        exp+=100
                        if exp>=100:
                            print "Level up! \n You are now level"+level+"."
                            level=2
    I need this to see if the player has meet the requirements to complete the quest.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    I see no variable inventory here.

    There is an inv here.
     
  3. Nsutton thread starter macrumors member

    Nsutton

    Joined:
    Dec 29, 2009
    Location:
    6 Feet Under
    #3
    My bad, I meant inv....
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    This doesn't use a class, and is probably poor in a number of ways. I don't know python. For the specific question you had, the "in" keyword is what you're looking for.

    Code:
    exp = 0
    gold = 0
    level = 1
    
    def visit_tavern(current_quest,inv):
    	if current_quest=="Chef's Helper":
    		done=raw_input("Chef Bill: You done? [yes/no]")
    		if done=="yes":
    			if "theGoldenSpoon" in inv:
    				inv.remove("theGoldenSpoon")
    				global gold
    				gold+=500
    				print "Chef Bill: Great! Here's some money"
    				print "Reward of 100xp"
    				global exp
    				global level
    				exp+=100
    				if exp>=100:
    					level=level+1
    					exp=exp%100
    					print "Level up! \n Now: " + str(level) +"."
    
    visit_tavern("Chef's Helper",["anApple","aDustyMap","someFalseTeeth","theGoldenSpoon","someString"])
    
     
  5. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #5
    I see what you did there.
     
  6. Nsutton thread starter macrumors member

    Nsutton

    Joined:
    Dec 29, 2009
    Location:
    6 Feet Under
    #6
    ok, thank you.

    besides it CAN have a class. why should i not have one? i use them organize things.. like class menu contains: menu,exit ,start
     
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    I didn't say not to use classes, i meant to indicate that i was pulling the code out and not using a class because i didn't know how. On the contrary, you should definitely use OOP and organize your code in classes, i just don't know python so to get a working example i decided to forego it in this case.

    -Lee
     
  8. Nsutton thread starter macrumors member

    Nsutton

    Joined:
    Dec 29, 2009
    Location:
    6 Feet Under
    #8
    my bad, i thought you were calling me out on my use of classes. you were right.
     

Share This Page