【发布时间】:2018-09-14 17:01:35
【问题描述】:
我对 python 还是很陌生,所以请多多包涵。我正在用 python 练习类和对象,所以我开始了这个银行账户类。有两种类型的银行账户:个人或企业。企业帐户继承了个人的一些行为,但我还没有完全完成。
这里的问题是,每当我存入一笔金额(例如 50)时,字典不会将值更新为相应的键(帐户持有人的姓名),我希望该值会被更新每当有人提款或存入账户时 - 例如:初始余额 = 50 存款 = 50,dict 将名称和余额更新为初始余额 + 存款
import sys
import self
#print a menu so user can pick
def menu():
print("Welcome to Wells Fargo!")
print("Lets get started!")
print("1. Open Individual Account \n"
"2. Open Business Account \n"
"3. Check Individual Account \n"
"4. Check Business Account \n"
"5. Exit")
#if user doesnt type number or 1,2,3 , throw error
try:
choice = int(input("Please enter your choice here: "))
except ValueError:
print("Oops! Looks like you didnt type something correct, please try again!")
if choice == 1:
print("Creating Individual account...")
#create object representing account info
#set name method
individual.set_name(self)
#set balance method
individual.set_balance(self)
#call menu
menu()
elif choice == 2:
print("Creating Business account...")
#create object representing account
name = str(input("Enter your name here: "))
start_balance = int(input("Enter the balance you want to start with: "))
save = int(input("Enter how much you want to deposit in your savings account: "))
#add object to list
bacc = Business(name, start_balance, save)
#call menu
menu()
elif choice == 3:
print("Loading Individual Accounts..")
#call check individual accounts method
individual.check_accounts()
elif choice == 4:
print("Loading Business Accounts...")
#call check business accounts method
Business.check_accounts()
elif choice == 5:
#exit the program
print("Thank you for using wells fargo, have a nice day!")
sys.exit(0)
#ask user if ready
def prompt_user():
try:
ready = int(input("Are you ready to open/access a account?1=Yes,2=No"))
except ValueError:
print("Oops! You did not type something right, please retry!")
else:
menu()
#individual bank account class
class individual:
#initialize defaults (name, balance, &ilst)
def __init__(self, balance=0, transaction=0,name=''):
self.balance = balance
self.transaction = transaction
self.name = name
self.idict = {}
#get the name of the account owner
def set_name(self):
self.name = str(input("Enter the name of the account owner here: "))
#add name to dictionary
idict[self.name] = 0
#get balance user wants to start with
def set_balance(self):
self.balance = int(input("Enter how much money you want to deposit into your account: "))
#if amount is less than 25, throw error
if self.balance < 25:
print("ERROR! Low Amount, must be greater than or equal to 25")
#else add it to the dictionary
else:
idict[self.name] = self.balance
global idict
idict = {
"John Adams": 5000,
"Josh Adkins": 4000
}
#return object as a string , is this needed??
def __str__(self):
return str(self.name)
#add money & calculate
def __add__(self,other):
balance = self.balance + other.balance
transaction = self.transaction + other.transaction
return balance + transaction
#subtract money & calculate
def __sub__(self, other):
balance = self.balance + other.balance
transaction = self.transaction + other.transaction
return balance - transaction
#return individual(self.name, self.balance)
#return user balance
def __int__(self):
#search for user name
for x,y in idict.items():
if self.name in idict:
print("fetching account balance...")
print(int(self.balance))
break;
else:
print("ERROR! Looks like you arent registered in our system!")
individual.imenu()
#individual account menu
def imenu():
#1. balance, 2. transactions(add, subtract) 3. close acc 4. exit
print("1. Get Account Balance \n"
"2. Deposit $$ \n"
"3. Withdraw $$ \n"
"4. Close Account \n"
"5. Exit")
choice2 = int(input("Enter your choice here: "))
if choice2 == 1:
individual.__int__(self)
elif choice2 == 2:
add_money = int(input("Enter the amount here: "))
sum1 = individual(self.balance, 0)
sum2 = individual(self.balance, add_money)
idict[self.name] = sum1.__add__(sum2)
print("Money added!")
individual.imenu()
elif choice2 == 3:
sub_money = int(input("Enter the amount here: "))
individual.__sub__(self, sub_money)
elif choice2 == 4:
individual.close_account(self)
elif choice2 == 5:
print("Goodbye!")
sys.exit(0)
#loop thru indiv acc(s)
def check_accounts():
#have user type in their name and check if they have an open account
enter_name = str(input("Enter your name here: "))
for key,value in idict.items():
#check if name is in list
if enter_name in idict:
print("Your account was found! Proceeding...")
individual.imenu()
else:
print("Looks like you dont have an individual account, please create one!")
#close individual account
def close_account(self):
print("Type in your name to proceed with account deletion: ")
try:
#have user type in their name
type_name = str(input("Type it here: "))
except ValueError:
#if user types anything other than name, throw error
print("Looks like you typed something incorrect! Please retry!")
#search business account dict ,
for x,y in idict.items():
#if the name is in the, dict remove it
if type_name in idict:
del[type_name]
#if the name is not in the dict, redirect to menu
else:
print("Looks like you dont have an account! Please create one!")
menu()
#call methods
prompt_user()
【问题讨论】:
-
请提供您的问题的最小示例,您期望什么结果以及您有什么结果
-
更新了! @CorentinLimier
-
这不是minimal reproducible example。
self模块来自哪里?这是一个非常奇怪和令人困惑的模块名称!该类中发生了一些奇怪的事情,例如global idict,并且一些方法缺少它们的selfarg。 -
欢迎来到 StackOverflow。请按照您创建此帐户时的建议阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布 MCVE 代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中并重现您描述的问题。您给定的代码无法运行(
self包丢失),并且似乎需要用户输入才能显示错误。它根本不是最小的。 -
好的,很抱歉@Prune
标签: python python-3.x dictionary