python 银行账户交易_Python | 根据交易计算银行帐户的净额

python 银行账户交易

Given a few transactions (deposit, withdrawal), and we have to compute the Net Amount of that bank account based on these transactions in Python.

给定几笔交易(存款,提款),我们必须根据这些交易在Python中计算该银行帐户的净额。

Example:

例:

    Input:
    Enter transactions: D 10000
    Want to continue (Y for yes): Y
    Enter transaction: W 5000
    Want to continue (Y for yes): Y
    Enter transaction: D 2000
    Want to continue (Y for yes): Y
    Enter transaction: W 100
    Want to continue (Y for yes): N

    Output:
    Net amount: 6900

Logic:

逻辑:

  • For this program, we will input transactions (till user’s choice is "Y") with type ("D" for deposit and "W" for withdrawal) and the amount – for that we will computer it in an infinite loop while True:

    对于此程序,我们将输入类型(“ D”代表存款,“ W”代表提款)和金额的交易(直到用户选择是“ Y”)和金额-为此,我们将在无限循环中将其计算为True:

  • Transactions will be like "D 2000" that means Deposit 2000, so there are two values needs to be extracted 1) "D" is the type of transaction and 2) 2000 is the amount to be deposited.

    交易将类似于“ D 2000”,这表示存款2000,因此需要提取两个值1)“ D”是交易的类型,而2)2000是要存入的金额。

  • A transaction input will be string type- convert/split the values to the list which is delimited by the space. For this, we use string.split() method.

    事务输入将是字符串类型-将值转换/分割到由空格分隔的列表。 为此,我们使用string.split()方法。

  • Now, the values will be in the list, the first value is the type – which is in string format and the second value is the amount – which is in also string format, we need to convert the amount in integer format. So we will convert its type (example code: amount = int(list[1])).

    现在,这些值将在列表中,第一个值是类型-字符串格式,第二个值是数量-也是字符串格式,我们需要将数量转换为整数格式。 因此,我们将转换其类型(示例代码: amount = int(list [1]) )。

  • After that, based on the transaction type check the conditions and adding/subtracting the amount from the net amount.

    之后,根据交易类型检查条件并从净额中减去/减去金额。

  • After that, ask for the user for the next transaction and check the weather, if the user’s choice is not "Y" or "y", break the loop and print the net amount.

    之后,要求用户进行下一次交易并检查天气,如果用户选择的不是“ Y”或“ y”,则中断循环并打印净额。

Program:

程序:

# computes net bank amount based on the input
# "D" for deposit, "W" for withdrawal 

# define a variable for main amount 
net_amount = 0

while True:
	# input the transaction 
	str = raw_input ("Enter transaction: ")

	# get the value type and amount to the list 
	# seprated by space
	transaction = str.split(" ")

	# get the value of transaction type and amount 
	# in the separated variables
	type = transaction [0]
	amount = int (transaction [1])

	if type=="D" or type=="d":
		net_amount += amount
	elif type=="W" or type=="w":
		net_amount -= amount
	else:
		pass

	#input choice 
	str = raw_input ("want to continue (Y for yes) : ")
	if not (str[0] =="Y" or str[0] =="y") :
		# break the loop 
		break

# print the net amount
print "Net amount: ", net_amount

Output

输出量

    Enter transaction: D 10000
    want to continue (Y for yes) : Y
    Enter transaction: W 5000
    want to continue (Y for yes) : Y
    Enter transaction: D 2000
    want to continue (Y for yes) : Y
    Enter transaction: W 100
    want to continue (Y for yes) : N
    Net amount:  6900


翻译自: https://www.includehelp.com/python/compute-the-net-amount-of-a-bank-account-based-on-the-transactions.aspx

python 银行账户交易

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Python 银行账户类的示例代码: ```python class Account: def __init__(self, name, balance): self.name = name self.balance = balance def deposit(self, amount): self.balance += amount def withdraw(self, amount): if self.balance >= amount: self.balance -= amount else: print("Insufficient balance!") def get_balance(self): return self.balance def __str__(self): return "Account name: {}, Balance: {}".format(self.name, self.balance) ``` 以上代码创建了一个名为 `Account` 的类,它包含了以下几个方法: - `__init__(self, name, balance)`:构造方法,用于初始化账户的名称和余额。 - `deposit(self, amount)`:存款方法,用于将指定金额 `amount` 存入账户余额中。 - `withdraw(self, amount)`:取款方法,用于从账户余额中取出指定金额 `amount`。 - `get_balance(self)`:查询余额方法,用于返回当前账户的余额。 - `__str__(self)`:字符串表示方法,用于返回账户的名称和余额的字符串表示。 使用该类可以创建多个银行账户,并进行存款、取款、查询余额等操作。例如: ```python # 创建一个名为 "Alice" 的账户,并初始化余额为 1000 元 alice_account = Account("Alice", 1000) # 存入 500 元 alice_account.deposit(500) # 取出 200 元 alice_account.withdraw(200) # 查询余额 print(alice_account.get_balance()) # 输出账户信息 print(alice_account) ``` 输出结果如下: ``` 1300 Account name: Alice, Balance: 1300 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值