'How to use classes to create attacks in python

I am trying to make it so that the user can input the name of another pokemon class so that I can use the attack function. Is there any way I could do this from user input? It seems as though even when I put in a valid name it returns an error. I would love to know as to how I can do this so that I can finish my game.

import random 



class Pokemon(object): 
  def __init__(self, name, level, pokemon_type, max_health, current_health, is_knocked_out): 
    self.name = name 
    self.level = level
    self.pokemon_type = pokemon_type
    self.current_health = current_health
    self.max_health = max_health 
    self.is_knocked_out = is_knocked_out
   


#knock out function 
  def knock_out(self): 
    self.is_knocked_out = True  
    print(self.name + " is now knocked out")
  
  #lose health func, takes in one parameter which is damage
  def lose_health(self, amount):
    if amount > self.current_health: 
      print(self.name + " has been knocked out and now has 0 hp") 
      self.current_health = 0
      self.is_knocked_out = True 
    else:
      self.current_health -= amount 
      print(self.name + " now has " + str(self.current_health) + " HP")


#hp regen function, takes in one parameter which is the amount of hp to be regained, wont allow you to go above the max  

  def regain_health(self, amount): 
    if amount > self.max_health: 
      print("You cannot heal past your max HP")
    else: 
      self.current_health += amount 
      print(self.current_health)


      #function to revive dead mons 
  def revive(self): 
    if self.is_knocked_out == True: 
        self.is_knocked_out = False 
        print(self.name + " has been revived and half of his hp has been restored!")
        self.current_health = self.max_health / 2
    else: 
        print("This pokemon is still alive ")
        return
        #attack function, in progress 
  def attack(self, pokemon_name, move_number): 
      if self.current_health > 0 and self.is_knocked_out == False: 
          if move_number == 1: 
              pokemon_name.lose_health(random.randint(1,150)) #Here is where the issue is, how can I make user input determine the name of the pokemon which is being attacked and sequentially make it lose HP? 
              print(pokemon_name + " was attacked")
          elif move_number == 2: 
              pass 
          elif move_number == 3: 
              pass 
          elif move_number == 4: 
              pass 
          else: 
               print("You are knocked out!")

Lucario = Pokemon("Lucario", 12, "fighting", 200, 200, False)

Blastoise = Pokemon("Blastoise", 12, "water", 200,200, False)

Blastoise.attack("Lucario", 1)


Solution 1:[1]

just use:

if move_number == 1: 
    globals()[pokemon_name]

what is globals()? it's a built-in function that load all global variable (predefined or user-defined), it's a dictionary

data = 5
globals()["data"]

every variable you defined, it will be added to globals() as

 globals()[name] = value

Solution 2:[2]

  def attack(self, pokemon, move_number): 
      if self.current_health > 0 and self.is_knocked_out == False: 
          if move_number == 1: 
              pokemon.lose_health(random.randint(1,150)) #Here is where the issue is, how can I make user input determine the name of the pokemon which is being attacked and sequentially make it lose HP? 
              print(pokemon.name + " was attacked")
          elif move_number == 2: 
              pass 
          elif move_number == 3: 
              pass 
          elif move_number == 4: 
              pass 
          else: 
               print("You are knocked out!")


Characters = dict()

Characters["Lucario"] = Pokemon("Lucario", 12, "fighting", 200, 200, False)

Characters["Blastoise"] = Pokemon("Blastoise", 12, "water", 200,200, False)

Characters["Blastoise"].attack(Characters["Lucario"], 1)

you also can use dictionary. what is dictionary? a dictionary is a collection of named variable, like list, but you can add to it by name...

how to define dictionary?

Characters = dict()

and add your variables:

Characters["<name>"] = value

like:

Characters["Blastoise"] = Pokemon("Blastoise", 12, "water", 200,200, False)

and use variables:

Characters["<name>"].<your-method>

like:

Characters["Blastoise"].attack(Characters["Lucario"], 1)

just one point: if you use dictionary, so you send variable (Pokemon object, not it's name), so attack's second parameter is pokemon and use:

print(pokemon.name + " was attacked")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2