can you guys please shorten this code for me...
I need to do the same thing for all 8 potions

def fightItems(yourHp, yourHpMax, heartPotion, megaHeartPotion,bloodXHealer, duelBloodXXHealer,energyYPotion, rainbowBasicHealer, attackXXXPotion, defenceXXXPotion):
                                    while karidQuest1chooseFightOptiion == "2":

                                        print '1. Heart Potion(',heartPotion,'remaining)'
                                        time.sleep(0.5)
                                        print '2. Mega Heart Potion(' , megaHeartPotion ,'remaining)'
                                        time.sleep(0.5)
                                        print '3. Blood X Healer(', bloodXHealer , 'remaining)'
                                        time.sleep(0.5)
                                        print '4. Duel Blood XX Healer(', duelBloodXXHealer, 'remaining'
                                        time.sleep(0.5)
                                        print '5. Energy Y Potion(', duelBloodXXHealer, 'remaining'
                                        time.sleep(0.5)
                                        print '6. Rainbow Basic Healer(', rainbowBasicHealer, 'remaining'
                                        time.sleep(0.5)
                                        print '7. Attack XXX Potion(', attackXXXPotion , 'remaining'
                                        time.sleep(0.5)
                                        print '8. Defence XXX Potion (', defenceXXXPotion, 'remaining'
                                        time.sleep(0.5)
                                        print '9. Exit'
                                        print 'which do you use/pick'
                                        karidFightChooseItem = raw_input()
                                        if karidFightChooseItem == "9":
                                            break
                                        else:
                                            if karidFightChooseItem == "1":
                                                if heartPotion > 0:
                                                    heartPotion -= 1
                                                    yourHp += 25
                                                    yourHp = getMaxHp(yourHp)
                                                    print 'Heart Potion:', heartPotion
                                                    time.sleep(0.25)
                                                    print 'Your Hp:' ,yourHp

                                                else:
                                                    print 'you have 0 heart potions'

Recommended Answers

All 5 Replies

Well I definatly applaude your effort the way you are coding this project, although I think by now you have the just of functions. And your code at this point is probably pretty confusing even to you at times. Look at this http://www.wellho.net/resources/ex.php4?item=y103/python_switch_case for some idea of how to shorten alot of it up. And to really get it shortened up try studying up on classes.

I've read the URL and I am using switches and dictionaries, but I do not know how to apply it in the code above. Help please?
~JEFF~

Honestly I'm not sure if the method in the url will work for your case, but I thought it showed some nice elements of object oreinted thinking that might help your code easier to understand. Like I said though you should check out classes they will make your project much easier to understand, and easier to keep track of variables and such things. Not exactly sure this matches with what you are doing, but here is an example of what I'm saying.

class Potion(object):
    def __init__(self, name, healAmount, postionleft):
        self.name = name               #name attribute so we can have many type of potion
        self.potionleft = postionleft  #keep track of how many potions are available
        self.healAmount = healAmount   #set amount that certain potion can heal

    def use(self, charhp):    #define a method of what to do if a potion is used
        self.potionleft -= 1  #need to take away one potion
        if self.potionleft == 0:   #if we have no more potions print so and return charecters hp
            print "you have no %s left" %self.name
            return charhp
        return charhp + self.healAmount #otherwise add the amount to heal by


charhp = 30 #start amount hp
spl = 5     #start amount smallpotions
lpl = 2     #start smount largepotions
smallpotion = Potion("smallpotion", 5, spl)  #create instance of Potion named smallpotion
largepotion = Potion("largepotion", 10, lpl) #create instance of Potion named largepotion

print "my hp is", charhp
charhp = smallpotion.use(charhp)  #use a smallpotion
charhp = smallpotion.use(charhp)
charhp = smallpotion.use(charhp)
charhp = smallpotion.use(charhp)
charhp = smallpotion.use(charhp)
print "now my hp is", charhp

Hope this will give you an idea why classes would make more sence for this. They allow you to think of objects as objects and code it that way. When I think of a potion I don't think about functions and blah blah blah. I think what is a potion, and what can it do? Classes give us a way to define it that way with our code.

I never thought of using classes, but I don't think it would shorten up the coding much

never mind the last post btw
now my coding is like this!!!

def fightItems(yourHp, yourHpMax, heartPotion, megaHeartPotion,bloodXHealer, duelBloodXXHealer,energyYPotion, rainbowBasicHealer, attackXXXPotion, defenceXXXPotion):
            while karidQuest1chooseFightOption == "2":

                print '1. Heart Potion(',heartPotion,'remaining)'
                time.sleep(0.5)
                print '2. Mega Heart Potion(' , megaHeartPotion ,'remaining)'
                time.sleep(0.5)
                print '3. Blood X Healer(', bloodXHealer , 'remaining)'
                time.sleep(0.5)
                print '4. Duel Blood XX Healer(', duelBloodXXHealer, 'remaining)'
                time.sleep(0.5)
                print '5. Energy Y Potion(', duelBloodXXHealer, 'remaining)'
                time.sleep(0.5)
                print '6. Rainbow Basic Healer(', rainbowBasicHealer, 'remaining)'
                time.sleep(0.5)
                print '7. Infinity Potion(', infinityPotion , 'remaining)'
                time.sleep(0.5)
                print '8. Infinity X Potion (', infinityXPotion, 'remaining)'
                time.sleep(0.5)
                print '9. Exit'
                print 'which do you use/pick'
                karidFightChooseItem = raw_input()
                if karidFightChooseItem == "9":
                    break
                else:
                    if karidFightChooseItem == "1":
                        yourHp = heartPotionZ.use(yourHP)
                        print 'you have',yourHp,'now'
                    elif karidFightChooseItem == "2":
                        yourHp = megaHeartPotionZ.use(yourHp)
                        print 'you have',yourHp,'now'
                    elif karidFightChooseItem == "3":
                        yourHp = bloodXHealerZ.use(yourHp)
                        print 'you have',yourHp,'now'
                    elif karidFightChooseItem == "4":
                        yourHp = duelBloodXXHealerZ.use(yourHp)
                        print 'you have',yourHp,'now'
                    elif karidFightChooseItem == "5":
                        yourHp = energyYPotionZ.use(yourHp)
                        print 'you have',yourHp,'now'
                    elif karidFightChooseItem == "6":
                        yourHp = basicRainbowHealerZ.use(yourHP)
                        print 'you have',yourHp,'now'
                    elif karidFightChooseItem == "7":
                        yourHp = infinityPotionX.use(yourHp)
                        print 'you have',yourHp,'now'
                    elif karidFightChooseItem == "8":
                        your = infinityXPotionZ.use(yourHp)
                        print 'you have',yourHp,'now'
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.