0

I want to have a instance, which the user can access using an input()statement. Here are my three instances

water1 = waterType("squirtle", 50)
grass1 = grassType("bulbasaur", 50)
fire1 = fireType("charmander", 50)

Here's what I mean

class waterType(Pokemon):
  """creates a water pokemon"""
  pokemon_type = "water"
  damage = 10
  def __init__(self, name, hp):
    self.name = name
    self.hp = hp
  def growl(self):
    print("Splish Splosh")
  def attack(self):
    enemy = input("Who do you want to attack?")  <--- over here, I would enter "fire1"
    """attribute error occurs here"""
    if (enemy.pokemon_type == "fire"):
      global damage
      water1.damage = water1.damage*2
      enemy.hp = enemy.hp - water1.damage
      return enemy.hp
      print ("It was super effective!")
      print (enemy.hp)

If I take all the "enemy" and replace it with "fire1", everything works perfectly. However, then I would need to manually input every single fire-type instance.

If it is not possible to assign an instance to an input, is there any way to fix my code?

1 Answer 1

1

What's happening right now is that you are assigning enemy to the string "fire1", rather than the variable.

This could be achieved as such:

enemy = eval(input("Who do you want to attack?"))

This should then reference the actual object called 'fire1'. Let me know if you need further help!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this is exactly what I wanted!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.