python - Function not running correctly -
python - Function not running correctly -
i'm making python 2.7 text-based rpg , function fighting isn't running properly. here's code:
def attack(self): print "a %r appears! wants fight!" % (self.name) while (player.health > 0) or (self.health > 0): player.health = player.health - ( ( randint(0,5) ) + attack_multiplier(self.level) ) print "%r strikes! health downwards %r" %(self.name, player.health) try: player.weapon = (raw_input("what attack with? >>").lower) if (player.inventory.get(player.weapon) > 0) , (player.health > 0) , (self.health > 0): if weapon_probability() == "critical hit": self.health = self.health - (((randint(0,5))) + (attack_multiplier(weapon_levels.get(player.weapon))) * 2) print "critical hit!" elif weapon_probability() == "hit": self.health = self.health - ((((randint(0,5))) + (attack_multiplier(weapon_levels.get(player.weapon))))) print "hit!" elif weapon_probability() == "miss": self.health = self.health print "miss" print "enemy health downwards %r!" % (self.health) elif player.health <= 0: print "your health...it’s falling" break elif self.health <= 0: print "enemy vanquished!" break except valueerror: print "you don't have that" what see is:
'bat' strikes! health downwards 95 attack with? >>sword 'bat' strikes! health downwards 91 attack with? >>sword 'bat' strikes! health downwards 87 attack with? >>sword 'bat' strikes! health downwards 85 attack with? >>sword 'bat' strikes! health downwards 82 attack with? >> that keeps repeating , player.health keeps going downwards negatives. can't find error. function method on class, , player instance of class.
you storing method, not lowercased input string:
player.weapon = (raw_input("what attack with? >>").lower) because did not call str.lower() there. don't have str.lower method stored in player.inventory player.inventory.get(player.weapon) returns none instead.
because in python 2 can ordered relative other objects, test:
player.inventory.get(player.weapon) > 0 is false.
calling method should prepare @ to the lowest degree issue:
player.weapon = raw_input("what attack with? >>").lower() rather utilize player.inventory.get() (which returns default , masks problems, utilize player.inventory[player.weapon]. this'll throw keyerror show user doesn't have weapon, adjust exception handler grab it:
except keyerror: print "you don't have that" python class python-2.7
Comments
Post a Comment