2017-10-12 139 views
0
class UniversityAthletics(): 
    def __init__(self, university_name, sport): 
     self.name = university_name 
     self.sport = sport 
     self.tickets_sold = 10 

    def tickets_sold(self): 
     """This just tells you how many tickets have been sold""" 
     print(str(self.tickets_sold) + " tickets have been sold") 

    def describe_athletics(self): 
     print(self.name.title() + " " + self.sport.title()) 

    def in_season(self): 
     print(self.sport.title() + " is in season.") 

    def set_tickets_sold(self, numticket): 
     """This sets the number of tickets that have been sold""" 
     self.tickets_sold = numticket 

    def increment_tickets_sold(self, moretickets): 
     """This increments the number of tickets sold""" 
     self.tickets_sold += moretickets 

athletics = UniversityAthletics('salisbury university', 'soccer') 
athletics.set_tickets_sold(20) 
athletics.tickets_sold() 
athletics.increment_tickets_sold(500) 
athletics.tickets_sold() 

我試圖讓那臺改變門票「increment_tickets_sold」,但數量已售出「set_tickets_sold」門票的數量,那麼屬性的屬性,當我嘗試設置數量的票,然後調用方法'tickets_sold()'我不斷收到錯誤。修改屬性的值通過方法

Traceback (most recent call last): File "E:/WorWic/IntroToProgramming/chapter9/University Athletics part 2.py", line 29, in athletics.tickets_sold() TypeError: 'int' object is not callable

我做錯了什麼?

+0

您正在用'tickets_sold'變量覆蓋'ticks_sold()'方法。改變他們的名字。例如,將變量更改爲'_tickets_sold' – GWW

+0

您在同一個對象上使用了'tickets_sold'名稱作爲兩個不同的事物。不要這樣做。 – user2357112

回答

1

您的問題是,tickets_sold既是屬性又是函數名稱。它獲取屬性並調用它,而不是函數。我建議將你的函數重命名爲別的東西。

+0

謝謝,我甚至沒有意識到這一點 – CholoBoy