Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I access a class instance in a list when there is multiple classes?
    primarykey
    data
    text
    <p>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:</p> <pre><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??? &lt;- 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() </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload