# Quidditch
class Player:
def __init__(self, name, base_energy):
"""
Players have a name, and begin with base_energy.
"""
self.name = name
self.base_energy = base_energy
def energy(self):
return self.base_energy
class Beater(Player):
role = "bludgers"
def energy(self, time):
"""
Returns the amount of energy left after playing for time minutes.
After playing for time minutes, Beaters lose their base energy level
divided by the number of minutes. If time is 0, catch the ZeroDivisionError
and print "You can't divide by zero!" instead.
>>> fred = Beater("Fred Weasley", 640)
>>> fred.energy(40)
624.0
>>> fred.energy(0)
You can't divide by zero!
"""
self.time = time
try:
self.energy = self.base_energy - (self.base_energy/(self.time))
return self.energy
except Exception as error:
print("You can't divide by zero!")
嗨,我正在做作业,我想使用异常来避免零除错误,但是,我遇到了另一个错误,该错误表明“ float”对象不可调用。我相信由于self.energy = self.base_energy - (self.base_energy/(self.time))
这行代码而发生了错误。但是我不知道如何解决它。请帮忙!