I am making a program for my CS class. Is there a way, in python, to set more than one value to a variable? Am I thinking about this in the wrong way?

I need to make a program that asks a bank account holder for their username.

If that user name is in the the system it asks for the pin.
If not, it kicks them back a step

If the username and pin match then it displays their account details.
If not, again, it kicks them back a step

Any help would be greatly appreciated!

Dani AI

Generated

You can stop trying to jam multiple names into a single plain variable and use a container or object that holds each account's data. @neo is right that a class helps encapsulate behaviour and points toward the basic container types. For a login flow the cleanest pattern is a mapping keyed by username that holds the PIN and whatever account details you need. That makes the existence check and the PIN lookup simple and fast.

A small class wrapping a username->record mapping keeps the code tidy and makes it easy to add checks (max attempts, locking, persistence) later:

class Bank:
    def __init__(self):
        self.accounts = {
            'lee': {'pin': '0314', 'balance': 120.50},
            'wes': {'pin': '9999', 'balance': 75.00},
        }

    def authenticate(self, username, pin):
        acct = self.accounts.get(username)
        return acct is not None and acct['pin'] == pin

    def get_account(self, username):
        return self.accounts.get(username)

Practical tips: treat PINs as strings (do not cast to int — leading zeros matter), trim whitespace and normalise case only if intended, limit login attempts to avoid infinite loops, and do not keep real PINs in plaintext when you move beyond a toy project. For anything beyond a class assignment, store account data in a file or database and store hashed PINs (use a proper password-hashing routine or library). Start simple with a dict/class setup to satisfy the assignment, then add persistence and hashing when you need production-style safety.

Recommended Answers

All 4 Replies

try to use classes :)

try to use classes :)

In the class I am going to create a variable called user_names.
Is is possible for a variable to have multiple values?
Example: user_name = lee, wes?

That idea is what I am stuck on. Is that possible?

Thanks

For example, use list.

user_names = []
user_names.append('User1')
user_names.append('User2')
print(user_names)
if user_names[0] == someuser:
    ....

There is many ways to reprecent group of information. Basic choice is list for changing data or tuple for rarely changing data. Tuple is faster but any change to it generates new tuple as it is immutable. Please go through the tutorials section on types.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.