Ask coding questions

← Back to all posts
classes + functions help needed
CookieSnowOwl (41)

HELP! IT DOESNT WORK PLZ HELP THXTHX

Answered by Coder100 (16802) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (16802)

you need to do it like self.chickenlow if you want to store properties to the class.

Like this:

class AI:
  def __init__(self):
    self.chickenlow = randint(5, 30)
  def target(a, self):
    if a < self.chickenlow:
      print("no trade. Too low. I not go with trade >:(")
    else:
      self.fitness = a / -1
      self.chickenlow += 3 * self.fitness
      print("Purchase done!")

please, take some time to learn about classes fully.

Coder100 (16802)

you did it wrong. Self has to be the FIRST argument, or else it will not be applied.

from random import randint
class AI:
  def __init__(self):
    self.chickenlow = randint(5, 30)
  def target(self, a):
    if a < self.chickenlow:
      print("no trade. Too low. I not go with trade >:(")
    else:
      self.fitness = a / self.chickenlow - 1
      self.chickenlow += 3 * self.fitness
      print("Purchase done!")
Meowzer = AI()
while True:
  x = int(input("dollars to buy chicken?\n"))
  Meowzer.target(x)

@MatthewHou