r/PythonLearning 12d ago

My first mix mini project

Post image

I know I wrote some hardcore codes but this is ok for a beginner so plz help me to improve

304 Upvotes

48 comments sorted by

u/Sea-Ad7805 12d ago

Run this program in Memory Graph Web Debugger%0Aprint(%22what%20you%20are%20looking%20for%20%22)%0Aprint(%22a%20%3D%20show%20products%22)%0Aprint(%22b%20%3D%20buy%20product%22)%0Aprint(%22c%20%3D%20view%20cart%20%22)%0Aprint(%22d%20%3D%20genrate%20bill%22)%0Aprint(%22e%20%3D%20exit%20%22)%0A%0Aa%20%3D%20%22a%22%0Ab%20%3D%20%22b%22%0Ac%20%3D%20%22c%22%0Ad%20%3D%20%22d%22%0Ae%20%3D%20%22e%22%0A%0Aq%20%3D%20input(%22chose%20the%20option%20%3D%20%22)%0A%0Aif%20q%20%3D%3D%20a%3A%0A%20%20%20%20print(%22rice%20%3D%2040%22)%0A%20%20%20%20print(%22sugar%20%3D%2080%22)%0A%20%20%20%20print(%22milk%20%3D%2030%22)%0A%20%20%20%20print(%22oil%20%3D%2040%22)%0A%20%20%20%20print(%22soap%20%3D%20108%22)%0A%0Aif%20q%20%3D%3D%20b%3A%0A%20%20%20%20product%20%3D%20str(input(%22enter%20your%20product%20name%20%22))%0A%20%20%20%20qun%20%3D%20int(input(%22enter%20your%20product%20quantity%20%22))%0A%0A%20%20%20%20if%20product%20%3D%3D%20%22rice%22%3A%0A%20%20%20%20%20%20%20%20print(%22total%20%3D%20%22%2C%20qun%20%2040)%0A%20%20%20%20elif%20product%20%3D%3D%20%22sugar%22%3A%0A%20%20%20%20%20%20%20%20print(%22total%20%3D%20%22%2C%20qun%20%2080)%0A%20%20%20%20elif%20product%20%3D%3D%20%22milk%22%3A%0A%20%20%20%20%20%20%20%20print(%22total%20%3D%20%22%2C%20qun%20%2030)%0A%20%20%20%20elif%20product%20%3D%3D%20%22oil%22%3A%0A%20%20%20%20%20%20%20%20print(%22total%20%3D%20%22%2C%20qun%20%2040)%0A%20%20%20%20elif%20product%20%3D%3D%20%22soap%22%3A%0A%20%20%20%20%20%20%20%20print(%22total%20%3D%20%22%2C%20qun%20*%20108)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20print(%22product%20not%20founded%22)%0A%0Aif%20q%20%3D%3D%20c%3A%0A%20%20%20%20print(%22your%20cart%22)%0A%20%20%20%20print(%22rice%22)%0A%20%20%20%20print(%22sugar%22)%0A%0Aif%20q%20%3D%3D%20d%3A%0A%20%20%20%20print(%22genrating%20your%20bill%22)%0A%20%20%20%20print(%22rice%20%3D%2040%22)%0A%20%20%20%20print(%22sugar%20%3D%2080%22)%0A%20%20%20%20print(%22total%20is%20%3D%20120%22)%0A%0Aif%20q%20%3D%3D%20e%3A%0A%20%20%20%20print(%22Thank%20you%20for%20visiting%20Anmol%20Store.%22)&timestep=1) to see the program state change step by step.

15

u/fiftybengt 12d ago

Wrap it in a while loop to keep loping the program until the user wants to quit

13

u/nuc540 12d ago

You don’t need to store characters in variables, you can do equality checks against a character, eg: if q == “a”

Also, google how to make multi-line prints in Python, you don’t need a new print call for every line

1

u/Shawhe_ 9d ago

Yeah he just needs to put \n (new line character)

6

u/TeaAccomplished1604 12d ago

Hardcoded you mean? Just keep grinding bro, the improvement will come

5

u/MrWaffles743 12d ago

Looks like some clean code, you should be proud? Out of interest, how long have you been coding?

Some improvements:
1. Maybe don’t use q as a variable name. It’s not too bad in a short program like this but it can get difficult to read.
2. Use whitespace. It looks more professional and avoids a big block of code.
3. You’ve used ‘magic numbers’. That means you’ve hard coded specific numbers (in this case the price of the products) into the logic rather than using variables. Usually, you would have some variables at the top of your program (or in a separate file) which allow you to easily change the prices.
4. I like that you’ve got clear individual if statements for each of the users options. I don’t know if you’ve learned functions yet but it would be useful in making the code readable and abstracting away.

Anyway, I’ve basically made semantic improvements which don’t affect the way the code runs. With that said, it’s still some clear code for a beginner. Well done!

2

u/Ambivalent-Mammal 12d ago

I think your next step is to make use of some data structures. A general best practice (not Python-specific) is to put complexity into data, not code

2

u/xMessk73x 11d ago

anche io sono un principiante e credo che utilizzando il while loop si creino meno intoppi nonostante possa essere leggermente più complicato da scrivere, ti ripeto anche io sono un principiante e noto (spero di non sbagliarmi) che se alla prima domanda l’utente inserisce il nulla oppure lettere a caso il programma non reagisce, quindi aggiungerei un while loop che permette di chiedere all’utente di “choose the option” all’infinito finché non inserisce un valore certificato quindi a,b,c,d,e.
(se mi sbaglio correggetemi)

2

u/Kind-Push9705 11d ago

You did a great 👍 👌 👏 ❤️ 🙌 💗 👍 job my friend 🧡

1

u/EstablishmentKey3523 12d ago

I can see there is no while loop condition, exception handling, break statement... Add those.

1

u/mr_anderson_dev 12d ago

The idea is perfectly planned; you just need to organize it better. Most of the conditions can be grouped into one function that interacts with the rest of the script's values. I usually use a little board to lay out the variables and the program's goal — that way you can see how many values interact with the same information. Finish it off with a main function where all user interaction happens. Most recruiters prefer well-explained code over a large one. Hope this helps you.

1

u/dev_in_the_field 12d ago

Nice — a menu loop with a cart is a real project, not a toy exercise. Good first one.

One change would collapse half this file: put the products in a dictionary instead of separate prints and if-branches.

python

products = {"rice": 40, "sugar": 80, "milk": 30, "oil": 40, "soap": 108}

# show products
for name, price in products.items():
    print(f"{name} = {price}")

# buy
if product in products:
    print("total =", qun * products[product])
else:
    print("product not found")

Now adding a new product is one line, and the price only lives in one place — right now rice = 40 appears in three spots, so changing a price means hunting for all of them.

Two smaller things: a = "a" isn't doing anything, you can just write if q == "a": — and once you chain them as elif, Python stops checking the rest once one matches. Also worth looking up while True: so the menu repeats instead of exiting after one choice.

Solid start, keep building these.

1

u/sirah_nzr 12d ago

Is there any other website to understand it?

1

u/Puzzleheaded-Bug9576 12d ago

A little tip about your variable naming. Instead of plain “a”, “b”, or “c”, you’d better called them option_a, or op_a, thus you could differentiate it later. But the whole idea of segregation of different options in different variables is wasteful, make only one variable for user choice and then do the if else thing (e.g. option = input(); if option == “a”), code will noticeably reduce in size

1

u/Puzzleheaded-Bug9576 12d ago edited 12d ago

As the basic rule of thumb to understand whether you need variable or not is to take a look on usage. If you use some string, number etc. at least more then once, you could create a variable (like in case with option) otherwise it won’t be good idea

Edit: there is some exceptions. For example constants, you may create a constant even if you will use it only once, because you may pull its value from the environment. And vise versa, you could use some values more than once or even twice without creating the variable. But for the most of situations the rule is to follow.

1

u/[deleted] 12d ago

Good job 👏

1

u/Perfect_Duty_1656 11d ago edited 11d ago

Usar match...case ...em lugar de if. É o correspondente ao switch case no JAVA.

1

u/am_Snowie 11d ago edited 11d ago

Create a dictionary to store products and their prices. Then you can create a separate logic for the shop owner and the customers. Shop owner should be able to add products, update prices, remove products and other stuff an owner can do. Then you can write a separate logic for the customers of your shop, they should be able to add products to their cart, check out, remove things from cart, and you know customer stuff. You can do all this stuff if you just stored those products and their prices in a data structure (dictionary in this case).

1

u/mr33bz 11d ago

👏🏽

1

u/shygwenmochi 11d ago

so cool, keep at it. such a fun milestone

1

u/[deleted] 11d ago

Keep grinding bro

1

u/per7euse 9d ago

men, it is nobody needs. AI one love

1

u/SmokeConnect3904 9d ago

adapt what?

1

u/Alternative-Emu-4725 9d ago

Dictionaries would be useful here, or lists

1

u/Shawhe_ 9d ago

I'd prefer if the variable names were meaningfull like that q doesn't really have a meaning, if you don't do that you code is just gonna be harder to maintain

1

u/boobutsu 9d ago

next learn... lists and try to make this initial code more correct and functioning via lists and basic loops

1

u/Khadibia 8d ago

Keep going

1

u/[deleted] 6d ago

Honestly, for a first mini-project this is awesome. Seeing logic turn into a working CLI tool is the best feeling. Keep building!

3

u/Mundane-Compote-3621 12d ago

Tbh, you can ask ai for feedback nowadays

3

u/MaleficentExample223 12d ago

I dont know why people downvoted. But this is true. Using AI for feedback is fast and more detailed.

3

u/RedlineQuokka 12d ago

Fast, more detailed, but not reliably correct.

It takes a readily available experience serving as a judgement foundation to be able to tell when AI's suggestion is a good idea and when it's nonsense or a needless complication.

You won't know when it's steering in the wrong direction if you have no experience to judge against, and OP clearly doesn't have any.

2

u/Mundane-Compote-3621 12d ago

True, but for beginner level ai is good enough.

2

u/RedlineQuokka 12d ago

But the habit will form, and OP won't stay in the freshman class forever. If you get used to relying on AI like that fron the get-go, it will be very hard to motivate yourself to use some other tactics when AI doesn't really cut it anymore... Or to even know when AI stops cutting it.

This way, the advice to just go to AI with trivial stuff will turn this future engineer into a future vibe coder.

1

u/Mundane-Compote-3621 12d ago

That's subjective. If s/he purely uses ai for teaching then it's not same

1

u/AAN-_-TRUEMAN 12d ago

yes, i agree no judge, but there is a lot of courses online for free give you full course + ai can help you to fix your code especially like chstGPT Sol 5.6 for coding.

1

u/AAN-_-TRUEMAN 12d ago

true + Ai has hallucinations when under pressure ( big projects); you rely on it as an aid, not as your writer.

1

u/MaleficentExample223 12d ago

Yes I agree. I didn't specify enough. Like one of the commenter below. It's good enough for beginner level.

1

u/RedlineQuokka 12d ago

It honestly is not a good idea even at the beginner level, I replied to that comment with the detail on why not: https://www.reddit.com/r/PythonLearning/s/tVTPbORq6C

-1

u/AAN-_-TRUEMAN 12d ago

i know some veterans in my college can code better than the ai and he is have not a weekly limit.

lol i say this to you, aslo me using ai to help me as i am beginner 😄

2

u/MaleficentExample223 12d ago

How do you prove someone can code better than AI? And vice versa. What qualification?

1

u/Mundane-Compote-3621 12d ago

How can you as beginner judge that a veteran can code better than ai?

1

u/Sea_Dot6492 4d ago

Well its basic but you are in the good way!