代码停止所有提款,即使不应该这样做



所以我本应该实现一段代码,防止用户每月从储蓄账户提款一次以上,但不幸的是,我遇到了麻烦,无法解决这个问题。我的代码阻止提款,即使唯一的交易是存款。

def okToWithdraw(acc: SavingAccount) -> "bool":
"""Compares two dates then gives the ok"""
# reads lines of dictionaries and appends the to a list
myList = openfile("accountTransactions.txt")
# today's date
tim = str(date.today())
# filters through the file so that only transactions
# from the same account are forwarded
found_list = search_dic_list(myList, "AccID", acc.acc_ID)
# if not found, green light and Withdrawal can proceed
if not found_list:
return True
else:
# here is the problem code
dic = found_list[len(found_list) - 1]
if days_between(tim, str(dic["TransTime"])) >= 30:
return True
return False
....
...
def search_dic_list(myList: "list", key: "str", item: "str") -> "list" or None:
"""Searches a dictionary list by its key for a certain value
returns the found value as a list
None is it doesnt exist"""
found_value = []
found = False
for dictionary in myList:
if dictionary[key] == item:
found_value.append(dictionary)
found = True
if found:
return found_value
return None

任何帮助都将不胜感激注意:每个交易都有一个唯一的ID,该ID基本相同,但会增加

trans_id = str(acc.acc_ID) + method + str(tim) + "_" + str(count)
# acc.acc_ID = account class ID ex. "1234_Sav"
# Method = a string ex "_WD_"
# str(tim) = time
# str(count) incrementing index

提前谢谢!

您可以在found_list中实现查找提款的小功能,并更改一些代码:

def okToWithdraw(acc: SavingAccount) -> "bool":
"""Compares two dates then gives the ok"""
# reads lines of dictionaries and appends the to a list
myList = openfile("accountTransactions.txt")
# today's date
tim = str(date.today())
# filters through the file so that only transactions
# from the same account are forwarded
found_list = search_dic_list(myList, "AccID", acc.acc_ID)
found_withdrawal = find_withdrawal(found_list)
# if not found, green light and Withdrawal can proceed
if not found_withdrawal:
return True
else:
# here is the problem code
dic = found_list[len(found_list) - 1]
if days_between(tim, str(dic["TransTime"])) >= 30:
return True
return False
def find_withdrawal(transactions: list)->datetime:
pass

相关内容

最新更新