我正在尝试通过在 Python 上使用面向对象编程来解决“保持变化”问题。我做了一个类:
class BankAccount:
def __init__(self):
self.savings = 100
def savings (self, amount):
self.savings = self.savings + amount
return self.savings
def getSavings (self):
return self.savings
然后我制作了一个单独的文件,尝试从文件中取出数字,四舍五入,然后将差额存入储蓄账户。但是,当我要求节省并尝试使用变量来增加节省时,我不断收到一条错误消息,指出它需要是int
.
def main():
account1 = BankAccount()
file1 = open("data.txt","r")
s = 0 # to keep track of the new savings
for n in file1:
n = float(n) #lets python know that the values are floats
z= math.ceil(n) #rounds up to the whole digit
amount = float(z-n)
s = int(amount + s)
x = (account1.savings(s)) # <<this is where the error occurs