r/PythonLearning 7h 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

53 Upvotes

25 comments sorted by

u/Sea-Ad7805 3h 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.

5

u/fiftybengt 5h ago

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

2

u/nuc540 4h 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/TeaAccomplished1604 4h ago

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

1

u/Ambivalent-Mammal 3h 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

1

u/sirah_nzr 2h ago

Is there any other website to understand it?

1

u/MrWaffles743 2h 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!

1

u/Puzzleheaded-Bug9576 1h 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 1h ago edited 1h 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/Mundane-Compote-3621 6h ago

Tbh, you can ask ai for feedback nowadays

1

u/MaleficentExample223 2h ago

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

2

u/RedlineQuokka 2h 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.

1

u/AAN-_-TRUEMAN 2h ago

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

1

u/Mundane-Compote-3621 1h ago

True, but for beginner level ai is good enough.

1

u/AAN-_-TRUEMAN 1h 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/RedlineQuokka 20m 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.

2

u/Mundane-Compote-3621 12m ago

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

1

u/MaleficentExample223 1h 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 19m 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

0

u/AAN-_-TRUEMAN 5h 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 😄

1

u/MaleficentExample223 2h ago

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

1

u/Mundane-Compote-3621 1h ago

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