Reputation: 709
I'm a beginning programmer who is building a program that simulates a bank with multiple bank accounts that a user can withdraw/deposit cash, create accounts, get exchange rates, etc. Currently, I'm trying to access a group of instances in my class that are all objects of my account class. The Account Manager class is responsible for managing these account objects and helping to organize them when user input is required. Right now, I'm trying to simulate my 3rd option on my menu which gets info on the account of a user's choice(they must manually put the ID of their account in in order to retrieve information on it, withdraw/deposit cash, etc.). Although I've managed to store all of these class instances in a list, I can't seem to use my get_account method to retrieve these for use. I'll post all of my code below. If you see anything else that is out of place, feel free to let me know. Code:
# Virtual Bank
# 3/21/13
# Account Manager Class
class AccountManager(object):
"""Manages and handles accounts for user access"""
# Initial
def __init__(self):
self.accounts = []
# create account
def create_account(self, ID, bal = 0):
# Check for uniqueness? Possible method/exception??? <- Fix this
account = Account(ID, bal)
self.accounts.append(account)
def get_account(self, ID):
for account in self.accounts:
if account.ID == ID:
return account
else:
return "That is not a valid account. Sending you back to Menu()"
Menu()
class Account(object):
"""An interactive bank account."""
wallet = 0
# Initial
def __init__(self, ID, bal):
print("A new account has been created!")
self.id = ID
self.bal = bal
def __str__(self):
return "|Account Info| \nAccount ID: " + self.id + "\nAccount balance: $" + self.bal
# Main
AccManager = AccountManager()
def Menu():
print(
"""
0 - Leave the Virtual Bank
1 - Open a new account
2 - Get info on an account
3 - Withdraw money
4 - Deposit money
5 - Transfer money from one account to another
6 - Get exchange rates(Euro, Franc, Pounds, Yuan, Yen)
"""
) # Add more if necessary
choice = input("What would you like to do?: ")
while choice != "0":
if choice == "1":
id_choice = input("What would you like your account to be named?: ")
bal_choice = float(input("How much money would you like to deposit?(USD): "))
AccManager.create_account(ID = id_choice,bal = bal_choice)
Menu()
elif choice == "2":
acc_choice = input("What account would you like to access?(ID only, please): ")
AccManager.get_account(acc_choice)
print(acc_choice)
Menu()
Upvotes: 0
Views: 744
Reputation: 16499
Your Account
objects don't actually seem to have ID
attributes; instead, they have id
attributes. Python is case-sensitive; try changing if account.ID == ID
to if account.id == ID
.
EDIT:
You are also returning after the very first mismatch. You need to remove one level of indentation from your else
block so that you get through the entire loop first, and in fact, your else
block shouldn't even be an else
block, since you're not actually matching an if
; the method should only fail if none of the accounts match the given ID.
EDIT 2:
Also, you're not actually assigning the return value from get_account()
to anything, so it's lost. I'm not exactly sure what you expect to happen there.
Upvotes: 3
Reputation:
The error lies in lines 31 and 35. You have written "id" instead of "ID". Fully capitalize those two things such that:
class Account(object):
"""An interactive bank account."""
wallet = 0
# Initial
def __init__(self, ID, bal):
print("A new account has been created!")
self.ID = ID
self.bal = bal
def __str__(self):
return "|Account Info| \nAccount ID: " + self.ID + "\nAccount balance: $" + self.bal
Please let us know if the code works after that.
Upvotes: 0