【发布时间】:2021-06-02 03:34:44
【问题描述】:
我想制作一种“注册系统”,但在银行使用对象,因此您作为客户必须创建您的帐户,因此,您不能拥有与其他客户相同的名称(用户名)。
我试过这个:
class Account:
def __init__(self, name, pin, balance):
self.name = name
self.pin = pin
self.balance = balance
def __get_name__(self):
return self.name
def __set_name__(self, name):
self.name = name
def __get_pin__(self):
return self.pin
def __set_pin__(self, pin):
self.pin = pin
def __get_balance__(self):
return self.balance
def __set_balance__(self, balance):
self.balance = balance
def _deposit(self, deposition_amount):
self.balance += deposition_amount
def _withdraw(self, withdrawal_amount):
self.balance -= withdrawal_amount
set_of_accounts = set()
def create_account():
name = input("Input name : ")
for account in set_of_accounts:
if account.__get_name__() == name:
print('There is other account with same name, try a new one')
else:
pin = str(input("Please input a pin of your choice : "))
balance = eval(input("Please input a amount to deposit to start an account : "))
account = Account(name, pin, balance)
set_of_accounts.add(account)
print("\n----New account created successfully !----")
print("Note! Please remember the Name and Pin")
print("========================================")
def print_all_customers():
print("Customer name list and balances mentioned below : \n")
if len(set_of_accounts) > 0:
for account in set_of_accounts:
print("->. Customer = " + str(account.__get_name__()))
print("->. Balance = " + str(account.__get_balance__()) + " -/Rs\n")
else:
print("No accounts are persisted yet.\n")
input("Please press enter key to go back to main menu to perform another function or exit ...")
def menu():
while True:
print("*************************************")
print("=<< 1. Open a new account >>=")
print("=<< 2. Withdraw Money >>=")
print("=<< 3. Deposit Money >>=")
print("=<< 4. Check Customers & Balance >>=")
print("=<< 5. Exit/Quit >>=")
print("*************************************")
choiceNumber = input("Select your choice number from the above menu : ")
if choiceNumber == "1":
create_account()
elif choiceNumber == "2":
pass
elif choiceNumber == "3":
pass
elif choiceNumber == "4":
print_all_customers()
elif choiceNumber == "5":
exit()
else:
print("Invalid option")
menu()
我的问题出在create_account() 中,因为在我写下名称后,程序没有进入 for 循环。所以我迷路了,因为我不知道为什么不执行 for 循环。
我希望有人可以帮助我,如果您有其他想法,我接受建议。
【问题讨论】:
-
如果帐户集为空。你认为 for 循环将如何执行?您需要添加一个特殊条件来检查集合是否为空。
-
您不需要单独的 for 循环:
if any(acc.__get_name__() == name) for acc in set_of_accounts): -
dunder 方法是怎么回事?
__names_like_this__为 Python 核心语言和标准库保留。__set_name__特别是 already used 用于描述符功能。 -
你用的是什么版本的python?
标签: python for-loop object oop set