Ask coding questions

← Back to all posts
Can't get a class with db to instantiate
OldWizard209 (1537)

Problem

So as you can see, in the code below, I am asking the user to log in, if not, Sign up, while saving the user's data of money, deposits, inventory in a key-value pair into the database. But to actually uses the user's stats saved to their value pair, I have to make the username available in all places in the code. Rather I assigned them to variables:

self.money = db[username][1]
self.inventory = db[username][2]
self.deposit_allowed = db[username][3]
self.deposited = db[username][4]
self.working = db[username][5]

But despite doing that, I get errors that "user name is not defined" and "self.inventory is not defined".

The Real Question

My real question is that how can I make the variables, that I have put up, have the values of the list that I have assigned to the value pair of the user's name and make it work globally.

Code

Here is a portion of the important code that I am using.

class Game:
    def __init__(self, username):
        self.money = db[username][1]
        self.inventory = db[username][2]
        self.deposit_allowed = db[username][3]
        self.deposited = db[username][4]
        self.working = db[username][5]
        
    def database(self):
        enter_game = input("Do you want to [1] Login\n[2] Signup:\n>")
        while enter_game == '1' or enter_game == '2':
            if enter_game == '1':
                username = input("Enter user name: ")
                password = input("Enter password: ")

                if username in db.keys():
                    if db[username][0] == password:
                        print("Loggedd In")
                        break

                else: 
                    print("Invalid Username, Password")
                    break

            if enter_game == '2':
                username = input("Enter new Username: ")
                password = input("Enter new password: ")

                db[username] = [password, 300000, [], 500, 0, False]
                break

Also, the [roject is literally massive, so rather, I have attached a link to go to the real code(The code that is used here is in the gameplay.py file):
Click here to go to the repl with the code.

Or if that doesn't work, click here:

https://replit.com/@OldWizard209/Life-SIMULATOR-In-Development-2#gameplay.py

Comments
hotnewtop
Coder100 (18187)

no that is not how you use classes!

you use classes by setting the instance into something, you should never ever create a class twice like that.

game = Game(username)

game.money
OldWizard209 (1537)

I don't understand how this is going to help me. I want to use the DB elements in the class, not outside it. To instantiate the class, I will need to go outside the class. @Coder100

Coder100 (18187)

hmm
maybe you need to learn more about classes

classes make instances, and instances have methods.

Methods are how you are going to accomplish making changes to the db. @OldWizard209