我正在使用Python创建一个银行系统程序,这个程序的要求非常基本:
我已经成功地编写了基本功能,现在我需要任何关于如何改进我的代码的反馈,以便创建一个具有清晰、简洁和结构化代码的程序。我非常感谢任何类型的反馈,无论是编码特定的反馈还是对我的一般编码的反馈,因为我一般都是编程初学者。
PS:我知道这个程序需要错误处理,我已经在做了。
import pyodbc
cnxn = pyodbc.connect('Driver={ODBC Driver 13 for SQL Server};Server=GLOBALGROWTH\SQLEXPRESS;Database=IDLBANKSYS;Trusted_Connection=yes;')
cursor = cnxn.cursor()
class CLIMenu(object):
def __init__(self, options: list):
self.options=options
def PrintCLIMenu(self):
print('=' * 30, '\tIDL BANKING SYSTEM', '=' * 30, sep='\n')
print()
print("PLEASE ENTER THE NUMBER CORRESPONDING TO YOUR DESIRED COMMAND IN THE PROMPT BELOW :\n ", *self.options,
sep='\n')
print()
def GetUserInput(self):
return input(">>>: ")
def RegisterCustomer():
RegisterCustomerForm = [input("NATIONAL IDENTITY CARD NUMBER : "), input("FULL NAME IN CAPITALS : "),
input("DATE OF BIRTH AS DD/MM/YYYY : "),
input("ADDRESS : "), input("CONTACT NUMBER : "), input("EMAIL ADDRESS : ")]
cursor.execute("INSERT into customer VALUES(?,?,?,?,?,?)", *RegisterCustomerForm)
cnxn.commit()
print("REGISTRATION SUCCESSFUL")
cursor.execute("INSERT into account VALUES(?,?,0)", RegisterCustomerForm[0], RegisterCustomerForm[1])
cnxn.commit()
def ViewCustomer():
CustToView=input("PLEASE ENTER THE NATIONAL IDENTITY CARD NUMBER OF THE CUSTOMER YOU WISH TO VIEW : ")
cursor.execute("SELECT * FROM customer WHERE Cust_NIC=?",CustToView)
columns = [column[0] for column in cursor.description]
print('\t'.join(str(i) for i in columns),end="")
print('\n')
for row in cursor.fetchall():
print ('\t'.join(str(j)for j in row))
input("\n==========PRESS ENTER KEY TO RETURN TO THE PREVIOUS MENU==========")
def ViewAllCustomers():
cursor.execute("SELECT * FROM customer")
columns = [column[0] for column in cursor.description]
print('\t'.join(str(i) for i in columns), end="")
print('\n')
for row in cursor.fetchall():
print('\t'.join(str(j) for j in row))
input("\n==========PRESS ENTER KEY TO RETURN TO THE PREVIOUS MENU==========")
class Transaction:
def __init__(self,acct,amt):
self.acct=acct
self.amt=amt
def Deposit(self):
cursor.execute("UPDATE account SET Acct_Bal=Acct_Bal+? WHERE Acct_NO=?",self.amt,self.acct)
cnxn.commit()
print("YOU'VE DEPOSITED %s TO ACCOUNT NUMBER %s" % (self.amt,self.acct))
def Withdrawal(self):
cursor.execute("UPDATE account SET Acct_Bal=Acct_Bal-? WHERE Acct_NO=?",self.amt,self.acct)
cnxn.commit()
print("YOU'VE WITHDRAWN %s FROM ACCOUNT NUMBER %s" % (self.amt, self.acct))
if __name__=='__main__':
while True:
MainMenu=CLIMenu(['\t1.ACCESS CUSTOMER DETAILS','\t2.ACCESS TRANSACTION PORTAL','\t3.EXIT'])
MainMenu.PrintCLIMenu()
UserInput=MainMenu.GetUserInput()
if UserInput =='1':
while True:
CustomerMenu = CLIMenu(['\t1.REGISTER CUSTOMER', '\t2.VIEW CUSTOMER',
'\t3.VIEW ALL CUSTOMERS','\t4.GO TO PREVIOUS MENU'])
CustomerMenu.PrintCLIMenu()
UserInput=CustomerMenu.GetUserInput()
if UserInput == '1':
RegisterCustomer()
continue
if UserInput == '2':
ViewCustomer()
continue
if UserInput == '3':
ViewAllCustomers()
continue
if UserInput == '4':
break
elif UserInput == '2':
AcctToTransact=input("PLEASE ENTER THE ACCOUNT NUMBER : ")
AmtToTransact=input("PLEASE ENTER THE TRANSACTION AMOUNT : ")
trnsct=Transaction(AcctToTransact,AmtToTransact)
TransactionMenu = CLIMenu(['\t1.DEPOSIT MONEY',
'\t2.WITHDRAW MONEY', '\t3.GO TO PREVIOUS MENU'])
TransactionMenu.PrintCLIMenu()
UserInput = TransactionMenu.GetUserInput()
if UserInput == '1':
trnsct.Deposit()
continue
if UserInput == '2':
trnsct.Withdrawal()
continue
if UserInput == '3':
continue
elif UserInput == '3':
break
cnxn.close()
quit()
发布于 2017-11-14 16:38:19
For
循环两次(以及后面的打印)。您应该将其解压缩到一个新函数中,并调用它两次。if
条件下的代码,将elif
下的代码提取为新函数。if
的UserInput
语句。参见一个示例这里。https://codereview.stackexchange.com/questions/160668
复制相似问题