r/PythonLearning • • 21d ago

This is my first python project - what can I improve??

Post image
128 Upvotes

39 comments sorted by

•

u/Sea-Ad7805 20d ago

Run program above by clicking Memory Graph Web Debugger and see its state change step by step.

9

u/Good_Person_000 21d ago

Spend 2-3 days learning Tkinter, give it a UI and it will be a good project

2

u/Jealous-Beyond-1104 21d ago

Okay thank you

11

u/TowerOutrageous5939 21d 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 21d ago

Learn pastebin / sendong the github link because screenshots of code is not the best way to show smth

1

u/MeaningfullyGracious 20d ago

github gist is even easier for a single file, no repo setup needed

4

u/Far_Data_6654 21d 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.

1

u/Mountain_Rip_8426 20d ago

it's a match statement in python, although i'd say a dictionary might be better

1

u/Far_Data_6654 19d ago

so it is. learn something new everyday, i don’t use python much.

4

u/Ok_Relative_2291 21d 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 20d 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

1

u/ConnectAffect2346 5d ago

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🙂

3

u/deadeye_82_1 20d ago

Learn how to use pastebin. Pictures are a no-go for program code.

2

u/Ajay2574 20d ago

Nice work keep it up may be you can use match

2

u/Interesting-Tree-388 20d 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 20d 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 20d ago

I'm surprised no one caught the several misspelled words, all category, spelled category. Spelling matters

2

u/sentientland 19d 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 21d ago

the way you share code with people

1

u/MudFrosty1869 20d ago

Would be kinda good time to tell them how. Thankfully someone that isn’t useless to society in the comments already did.

1

u/SnooCalculations7417 20d ago

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.

1

u/MudFrosty1869 20d ago

Maybe take a look around dumbass xD

1

u/SnooCalculations7417 20d ago

I confirmed you contributed nothing. Thanks.

1

u/MudFrosty1869 20d ago

Stay ignorant and useless. Expect to be treated accordingly.

1

u/SnooCalculations7417 20d ago

Why are you so passionate about me? Failing to provide a means for this random redditor to share his code. That's not a screenshot. Touch grass my guy

1

u/Hot_Technology8777 21d ago

Looks good to me. Like someone else mentioned, name your stuff better for you and everyone elses sake.

1

u/TapEarlyTapOften 21d 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 21d ago

Create logic based on dict and use new line to remove extra print.

1

u/New_Cartoonist337 20d ago

Is so faking hard 🥀🥀 i want learning in python

1

u/nerfherder616 20d 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 18d ago

Have you write it yourself or with ai ? i can not write anything without ai myself yet

1

u/maybeflush 16d ago

add a docstring

1

u/Kaveh_Nazarlo 16d ago

Where did you learn Python bro ?

1

u/LunaFall23 1d ago

Coding in light mode has to be a crime you cannot prove me otherwise

0

u/ColdTrky 20d ago

Use Ai. Who cares about code in 1/2 years

3

u/Thewolf19111 20d ago

You plz go away we REAL devs only use AI as some sort of spell checker

1

u/ColdTrky 19d ago

Hahahaha delusional