class Accounting: def _init_(self,income,pay): self.i=income self.p=pay def surplus(self): incomes=sum(self.i) pays=sum(self.p) sur=incomes-pays print("一年省余{}".format(sur)) def average(self): income_avg=sum(self.i)/len(self.i) pay_avg=sum(self.p)/len(self.p) print("平均收入{}\n平均支出{}".format(income_avg,pay_avg)) li1=[5000,5200,5500,5300,5000,5600,5100,5400,5200,5000,5400,5300] li2=[1000,900,1100,500,800,2000,700,1000,1000,900,1100,1000] a1=Accounting(li1,li2) a1.surplus() a1.average() 能帮我看一下这些代码哪里出错了
时间: 2023-12-04 11:02:08 浏览: 72
surplus:适用于S.js应用程序的高性能JSX Web视图
这段代码有两个问题:
1. `_init_` 应该是 `__init__`,是双下划线。
2. 在 `__init__` 方法中,应该将 `income` 和 `pay` 赋值给实例变量 `self.i` 和 `self.p`。所以 `__init__` 应该这样写:
```python
def __init__(self, income, pay):
self.i = income
self.p = pay
```
修改后的代码如下:
```python
class Accounting:
def __init__(self, income, pay):
self.i = income
self.p = pay
def surplus(self):
incomes = sum(self.i)
pays = sum(self.p)
sur = incomes - pays
print("一年省余{}".format(sur))
def average(self):
income_avg = sum(self.i) / len(self.i)
pay_avg = sum(self.p) / len(self.p)
print("平均收入{}\n平均支出{}".format(income_avg, pay_avg))
li1 = [5000, 5200, 5500, 5300, 5000, 5600, 5100, 5400, 5200, 5000, 5400, 5300]
li2 = [1000, 900, 1100, 500, 800, 2000, 700, 1000, 1000, 900, 1100, 1000]
a1 = Accounting(li1, li2)
a1.surplus()
a1.average()
```
阅读全文