Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I create something that I can use to access class instances later in my programming?
    primarykey
    data
    text
    <p>I'm a beginning programmer in high school who is interested in computer science. I've been teaching myself to program with Python using Mike Dawson's book, <em>Learning to program for the absolute beginner with Python, 3rd Edition</em>. I'm writing a program to practice my newly acquired knowledge of OOP. The program simulates a bank in which a player can open bank accounts, withdraw/deposit cash, get exchange rates, transfer money from one account to another, and other features. I am having severe trouble with the program, as the main part of the program that simulates the fact that there are multiple accessible accounts that a user can access. I'm now sure how or if it is the way I should be doing things, but I tried writing a class attribute that is an empty list called all_accounts. Each time an account is instantiated, the instance is appended to the list. When a player chooses a menu option such as withdrawing/depositing cash, I want the instance in the list to be accessed so that the object's attributes (such as balance) can be modified. I have no idea how to do this and I've been searching for an answer for about 3 days now without finding anything. Keep in mind, I'm a beginner programmer, so my code will probably be considered poorly written. I'll post the entire code below, as I feel it is necessary that people get a complete feel of how the code works and functions. Also, feel free to notify me of anything else in my code that isn't right or that I could be doing better. I'm always open to learning.</p> <pre><code>class Account(object): """An interactive bank account""" wallet = 0 all_accounts = [] # initial def __init__(self, ID, bal): self.ID = ID self.bal = bal print("A new account has been opened!") Account.all_accounts.append(self) def withdraw(self, w_input): if w_input &lt; self.bal or w_input == 0: print("That is not a valid amount. Sending you back to menu.\n") Menu() else: self.bal = self.bal - w_input print("You withdrew $" + str(w_input) + ".\n") wallet += w_input print("You wallet now contains $" + Account.wallet) Menu() def deposit(self): # Whatever def Menu(): print( """ 0 - Leave the Virtual Bank 1 - Open a new account 2 - Withdraw money 3 - Deposit money 4 - Transfer money from one account to another 5 - 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": account_name = input("What is your account's ID?: ") account_bal = float(input("How much money would you like to put into the account?(USD): ")) account_name = Account(ID = account_name, bal = account_bal) Menu() elif choice == "2": account_choice = input("What is your account ID?: ") if account_choice in instance.Account.all_account: withdraw_choice = input("How much money would you like to withdraw?: ") account_choice.withdraw(w_input = withdraw_choice) else: print("Nope.") if choice == "0": print("\nThank you for visiting the virtual bank!") input("Press the [ENTER] key to exit the program.") Menu() </code></pre>
    singulars
    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.
    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