Reputation: 149
i have a problem where if i type 3 or more characters in pycharm it lags madly. i uninstalled the application and will try installing to see if that fixes the problem + i was in the middle of writing some code. can someone copy my code and paste it on his pycharm to see if it works. if not, can someone tell me what the problem is. probably problems i am new to python my code:
class bank_account:
def __init__(self, account_number,name,opening_balance,type_of_account):
self.account_number = account_number
self.name = name
self.opening_balance = opening_balance
self.type_of_account = type_of_account
amount_of_money = opening_balance
def __str__(self):
print('account number: ', account_number)
print('name: ',name)
print("opening balance: ", opening balance)
print('type of account: ',type_of_account)
def deposit(amount):
global amount_of_money
amount_of_money += amount
def withdraw(amount):
global amount_of_money
amount_of_money -=amount
def get_balance():
global amount_of_money
return str(amount_of_money) + '$'
acc1 = bank_account('66666666666666', 'mike',1000,'deposit')
acc1.deposit(250)
acc1.get_balance()
print(acc1)
Thanks
Upvotes: 1
Views: 62
Reputation: 1370
Your code is not good to say the least. PyCharm should tell you if there any problems before it runs. I'm not sure if you are running on a potato that it lags.
Here are the mistakes I found:
opening balance
should be opening_balance
indentation errors
all class functions should have self
as a first argument
don't use global
to access class variables. Use self.
PEP-8 violations that can be corrected with pressing CTRL+ALT+L
on your keyboard
__str__
should not print. It should rather return an str
Edit: classes should follow CamelCaps conventions
Not an error but I suggest sticking with either single or double quotes to encapsulate strings. Everyone should at least be consistent with themselves.
There are probably some logic mistakes but that depends on what you want to do with the class.
class BankAccount:
def __init__(self, account_number, name, opening_balance, type_of_account):
self.account_number = account_number
self.name = name
self.opening_balance = opening_balance
self.type_of_account = type_of_account
self.amount_of_money = opening_balance
def __str__(self):
result = "account number: " + str(self.account_number)
result += "\nname: " + str(self.name)
result += "\nopening balance: " + str(self.opening_balance)
result += "\ncurrent balance: " + str(self.amount_of_money)
result += "\ntype of account: " + str(self.type_of_account)
return result
def deposit(self, amount):
self.amount_of_money += amount
def withdraw(self, amount):
self.amount_of_money -= amount
def get_balance(self):
return str(self.amount_of_money) + "$"
acc1 = BankAccount("66666666666666", "mike", 1000, "deposit")
acc1.deposit(250)
print(acc1.get_balance())
print(acc1)
I highly recommend following a tutorial series on Python. You won't be blundering about for hours not knowing what to do. This one by Corey Schafer seemed very professional to me and I learnt Flask from one of his tutorials.
If PyCharm is too heavy duty for you computer, there are many other lighter editors such as Atom or Sublime Text.
Upvotes: 1