Questions about creating Python class objects

Asked 1 years ago, Updated 1 years ago, 247 views

acc_cus = []
dif = 0
while 1:
    num = int(input(
        """
++++++++++++++++++++++++++++++
Enter the number of the item you want.

1. Create an account
2. Deposit
3. Withdrawal
4. Check account information
5. Shutdown
++++++++++++++++++++++++++++++
        """
        ))

    class Bank:
        def __init__(self,name):
            self.name = name
            self.account = [0]

        def inform(self):
            return "++++++++++++++++++++++++++++++++++++++++++++++++++++++" account name: " + str(self.name) +"\n"+" balance: " + str(self).account[0])+"원\n"+"++++++++++++++++++++++++++++++"



    if num == 1:
        name = input("""
++++++++++++++++++++++++++++++
Please enter a name for your account.
++++++++++++++++++++++++++++++
        """)
        dif += 1
        "customer"+str(dif) = Bank(name)
        fin = 'customer'+str(dif)
        acc_cus.append(fin.name)
        print(fin.inform())
        print("""
++++++++++++++++++++++++++++++
Account creation is complete.
++++++++++++++++++++++++++++++
        """)
        print("""
++++++++++++++++++++++++++++++
Current Account List\n""+str(acc_cus)+"\n++++++++++++++++++++++++++++++++++++++++"
              )
    #(Looping)

I'm a novice at Python who has been studying for about a week.

When creating an object here, I want to set the instance name as custom1 and custom2, but the diff is 1, 2... If you try to connect with "custom"+str(diff) with this setting, the instance setting will not work. What's the problem?

python class instance

2023-02-05 13:09

1 Answers

You must include the following variables:

b = "customer"+str(dif)
a = Bank(name = b)
print(a.name, a.account)


2023-02-05 18:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.