如何在初始余额上添加金额后打印新的余额?



我正在学习Python,并使用一个简单的ATM代码。我已经测试过了,一切都在下游工作——我的意思是:

  1. 当类初始化时,我有几个选项-余额,存款,取款,退出。
  2. 当我运行Balance时,我收到设置的金额。

2.1。我选择"存款"——它显示这个人账户中的新金额

2.2。当我使用取款时,我也得到了正确的金额

  1. 问题-当我存款然后键入余额时,我将获得用户的初始余额-这是预期的。我如何更改代码,以便在存款后选择余额显示新的余额?

这是否可能在不使代码复杂化的情况下执行?

代码:


class User:
def __init__(self):
self.fname = input('Enter your first name: ')
self.lname = input('Enter your last name: ')
self.age = input('Enter your age: ')
def user_details(self):
print('Details:')
print(f"First Name: {self.fname}")
print(f"Last Name: {self.lname}")
print(f"User age: {self.age}")
def deposit_money(self):
self.deposit_amount = 100
return self.deposit_amount

def withdraw_money(self, withdraw_amount):
self.withdraw_amount = withdraw_amount
return self.withdraw_amount

class ATM:

atm_balance = 10000
def __init__(self):
self.machine_balance = self.atm_balance

def user_bank_balance(self):
self.user_balance = 300
print ('Your current balance is ${}'.format(self.user_balance))
def deposit_atm(self, user):
self.total_savings = 0
deposit_m = float(input('How much do you want to deposit? '))
if deposit_m > user.deposit_money():
print('You do not have enough money to deposit')
elif deposit_m == user.deposit_money():
print('Amount deposited: ${}'.format(deposit_m))
self.total_savings = self.user_balance + deposit_m
print('Total amount in your account: ${}'.format(self.total_savings))
def withdraw_atm(self):
savings_left = 0
sum_to_withdraw = float(input('How much do you want to withdraw? '))
if self.atm_balance > sum_to_withdraw and self.user_balance > sum_to_withdraw:
savings_left = self.total_savings - sum_to_withdraw
print("You have withdraw {}".format(sum_to_withdraw))
print('You balance is {}'.format(savings_left))
elif self.atm_balance > sum_to_withdraw and self.user_balance < sum_to_withdraw:
print('Daily limit eceeded')
else:
print('ATM out of service')

class ATMUsage:
@classmethod
def run(cls):

print('Bulbank ATM')
instructions = print(""" 
Type 'Balance' to check your current balance,
Type  'Deposit' to deposit amount into your account,
Type  'Withdraw' to withdraw from your account,
Type  'Exit' to exit from your account,
""")
active = True
user1 = User()
atm1 = ATM()
user1.user_details()
while active:
selection = input("What would you like to do: 'Balance', 'Deposit', 'Withdraw', 'Exit': ") 
if selection == 'Balance'.lower():
atm1.user_bank_balance()
elif selection == 'Deposit'.lower():
atm1.deposit_atm(user1)
elif selection == "Withdraw".lower():
atm1.withdraw_atm()
elif selection == 'Exit'.lower():
print('Thanks for passing by. Have a good one!')
break
else:
print('Wrong selection. Please, try again')

ATMUsage.run()

这是因为每次调用user_bank_balance方法时,都将user_balance属性设置为300。所以不管你对user_balance做了什么更新,只要你调用user_bank_balance方法,你都会得到300

class ATM:
atm_balance = 10000
def __init__(self):
self.machine_balance = self.atm_balance
self.user_balance = 300
def user_bank_balance(self):
print ('Your current balance is ${}'.format(self.user_balance))

最新更新