Is it possible to access a single object for a single part of a program in Python 3?
-
I'm a beginner programmer who is writing an interactive virtual bank program to help me strengthen my newly-acquired OOP skills. I'm trying to create two separate menus for my program, one for creating an account, getting exchange rates, accessing the account menu(second menu) and leaving the program. The second menu(account menu) is for checking your balance, withdrawing/depositing cash, and going back to the main menu. When I access the account menu however, I want the program to remember that I am talking about one specific bank account object while there may be more, but then if I go back to the main menu and go back into the account menu ask for a new account name and remember that account until the user backs out to the main menu. How do I accomplish this? Is there a better way to do it that I'm not thinking about? I'll leave my code below(be warned, it'll be rough. Feel free to correct or improve on anything that you see wrong in my code or anything that I could be doing better. I enjoy the tips and criticism.) NOTE: Please don't write my program for me. This is something that I want to accomplish on my own, so don't necessarily provide code for me, but instead tell me what to do while still letting me learn how to do it myself. Thanks :) # Virtual Bank # 3/20/13 class BankAccount(object): """An interactive bank account.""" wallet = 0 accounts = [] # Initial def __init__(self, name, bal): print("A new bank account has been created!") self.name = name self.bal = bal self.name += accounts # Account Info def __str__(self): account_info = "|Account Info| \n Account ID: " + self.name + "\nAccount Balance: $" + str(self.bal) return account_info def AccountMenu(): AccMenuFun() # Welcome player print("|Welcome to the Virtual Bank!|") # Menu Loop def BankMenu(): global account_name_dec print( """ 0 - Leave the bank 1 - Create a new account 2 - Access an account(Account Menu) 3 - Get Exchange Rates(Euro, Yen, Pounds, Yuan, Franc) """ # Add more options if neccessary ) decision = input("What would you like to do: ") while decision != "0": if decision == "1": account_name_dec = input("What would you like to name your account: ") account_bal_dec = float(input("How much money would you like to deposit into your account?: ")) account_name_dec = BankAccount(name = account_name_dec, bal = account_bal_dec) BankMenu() elif decision == "2": choice = input("Which account would you like to access?: ") if choice == account_name_dec: account_name_dec.AccountMenu() else: print("That is not a valid account ID. Sending you back to main menu!") BankMenu() if decision == "0": print("Thank you for visiting the Visual Bank!") input("Press the [ENTER] key to exit.") # Account Menu Function def AccMenuFun(): print("|Account Menu for:", account_name_dec) print( """ 0 - Go back to Main Bank Menu 1 - Get Account Info 2 - Withdrawal Cash 3 - Deposit Cash """ # Add more options if neccessary ) decision = input("Choice: ") while decision != "0": if decision == "1": print(account_name_dec) # Initiate Menus BankMenu()
-
Answer:
Enlarging on +Richard Careaga's answer, you called your class BankAccount, but then I see a list named accounts within that class. Closer to the mark would be to have a class BankAccount and for each bank account that you create, you create one instance of a BankAccount object. You'll then need something to keep track of a bunch of BankAccount objects. Maybe that could go into a class named Bank? Are you clear on the distinction between a class and an object? You create objects that are instances of a class. The methods of the class are, in general, available for the object to use. You can have many instances, that is many objects, all generated from the same Class. And, of course, the program can have more than one class of object incorporated into it. Getting back to your original question, if you structure the program as I've described, it'd be easy to write functions that are passed just one BankAccount object and operate on that argument without making calls to retrieve other BankAccount objects. Python is a "consenting adults" programming language and as such it has nothing to make it impossible for your function to break the convention and go get another BankAccount other than the one it was passed to operate upon. So don't write code that misbehaves like that.
R. Drew Davis at Quora Visit the source
Related Q & A:
- How To Send a Complex Object to a Rest Web Service?Best solution by codeproject.com
- How to place a different background image on each part of a book?Best solution by TeX - LaTeX
- Is it possible to use a phone camera as a webcam?Best solution by Yahoo! Answers
- Is it possible to use a game controller as a mouse?Best solution by Yahoo! Answers
- Is it possible to do a component to s-video to rca for a video signal?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.