0

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)
Ryan
  • 19
  • 3
  • Use a dictionary. Mandatory reading https://nedbatchelder.com/blog/201112/keep_data_out_of_your_variable_names.html – buran Apr 27 '22 at 17:21
  • Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) Basically it's the same problem, even wose because you don't use loop, but create binch of names – buran Apr 27 '22 at 17:22
  • And technically `Lucaro` and `Blastoise` objects are instances of class `Pockemon`, not "class" – buran Apr 27 '22 at 17:24

2 Answers2

0

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
MoRe
  • 2,296
  • 2
  • 3
  • 23
  • if you dont mind could you explain exactly how this works? I have never seen the globals() method used before. – Ryan Apr 27 '22 at 17:13
  • @RyanParker I edited my post, hope be helpfull – MoRe Apr 27 '22 at 17:18
  • Please, don't suggest using `globals()` for this – buran Apr 27 '22 at 17:20
  • @buran why do you say that? – Ryan Apr 27 '22 at 17:24
  • @RyanParker, you should create a dict for this, not to mess with `globals()`. That's the proper way to do it. – buran Apr 27 '22 at 17:26
  • @buran ok, I now that `global` is not best solution because an user can access to other variable and it's harmfull and it's better to use dictionary, but I assume that he dont want to use dictionary and so, it is only way – MoRe Apr 27 '22 at 17:26
  • I will figure out how to use dictionaries to do it, any other resources you have? – Ryan Apr 27 '22 at 17:27
  • Why do you "assume" anything about OP intentions? Most likely they just don't know how they should handle it. – buran Apr 27 '22 at 17:27
  • @buran do you have any more resources to point me in the right direction? I read the above resources and i'm still a little confused. – Ryan Apr 27 '22 at 17:31
0
  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")
MoRe
  • 2,296
  • 2
  • 3
  • 23