Please help me with attack code for a text based adventure game - it is not working

Question:
I have been coding this for some time, but have only just hit the point that i must code the brutal murder of some Goblins! However, something in my code is wrong, not giving me errors but not working how it should. Plz help…

Here is the code in question. Please bear in mind that i am just trying to see my code working, and I will flesh it out and add better names at a later date.

player = Player(race=charrace, name=charname)
enemy = Enemy(xpdropped=0, name="Bobby", hp=10, defence=10, mana=10)
time.sleep(1)
gap(1)
player.explaintype()
gap(1)
while enemy.hp != 0:
	gap(1)
	action = input("Attack, Cast a Spell, Use an Item or Run: ").lower
	time.sleep(1)
	if action == "attack" or action == "atk":
		typewriter(f"You hit the {enemy.name} with a {player.weapon}.")
		time.sleep(1)
		player.hurtsomeonephysical(enemy)
	if action == "check":
		enemy.ihaveeyesonthetarget()
	gap(1)

Here is the hurt and eyes code:

def hurtsomeonephysical(self, target):
		if self.weapon.magic is True:
			if self.weapon.manause >= self.currentmana or self.weapon.manause == self.currentmana:
				typewriter(
					f"You do not have enough Mana to perform that action. The action requires {self.weapon.manause} Mana to use, but you only have {self.currentmana}.\n"
				)
				gap(1)
			else:
				self.currentmana -= self.weapon.manause
				self.castit = True
				damage = rand.randint(self.weapon.damagemin,self.weapon.damagemax) + self.strmod
				target.hp -= damage
				self.castit = False
		else:
			damage = rand.randint(self.weapon.damagemin,self.weapon.damagemax) + self.strmod
			target.hp -= damage

#and eyes which IS separate
	def ihaveeyesonthetarget(self): #i beg ignore the names
		typewriter(f"I am a {self.name}. I am at {self.hp} health, and my max health is {self.maxhp}. I am currently at {self.currentmana} out of {self.maxmana}. My defence score is {self.defence}. I will drop {self.xpdropped}xp.")

the result

_   _   ____ _     _       ____  _  _     _____ ____  ____ 
           / \ / \ / __// \   / \     /  _ \/ \/ \ |\/  __//  __\/ ___\
           | |_| ||  \  | |   | |     | | \|| || | //|  \  |  \/||    \
           | | | ||  /_ | |_/\| |_/\  | |_/|| || \// |  /_ |    /\___ |
           \_/ \_/ \___\\____/\____/  \____/\_/\__/  \____\\_/\_\\____/





What would you like your character to be called?
I will name my character Theo

An excellent choice!

And as for your race? You can have a choice between three:

Will you be an Elf: A majestic being, born of magical heritage - though not many know it - and swift on their feet.
Will you be a Human: A plain yet powerful being who excels at most things and fails at little.
Or might you be a Dwarf: A hardy, cave-dwelling creature who can withstand the harshest blows.


I will be a/an elf

My name is Theo. I am a/an Elf. I am at 9 health, and my max health is 18. My strength is 13 with a modifier of 1. My constitution is 12 with a modifier of 1. My mana score is 12 with a modifier of 2 and I am currently at 2 out of 2. My defence score is 15. I am level 1 with 0 xp. My inventory slots are/holding Empty, Empty, Empty. My armour/clothing is Simple Cloth on my chest, and Simple Cloth on my legs. I benefit from my elven heritage when casting spells.

Attack, Cast a Spell, Use an Item or Run: atk


Attack, Cast a Spell, Use an Item or Run: check


Attack, Cast a Spell, Use an Item or Run: atk

Incase it helps i also can add the player init in the comments

You are not calling the lower() method on the string… you are just referencing the method without executing it. This will cause the action to not getting set to the lowercase version of the user input as you expect.

You need to call the lower() properly, like:

while enemy.hp != 0:
    gap(1)
    action = input("Attack, Cast a Spell, Use an Item or Run: ").lower()  # You forgot the ()
    time.sleep(1)
    if action == "attack" or action == "atk":
        typewriter(f"You hit the {enemy.name} with a {player.weapon}.")
        time.sleep(1)
        player.hurtsomeonephysical(enemy)
    if action == "check":
        enemy.ihaveeyesonthetarget()
    gap(1)

Oh, by the way, what will you do if the user enter an invalid command?

2 Likes