Today I tried the code below to add users into bank and deposit or withdraw funds:
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!')
Whenever I create a user and try to user the deposit option add some funds I get this type error: self.amount = int(self.amount) x TypeError: can only concatenate str (not "int") to str
CodePudding user response:
So, it's complaining that you can't perform "string int". This is happening because your code for making a user account takes the amount
property as a string:
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.'))
To fix this, replace this with:
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.'))
Now, upon depositing, you're doing int int, and it works! Output:
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.
One more tip. In your class, the deposit
function is not behaving the way you would expect. You want to add x
(deposit amount) to the existing amount. Instead of:
class BankAccount:
def deposit(self, x):
self.amount = int(self.amount) x # <--- Amount = amount amount x, this is probably not what you want
You probably want:
def deposit(self, x):
self.amount = x # <--- Amount = amount x
CodePudding user response:
that's because input
function return is a string by default, you must cast it into int for your "amount".
CodePudding user response:
While creating the bankaccount ( when response == 'A' ) You meant to make test[user_id] = int( input( ... ) ). You missed the int. Also, What is the purpose of adding the class to the list everytime an account is created?
If you meant to create an instance of the class you forgot the BankAccount () .
CodePudding user response:
I'm guessing you reproduce this error after adding a new user, then depositing funds?
If so, this is your issue:
test[user_id].amount = input('Enter the balance: ')
You need to convert the user input to an int
before setting amount
. Here is the correct code:
test[user_id].amount = int(input('Enter the balance: '))
If you want to guard against this condition in general, I would suggest you check out properties as this will allow you to ensure you're always dealing with an int
when you're adding to amount
, regardless of source.
CodePudding user response:
I am not sure if you chose not to include it, but be sure you have an init() function to create instances of your class. Here's the Python docs on classes: https://docs.python.org/3/tutorial/classes.html