首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建一个名为“帐户”的对象,用于银行业务

创建一个名为“帐户”的对象,用于银行业务
EN

Stack Overflow用户
提问于 2022-07-12 17:51:41
回答 1查看 41关注 0票数 0

  1. 创建一个名为Account的对象。这将用于银行system.
  2. Initialize帐户,该帐户有三个数据作为输入: Firstname、Lastname和初始deposit.
  3. Create 4附加成员函数:存款、取款、费用计算,如果帐户中的总金额小于1000美元,则费用计算为每月10美元。利息设定为3%。

我已经让代码工作了一次,但是所有变量都是整数,这意味着它们中没有一个是小数。这就是我现在所拥有的,我一直在犯错误:

代码语言:javascript
复制
input = 'John Doe 4893.27'

account = str(input)
account = account.split()

first_name = account[0]
last_name = account[1]
i_deposit = account[2]

i_deposit = float(i_deposit)
i_deposit = '{:.2f}'.format(i_deposit)

new_amount = 0
fee = 0
d = 0
w = 0


def Deposit(d):
    d = float(d)
    d = '{:.2f}'.format(d)
    d = bin(d)
    new_amount = bin(i_deposit) + d
    new_amount =  bin(new_amount)
    return bin(new_amount)


def Withdraw(w):
    w = float(w)
    w = '{:.2f}'.format(d)
    w = bin(w)
    new_amount = bin(i_deposit) + w
    new_amount = bin(new_amount)
    return bin(new_amount)


def FeeCalc(money):
    money = float(i_deposit)
    if money <= 1000:
        fee = 10
    else:
        fee = 0
    fee = '{:.2f}'.format(fee)
    return fee

i = 0
def Interest(i):
    i = float(i_deposit)
    i = int(i)
    i = i * .03
    i = '{:.2f}'.format(i)
    return i

dep_amount = Deposit(d)
wit_amount = Withdraw(w)
net_amount =  sum(dep_amount, wit_amount)
new_amount = int(new_amount)
int_fee = Interest(i)

Withdraw(276.84)

print(first_name, last_name, 'account information:', '\nInitial deposit: $', i_deposit, '\nNew balance: $', new_amount, '\nFee: $', fee, '\nInterest: $', int_fee)

最近我似乎无法修复的错误是:

代码语言:javascript
复制
Traceback (most recent call last):
  File "/Users/shardae/PycharmProjects/HW1/main.py", line 55, in <module>
    dep_amount = Deposit(d)
  File "/Users/shardae/PycharmProjects/HW1/main.py", line 23, in Deposit
    d = bin(d)
TypeError: 'str' object cannot be interpreted as an integer

Process finished with exit code 1
EN

回答 1

Stack Overflow用户

发布于 2022-07-12 19:03:27

当您使用“{:.2F}‘.format(Some_variable)”函数时,实际上是将指定值的任何变量转换为字符串。我不相信那是你想要的。在此之后,让我对您的程序进行一些修改,以保持代码的精神。

代码语言:javascript
复制
input = 'John Doe 4893.27'

account = str(input)
account = account.split()

first_name = account[0]
last_name = account[1]
i_deposit = float(account[2])

def Deposit(amount, d):
    amount = amount + d
    return amount

def Withdraw(amount, w):
    w = (int(w) * 100) / 100    # Example of insuring precision to two decimal places
    amount = amount - w
    return amount

def FeeCalc(money):
    fee = 0
    if money <= 1000.00:
        fee = 10
    return fee

def Interest():
    i = float(i_deposit)
    i = int(i)
    i = i * .03
    return i

new_amount = i_deposit              # Set the work field to the initial deposit balance

int_fee = Interest()                # Calculate interest and add it to the balance

new_amount += int_fee               

new_amount = Withdraw(new_amount, 276.84)   # Calculate the new balance once the withdrawal amount is applied

fee = FeeCalc(new_amount)               # Subtract any bank fee if the balance has dropped below $1000.00

new_amount -= fee

print(first_name, last_name, 'account information:', '\nInitial deposit: $', '{:.2f}'.format(i_deposit), '\nNew balance: $', '{:.2f}'.format(new_amount), '\nFee: $', fee, '\nInterest: $', int_fee)

代码仍然使用浮点变量。但是,只有在打印输出中才会发生两个小数点的特定格式设置。

继续回顾这些调整,看看这是否满足了你的解决方案的精神。

希望这能有所帮助。

致以问候。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72956490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档