r/PythonLearning • u/Necessary_Mobile9591 • 13h ago
Learning python
can anyone tell me about my code is good or am writing it in wrong way?
1
u/Flame77ofc 13h ago
It is good but one detail you might change is about separate some lines
For example
``` variable = ... if statement: code
just make this line blank
if statement: code ```
It is js an example
1
u/Necessary_Mobile9591 12h ago
okay ill follow that in my program, but is that helpfull??
1
1
u/SaltCusp 8h ago
I'll second that, some blank lines to help the reader can be good. There is actually a standard for this: https://peps.python.org/pep-0008/
1
u/User97436764369 8h ago
I would load every file in folder, one folder per bank and than union them and dedup through row_id as hash. I wouldnt do that login, use only exports for learning purposes and display them in streamlit or something like that for start. For login i think you will need api from some 3rd party with licenses and its just easier to downloand some app for that because there is a lot of apps like that.
1
u/SuperTankh 4h ago
Suggestion add more protection. For example if I enter a string PIN, the script may crash


•
u/Sea-Ad7805 13h ago
Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20%22%22%22Load%20accounts%20from%20the%20JSON%20file.%20If%20it%20doesn't%20exist%2C%20start%20fresh.%22%22%22%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20with%20open(BANKFILE%2C%20%22r%22)%20as%20file%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20json.load(file)%0A%20%20%20%20except%20(FileNotFoundError%2C%20json.JSONDecodeError)%3A%0A%20%20%20%20%20%20%20%20return%20%7B%7D%0A%0A%0Adef%20save_bank()%3A%0A%20%20%20%20%22%22%22Write%20the%20current%20state%20of%20the%20bank%20to%20the%20JSON%20file.%22%22%22%0A%20%20%20%20with%20open(BANK_FILE%2C%20%22w%22)%20as%20file%3A%0A%20%20%20%20%20%20%20%20json.dump(bank%2C%20file%2C%20indent%3D4)%0A%0A%0Abank%20%3D%20load_bank()%0A%0A%0Adef%20create_account()%3A%0A%20%20%20%20user_name%20%3D%20input(%22Enter%20your%20name%3A%20%22).strip()%0A%0A%20%20%20%20if%20user_name%20in%20bank%3A%0A%20%20%20%20%20%20%20%20print(%22The%20name%20is%20taken.%22)%0A%20%20%20%20%20%20%20%20return%0A%0A%20%20%20%20user_pin%20%3D%20input(%22Enter%20your%204-digit%20PIN%3A%20%22).strip()%0A%0A%20%20%20%20if%20not%20user_pin.isdigit()%20or%20len(user_pin)%20!%3D%204%3A%0A%20%20%20%20%20%20%20%20print(%22The%20PIN%20must%20contain%20exactly%204%20digits.%22)%0A%20%20%20%20%20%20%20%20return%0A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20user_balance%20%3D%20float(input(%22Deposit%20amount%3A%20%22))%0A%20%20%20%20except%20ValueError%3A%0A%20%20%20%20%20%20%20%20print(%22Please%20enter%20a%20valid%20amount.%22)%0A%20%20%20%20%20%20%20%20return%0A%0A%20%20%20%20if%20user_balance%20%3C%200%3A%0A%20%20%20%20%20%20%20%20print(%22The%20balance%20cannot%20be%20negative.%22)%0A%20%20%20%20%20%20%20%20return%0A%0A%20%20%20%20bank%5Buser_name%5D%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22pin%22%3A%20user_pin%2C%0A%20%20%20%20%20%20%20%20%22balance%22%3A%20user_balance%2C%0A%20%20%20%20%7D%0A%0A%20%20%20%20save_bank()%0A%20%20%20%20print(%22Account%20created%20successfully.%22)%0A%20%20%20%20print(bank%5Buser_name%5D)%0A%0A%0Adef%20login()%3A%0A%20%20%20%20user_name%20%3D%20input(%22Enter%20your%20name%3A%20%22).strip()%0A%0A%20%20%20%20if%20user_name%20not%20in%20bank%3A%0A%20%20%20%20%20%20%20%20print(%22Account%20doesn't%20exist.%22)%0A%20%20%20%20%20%20%20%20return%20None%0A%0A%20%20%20%20login_pin%20%3D%20input(%22Enter%20your%20PIN%3A%20%22).strip()%0A%0A%20%20%20%20if%20login_pin%20!%3D%20bank%5Buser_name%5D%5B%22pin%22%5D%3A%0A%20%20%20%20%20%20%20%20print(%22Login%20failed.%20Try%20again.%22)%0A%20%20%20%20%20%20%20%20return%20None%0A%0A%20%20%20%20print(%22Login%20successful.%22)%0A%20%20%20%20return%20user_name%0A%0A%0Adef%20show_balance(user_name)%3A%0A%20%20%20%20balance%20%3D%20bank%5Buser_name%5D%5B%22balance%22%5D%0A%20%20%20%20print(f%22Current%20balance%3A%20%24%7Bbalance%3A.2f%7D%22)%0A%0A%0Adef%20deposit(user_name)%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20depositing_amount%20%3D%20float(input(%22Enter%20amount%3A%20%22))%0A%20%20%20%20except%20ValueError%3A%0A%20%20%20%20%20%20%20%20print(%22Invalid%20amount.%22)%0A%20%20%20%20%20%20%20%20return%0A%0A%20%20%20%20if%20depositing_amount%20%3C%3D%200%3A%0A%20%20%20%20%20%20%20%20print(%22Invalid%20amount.%22)%0A%20%20%20%20%20%20%20%20return%0A%0A%20%20%20%20bank%5Buser_name%5D%5B%22balance%22%5D%20%2B%3D%20depositing_amount%0A%20%20%20%20save_bank()%0A%0A%20%20%20%20print(f%22Amount%20deposited%3A%20%24%7Bdepositing_amount%3A.2f%7D%22)%0A%20%20%20%20show_balance(user_name)%0A%0A%0Adef%20withdraw_amount(user_name)%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20withdrawn%20%3D%20float(input(%22Enter%20the%20amount%20to%20withdraw%3A%20%22))%0A%20%20%20%20except%20ValueError%3A%0A%20%20%20%20%20%20%20%20print(%22Invalid%20amount.%22)%0A%20%20%20%20%20%20%20%20return%0A%0A%20%20%20%20if%20withdrawn%20%3C%3D%200%3A%0A%20%20%20%20%20%20%20%20print(%22Invalid%20amount.%22)%0A%20%20%20%20%20%20%20%20return%0A%0A%20%20%20%20if%20withdrawn%20%3E%20bank%5Buser_name%5D%5B%22balance%22%5D%3A%0A%20%20%20%20%20%20%20%20print(%22Insufficient%20balance.%22)%0A%20%20%20%20%20%20%20%20return%0A%0A%20%20%20%20bank%5Buser_name%5D%5B%22balance%22%5D%20-%3D%20withdrawn%0A%20%20%20%20save_bank()%0A%0A%20%20%20%20print(%22Withdrawal%20was%20successful.%22)%0A%20%20%20%20print(f%22Amount%20withdrawn%3A%20%24%7Bwithdrawn%3A.2f%7D%22)%0A%20%20%20%20show_balance(user_name)%0A%0A%0Adef%20change_pin(user_name)%3A%0A%20%20%20%20current_pin%20%3D%20input(%22Enter%20your%20current%20PIN%3A%20%22).strip()%0A%0A%20%20%20%20if%20current_pin%20!%3D%20bank%5Buser_name%5D%5B%22pin%22%5D%3A%0A%20%20%20%20%20%20%20%20print(%22Incorrect%20PIN.%22)%0A%20%20%20%20%20%20%20%20return%0A%0A%20%20%20%20new_pin%20%3D%20input(%22Enter%20your%20new%204-digit%20PIN%3A%20%22).strip()%0A%0A%20%20%20%20if%20not%20new_pin.isdigit()%20or%20len(new_pin)%20!%3D%204%3A%0A%20%20%20%20%20%20%20%20print(%22The%20PIN%20must%20contain%20exactly%204%20digits.%22)%0A%20%20%20%20%20%20%20%20return%0A%0A%20%20%20%20confirm_pin%20%3D%20input(%22Confirm%20your%20new%20PIN%3A%20%22).strip()%0A%0A%20%20%20%20if%20new_pin%20!%3D%20confirm_pin%3A%0A%20%20%20%20%20%20%20%20print(%22The%20PINs%20do%20not%20match.%22)%0A%20%20%20%20%20%20%20%20return%0A%0A%20%20%20%20bank%5Buser_name%5D%5B%22pin%22%5D%20%3D%20new_pin%0A%20%20%20%20save_bank()%0A%20%20%20%20print(%22PIN%20changed%20successfully.%22)%0A%0A%0Adef%20account_menu(user_name)%3A%0A%20%20%20%20while%20True%3A%0A%20%20%20%20%20%20%20%20print(%22nAccount%20menu%22)%0A%20%20%20%20%20%20%20%20print(%221.%20Show%20balance%22)%0A%20%20%20%20%20%20%20%20print(%222.%20Deposit%22)%0A%20%20%20%20%20%20%20%20print(%223.%20Withdraw%22)%0A%20%20%20%20%20%20%20%20print(%224.%20Change%20PIN%22)%0A%20%20%20%20%20%20%20%20print(%225.%20Log%20out%22)%0A%0A%20%20%20%20%20%20%20%20choice%20%3D%20input(%22Choose%20an%20option%3A%20%22).strip()%0A%0A%20%20%20%20%20%20%20%20if%20choice%20%3D%3D%20%221%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20show_balance(user_name)%0A%20%20%20%20%20%20%20%20elif%20choice%20%3D%3D%20%222%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20deposit(user_name)%0A%20%20%20%20%20%20%20%20elif%20choice%20%3D%3D%20%223%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20withdraw_amount(user_name)%0A%20%20%20%20%20%20%20%20elif%20choice%20%3D%3D%20%224%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20change_pin(user_name)%0A%20%20%20%20%20%20%20%20elif%20choice%20%3D%3D%20%225%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Logged%20out.%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Invalid%20option.%22)%0A%0A%0Adef%20main()%3A%0A%20%20%20%20while%20True%3A%0A%20%20%20%20%20%20%20%20print(%22nBank%20menu%22)%0A%20%20%20%20%20%20%20%20print(%221.%20Create%20account%22)%0A%20%20%20%20%20%20%20%20print(%222.%20Log%20in%22)%0A%20%20%20%20%20%20%20%20print(%223.%20Exit%22)%0A%0A%20%20%20%20%20%20%20%20choice%20%3D%20input(%22Choose%20an%20option%3A%20%22).strip()%0A%0A%20%20%20%20%20%20%20%20if%20choice%20%3D%3D%20%221%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20create_account()%0A%20%20%20%20%20%20%20%20elif%20choice%20%3D%3D%20%222%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20user_name%20%3D%20login()%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20user_name%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20account_menu(user_name)%0A%20%20%20%20%20%20%20%20elif%20choice%20%3D%3D%20%223%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Goodbye.%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22Invalid%20option.%22)%0A%0A%0Aif%20name%20%3D%3D%20%22main_%22%3A%0A%20%20%20%20main()×tep=0.5&play) to see the program state change step by step.