下面是我得到的錯誤:類型錯誤:浮子需要
Running...done.
Traceback (most recent call last):
File "main.py", line 13, in <module>
print('Alice:\n Net pay: $%7.2f' % alice.calculate_pay())
TypeError: a float is required
奇怪的錯誤,我有什麼做不知道。我顯然是從Python開始的,因此遠程bug跟蹤很容易,減去這一點。這裏的代碼如下:
class Employee:
def __init__(self):
self.wage = 0
self.hours_worked = 0
def calculate_pay(self):
self.calculate_pay = (self.wage * self.hours_worked)
alice = Employee()
alice.wage = 9.25
alice.hours_worked = 35
print('Alice:\n Net pay: $%7.2f' % alice.calculate_pay())
bob = Employee()
bob.wage = 11.50
bob.hours_worked = 20
print('Bob:\n Net pay: $%7.2f' % bob.calculate_pay())
你有沒有嘗試代碼,而浮點數?因爲我認爲它也不行。請嘗試使用正常數字。像9-35。你的功能沒有回報 – GLHF 2014-12-08 03:39:09