The code seems to be intact, I don’t know what’s wrong. Here is the error:

Traceback (most recent call last): File "python", line 23, in <module> NameError: name 'money' is not defined 

Here is the code:

 class School: def __init__(self,money,pupils,averageCost,equipment): self.money = money self.pupils = pupils self.averageCost = averageCost self.equipment = equipment def count(self,money,pupils,averageCost,equipment): x = 0 while x > -1: x += 1 self.money += averageCost * pupils self.equipment -= 1 return self.money return self.pupils return self.averageCost return self.equipment school_1 = School(10000,80,250,15000) school_1.count(money,pupils,averageCost,equipment) 
  • variables money,pupils,averageCost,equipment not declared and not initialized before the call: school_1.count(money,pupils,averageCost,equipment) - MaxU
  • @MaxU and what about init ? - Michael
  • when you call school_1.count(money,pupils,averageCost,equipment) you refer to external (not related to class variables) variables - MaxU
  • one
    @ Mikhail, why does count accept these variables at all? He can get them through self , and they are absolutely not needed in the function arguments - andreymal
  • 2
    @Michael, never, never use 1 or 2 spaces for indents. All right, use, but carefully. Also, you have indentation problems in the code, so it does not work, please correct. Also, you have an error due to several returns - gil9red

1 answer 1

 class School: def __init__(self,money,pupils,averageCost,equipment): self.money = money self.pupils = pupils self.averageCost = averageCost self.equipment = equipment def count(): x = 0 while x > -1: x += 1 self.money += self.averageCost * self.pupils self.equipment -= 1 return (self.money, self.pupils, self.averageCost, self.equipment) school_1 = School(10000,80,250,15000) (money,pupils,averageCost,equipment) = school_1.count() 

Corrected the code except for one, incomprehensible to me details.

 while x > -1: x += 1 

This is an infinite loop, correct the condition