简单的面向对象的、货币管理代码,用户可以在其中存入、提取、显示和退出余额。
class BankAccuount:
def __init__(self, name):
self.name = name
self.balance = 0
print('Hello! Welcome to the Deposit and Withdrawal Machine {}!'.format(self.name))
def deposit(self):
'''adds money to balance'''
amount = float(input('\nEnter amount to be deposited > '))
self.balance += amount
print(f'Amount deposited: {amount}')
self.display()
def withdrawal(self):
'''subtracts money from balance if funds are sufficient'''
amount = float(input('\nEnter amount to be withdrawn > '))
if amount > self.balance:
print('Insufficient balance')
else:
self.balance -= amount
print('Amount withdrawn:', amount)
self.display()
def display(self):
print(f'\nNet Available Balance is {self.balance}')
whats_your_name = input('What is your name? ')
consumer = BankAccuount(whats_your_name)
while True:
action = input('\nWould you like to deposit (D), withdraw (W), show balance (S), or exit (E)? ').upper()
if action in 'DW':
while True:
try:
if action == 'D':
consumer.deposit()
elif action == 'W':
consumer.withdrawal()
break
except ValueError:
print('Use numbers please')
elif action == 'S':
consumer.display()
else:
print(f'Thank you for your visit {whats_your_name}!')
break发布于 2019-09-19 20:47:17
我看到你的一些方法有一些文档字符串。但我没有看到您的BankAccount类或模块。你也应该包括这些。
您在类中接受存款和取款方法中的输入。这些方法应该只有一个功能:从帐户本身增减资金。您应该处理类之外的用户输入。
现在,所有用户输入代码都在程序的全局范围内处理。这段代码应该被定义为一个函数,由主保护程序调用(稍后会进入)。这将使您能够更好地构造代码,并根据需要将代码分成块。它还能让你更好地看到你的程序的总体情况。
我看到你在用"".format()和f""。你应该坚持一个,因为他们都在做同样的事情。就我个人而言,我喜欢专门使用f""。
如上所述,类之外的代码应该包含在主保护程序调用的函数中,或者包含在主保护程序本身中。
主要警卫:
if __name__ == '__main__':
... code stuff here ...模块中有一个主保护子句,使您既可以直接在模块中运行代码,也可以使用模块中来自其他模块的过程和类。如果没有主保护子句,启动脚本的代码将在模块导入时运行。
使用类型提示可以查看要传递给方法的变量类型,以及返回哪种类型变量的方法。我在你的类方法中添加了一些,看看你喜欢它们。它们会很有帮助。
"""
Module Docstring (a description of your program goes here)
"""
class BankAccount:
"""
BankAccount Class
"""
def __init__(self, name: str):
self.name = name
self.balance = 0
def deposit(self, amount: float) -> bool:
'''adds money to balance'''
if amount > 0:
self.balance += amount
return True
return False
def withdraw(self, amount: float) -> bool:
'''subtracts money from balance if funds are sufficient'''
if amount > self.balance or amount < 0:
return False
self.balance -= amount
return True
def display_balance(self):
""" displays current account balance """
print(f'\nNet Available Balance is ${self.balance}')
def interface():
"""
Interface for interacting with the bank account
"""
print(f'Hello! Welcome to the Deposit and Withdrawal Machine {name}!')
while True:
action = input('\nWould you like to deposit (D), withdraw (W), show balance (S), or exit (E)? ').upper()
if action in "DWSE":
if action == "D":
try:
deposit_amount = float(input("How much would you like to deposit: "))
if not account.deposit(deposit_amount):
print("Please enter a positive number!")
else:
print(f"Successfully deposited {deposit_amount} into your account.")
except ValueError:
print("Please enter a positive number.")
if action == "W":
try:
withdraw_amount = float(input("How much would you like to withdraw: "))
if not account.withdraw(withdraw_amount):
print("You do not have enough money to withdraw.")
else:
print(f"Successfully withdraw {withdraw_amount} from your account.")
except ValueError:
print("Please enter a positive number.")
if action == "S":
account.display_balance()
if action == "E":
break
if __name__ == '__main__':
name = input('What is your name? ')
account = BankAccount(name)
interface()https://codereview.stackexchange.com/questions/229326
复制相似问题