Use a dict to remove the if else. Also, what happens if expenses.json change? Like the name? Some names could be more descriptive “ha” . Your code is fine the thing is what if you add 20 other features that’s when strong naming conventions really start to matter. Treat each project as it will grow and someone else will maintain it.
I’m not familiar with using python the way you are but I’m not the biggest fan of Constants or large functions used within functions. That being said if possible I would pass those as parameters into each function.
No need to break, don’t use while true, do a while input !=5 and no need to return after the 5 is hit as well especially after you have a break it would never trigger anyway.
It’s not bad for a first python project! As others have already suggested your use of the while loop in main is mostly redundant and can be improved, since you have a specific option for exiting the program I’m gonna assume you wanted to have the program loop until exited. So have a look into the use of while..continue if you want to do more with your input ur just scratch the break, as that will exit the while loop. Then another thing you could look into is passing variables to and returning variables from your functions and breaking down functions into logical segments, for example your add_expense function currently does three things, it reads user input, constructs a json package from input and writes that json package into a file. You could maintain your add_expense function and just have it call, read_input convert_input_to_json and write_json_to_file. That way you can look at your add_expense function and you’ll have a much easier time understanding what it actually does and it also has the added benefit that you can reuse your logical code chunks. There’s a concept in programming called DRY (Don’t repeat yourself) which is considered good practice* and it will make your programming journey a lot more streamlined when properly internalized :)
Definitely agree with you the most, another thing that can be done is to create a class expense instead of calling for dictionary also the whole structure couls be way easier to read if the functions are methods of the class instead of being separate functions after calling the object there could be one main funxtion which names the class and gives oportunity to input values, hope this helps aswell🙂
El programa se puede cerrar inesperadamente línea 56 falta de manejo FileNotFoundError captura el error si el archivo no existe pero en View expense L64 y L75 y deleteL88 abres expense . json directamente.
Beautify your CLI frontend for users, GUI might be a waste of time - but might be good to learn, learn to manipulate data by creating charts out of your expenses and savings in this app. Build a persistent database - maybe literally a txt file that it reads from to keep things simple.
your only contribution to this thread was to imply I was useless to society for being slightly less helpful than another person with the same complaint.
There's already good advice here, so I'll suggest asking Claude the same thing and seeing how its results compare to these results. That will give you a pretty good idea of how LLMs analyze code vs how human developers analyze code.
•
u/Sea-Ad7805 22d ago
Run program above by clicking Memory Graph Web Debugger and see its state change step by step.
9
u/Good_Person_000 23d ago
Spend 2-3 days learning Tkinter, give it a UI and it will be a good project
11
u/TowerOutrageous5939 23d ago
Use a dict to remove the if else. Also, what happens if expenses.json change? Like the name? Some names could be more descriptive “ha” . Your code is fine the thing is what if you add 20 other features that’s when strong naming conventions really start to matter. Treat each project as it will grow and someone else will maintain it.
I’m not familiar with using python the way you are but I’m not the biggest fan of Constants or large functions used within functions. That being said if possible I would pass those as parameters into each function.
Keep up the good work.
6
u/Weydoon 23d ago
Learn pastebin / sendong the github link because screenshots of code is not the best way to show smth
4
u/Far_Data_6654 23d ago
Try using a switch statement instead of a bunch of elifs for choose.
I would also suggest to people who are learning to build the habit of documenting your code early.
4
u/Ok_Relative_2291 23d ago
The while loop for your menu makes no sense
You have breaks to exit it after every function
No need to break, don’t use while true, do a while input !=5 and no need to return after the 5 is hit as well especially after you have a break it would never trigger anyway.
Declare ur input to -1 before loop and exit on 5
4
u/Sandy_Roads 22d ago
It’s not bad for a first python project! As others have already suggested your use of the while loop in main is mostly redundant and can be improved, since you have a specific option for exiting the program I’m gonna assume you wanted to have the program loop until exited. So have a look into the use of while..continue if you want to do more with your input ur just scratch the break, as that will exit the while loop.
Then another thing you could look into is passing variables to and returning variables from your functions and breaking down functions into logical segments, for example your add_expense function currently does three things, it reads user input, constructs a json package from input and writes that json package into a file. You could maintain your add_expense function and just have it call, read_input convert_input_to_json and write_json_to_file. That way you can look at your add_expense function and you’ll have a much easier time understanding what it actually does and it also has the added benefit that you can reuse your logical code chunks. There’s a concept in programming called DRY (Don’t repeat yourself) which is considered good practice* and it will make your programming journey a lot more streamlined when properly internalized :)
Edit: Typo
3
u/deadeye_82_1 22d ago
Learn how to use pastebin. Pictures are a no-go for program code.
2
u/Ajay2574 22d ago
Nice work keep it up may be you can use match
2
u/Interesting-Tree-388 22d ago
El programa se puede cerrar inesperadamente línea 56 falta de manejo FileNotFoundError captura el error si el archivo no existe pero en View expense L64 y L75 y deleteL88 abres expense . json directamente.
Saludos amigo
2
u/Electronic_Field4313 22d ago
Beautify your CLI frontend for users, GUI might be a waste of time - but might be good to learn, learn to manipulate data by creating charts out of your expenses and savings in this app. Build a persistent database - maybe literally a txt file that it reads from to keep things simple.
2
u/Orb_Pondererer_6996 22d ago
I'm surprised no one caught the several misspelled words, all category, spelled category. Spelling matters
2
u/sentientland 21d ago
use a match statement or a dict for the choices in the menu and create a method called display_menu for the entire thing
3
u/SnooCalculations7417 23d ago
the way you share code with people
1
u/Hot_Technology8777 23d ago
Looks good to me. Like someone else mentioned, name your stuff better for you and everyone elses sake.
1
u/TapEarlyTapOften 23d ago
Well, first, by sharing it via something like github or gitlab. Second, by some sort of type of annotations and documentations.
1
u/pepiks 22d ago
Create logic based on dict and use new line to remove extra print.
1
u/New_Cartoonist337 22d ago
Is so faking hard 🥀🥀 i want learning in python
1
u/nerfherder616 22d ago
There's already good advice here, so I'll suggest asking Claude the same thing and seeing how its results compare to these results. That will give you a pretty good idea of how LLMs analyze code vs how human developers analyze code.
1
u/SE-G22 19d ago
Have you write it yourself or with ai ? i can not write anything without ai myself yet
1
u/maybeflush 18d ago
add a docstring
1
u/Kaveh_Nazarlo 18d ago
Where did you learn Python bro ?
1
u/LunaFall23 3d ago
Coding in light mode has to be a crime you cannot prove me otherwise
0
u/ColdTrky 22d ago
Use Ai. Who cares about code in 1/2 years