在 python 中使用 class 时如何解决类型错误?

How to solve type error while using class in python?

今天我尝试了下面的代码来将用户添加到银行并存取款:

class BankAccount:
    owner: str
    amount: int
    min_amount: int
    id: int

    def deposit(self, x):
        self.amount += int(self.amount) + x
test = [
    BankAccount(),
    BankAccount(),
    BankAccount(),
    BankAccount(),
    BankAccount(),
    BankAccount(),
    BankAccount(),
    BankAccount(),
    BankAccount(),
    BankAccount(),
    BankAccount(),
]
while True:
    try:
        print(
            'Press enter: \nW) to withdraw. \nD) to deposit and \nT) to do transactions and \nA) to add users \nL) to check '
            'the user information')
        response = input('>>> ')
        if response == 'A':
            test.append(BankAccount)
            user_id = int(input('Enter ID (0 to 10): '))
            test[user_id].owner = input('Enter name: ')
            test[user_id].amount = input('Enter the balance: ')
            dummy = int(input('Press enter to return.'))
        elif response == 'D':
            user_id = int(input('Enter user ID: '))
            a = int(input('Enter amount: '))
            test[user_id].deposit(a)
            dummy = int(input('Press enter to return.'))
        elif response == 'L':
            user_id = int(input('Enter user ID: '))
            print('Name: ', test[user_id].owner.capitalize())
            print('Balance: ', test[user_id].amount)
            dummy = int(input('Press enter to return.'))
        else:
            print('Invalid input!')
    except ValueError:
        print('Invalid input!')

每当我创建一个用户并尝试使用存款选项添加一些资金时,我都会收到这种类型的错误: self.amount += 整数(self.amount) + x 类型错误:只能将 str(不是“int”)连接到 str

我不确定您是否选择不包含它,但请确保您有一个 init() 函数来创建 class 的实例。这是 classes 上的 Python 文档:https://docs.python.org/3/tutorial/classes.html

那是因为input函数return默认是一个字符串,你必须把它转换成你的“金额”的int。

我猜你在添加新用户并存入资金后重现了这个错误?

如果是,这就是你的问题:

test[user_id].amount = input('Enter the balance: ')

在设置 amount 之前,您需要将用户输入转换为 int。这是正确的代码:

test[user_id].amount = int(input('Enter the balance: '))

如果你想在一般情况下防范这种情况,我会建议你 check out properties 因为这将允许你确保你在添加时始终处理 int amount,无论来源如何。

所以,它抱怨你不能执行“string + int”。发生这种情况是因为您创建用户帐户的代码将 amount 属性 作为字符串:

if response == 'A':
     test.append(BankAccount)
     user_id = int(input('Enter ID (0 to 10): '))
     test[user_id].owner = input('Enter name: ')
     test[user_id].amount = input('Enter the balance: ')  # <--- amount becomes string
     dummy = int(input('Press enter to return.'))

要解决此问题,请将其替换为:

if response == 'A':
    test.append(BankAccount)
    user_id = int(input('Enter ID (0 to 10): '))
    test[user_id].owner = input('Enter name: ')
    test[user_id].amount = int(input('Enter the balance: '))  # <--- int() makes this an integer
    dummy = int(input('Press enter to return.'))

现在,在存款时,您正在做 int + int,而且它有效!输出:

Press enter: 
W) to withdraw.
D) to deposit and
T) to do transactions and
A) to add users
L) to check the user information
>>> A
Enter ID (0 to 10): 1
Enter name: Yeet
Enter the balance: 10
Press enter to return.
Invalid input!

Press enter:
W) to withdraw.
D) to deposit and
T) to do transactions and
A) to add users
L) to check the user information
>>> D
Enter user ID: 1
Enter amount: 2
Press enter to return.

再给个小费。在您的 class 中,deposit 函数未按您预期的方式运行。您要将 x(存款金额)添加到现有金额。而不是:

class BankAccount:
    def deposit(self, x):
        self.amount += int(self.amount) + x   # <--- Amount = amount + amount + x, this is probably not what you want

你可能想要:

    def deposit(self, x):
        self.amount += x   # <--- Amount = amount + x

在创建银行账户时(当响应 == 'A' 时)您打算进行测试 [user_id] = int( input( ... ) )。 你错过了 int。 此外,每次创建帐户时将 class 添加到列表的目的是什么?

如果您打算创建 class 的实例,您忘记了 BankAccount () .