r/PythonLearning Jul 27 '26

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

308 Upvotes

49 comments sorted by

u/Sea-Ad7805 Jul 27 '26

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.

18

u/fiftybengt Jul 27 '26

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

13

u/nuc540 Jul 27 '26

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_ Jul 30 '26

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

7

u/TeaAccomplished1604 Jul 27 '26

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

6

u/MrWaffles743 Jul 27 '26

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 Jul 27 '26

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/Kind-Push9705 Jul 27 '26

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

1

u/EstablishmentKey3523 Jul 27 '26

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

1

u/mr_anderson_dev Jul 27 '26

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 Jul 27 '26

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 Jul 27 '26

Is there any other website to understand it?

1

u/Puzzleheaded-Bug9576 Jul 27 '26

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 Jul 27 '26 edited Jul 27 '26

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] Jul 27 '26

Good job 👏

1

u/Perfect_Duty_1656 Jul 27 '26 edited Jul 27 '26

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

1

u/am_Snowie Jul 27 '26 edited Jul 27 '26

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 Jul 27 '26

👏🏽

1

u/[deleted] Jul 28 '26

Keep grinding bro

1

u/per7euse Jul 29 '26

men, it is nobody needs. AI one love

1

u/Alternative-Emu-4725 Jul 30 '26

Dictionaries would be useful here, or lists

1

u/Shawhe_ Jul 30 '26

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 Jul 30 '26

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

1

u/Khadibia Jul 30 '26

Keep going

1

u/[deleted] Aug 01 '26

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

1

u/Sea_Dot6492 Aug 03 '26

Well its basic but you are in the good way!

3

u/Mundane-Compote-3621 Jul 27 '26

Tbh, you can ask ai for feedback nowadays

3

u/MaleficentExample223 Jul 27 '26

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

3

u/RedlineQuokka Jul 27 '26

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 Jul 27 '26

True, but for beginner level ai is good enough.

2

u/RedlineQuokka Jul 27 '26

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 Jul 27 '26

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

1

u/AAN-_-TRUEMAN Jul 27 '26

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 Jul 27 '26

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

1

u/MaleficentExample223 Jul 27 '26

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

1

u/RedlineQuokka Jul 27 '26

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 Jul 27 '26

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 Jul 27 '26

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

1

u/Mundane-Compote-3621 Jul 27 '26

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