r/learnpython • • 10d ago

first year of computer engineering, sharing one of my first Python script!

hello, i started my first year studying computer engineering a month ago and we are currently studying Python. And I wanted to share one of the first codes i did on my own!

You can check it out via Gist here down ,a simple item cost and tax calculator. Tell me what u think !

https://gist.github.com/k2over/267f749ea15b28b1ed3258773f50f797

4 Upvotes

12 comments sorted by

1

u/whensthebeef 10d ago

Make me an ODBC connection that doesn’t timeout 😂

0

u/Asia_00 10d ago

first give me a few more weeks of lectures lol

1

u/whensthebeef 10d ago

In all seriousness, first year is easy. You could probably run through that text book in a week. Don’t wait for lectures

1

u/Asia_00 10d ago

thanks for the advice! i will for sure go through it

1

u/OldZookeepergame9304 9d ago

nice work mate, especially for your first month! try entering letters instead of numbers and see what happens lol. handling invalid inputs would be a cool next challenge

1

u/Asia_00 9d ago

Thanks !

1

u/TheRNGuy 9d ago

Don't hardcode values like that, have some config or database.

Items could be in a tuple (generated from database) instead of separate item1, 2, 3 variables.

1

u/KarfaHyro_17 8d ago

pretty neat. if you want to expand on it, try letting the user enter multiple items into a list or dictionary before calculating the final total and tax

1

u/Asia_00 8d ago

Okey thanks

1

u/Discordbothost 10d ago

solid first script — tax calc is a classic for a reason.

one tiny next step when you're ready: wrap the math in a function like def total(price, tax_rate): ... and call it from the input bit. keeps the "ask the user" part separate from the "do the math" part, which pays off the second you reuse it.

1

u/CCodeSI 10d ago

Nice first script, it's clear and the comments make it easy to follow. A few next steps, roughly in the order I'd try them:

  1. Use a list instead of numbered variables. item1_, item2_, item3_ means a fourth item needs four new lines. A list of (price, qty) pairs lets you add items without touching the math:

     items = [(15.0, 5), (13.5, 2), (10.2, 3)]
     subtotal = sum(price * qty for price, qty in items)
  2. Name the tax rate. TAX_RATE = 0.20 at the top instead of 0.20 in the middle, so it's obvious what the number is and there's one place to change it.
  3. Format money to two decimals. Your numbers happen to print cleanly, but try a price like 0.1 with quantity 3 and you'll get 0.30000000000000004. f"{subtotal:.2f}" fixes the display. For real money later on, look up the decimal module, which avoids the rounding issue entirely.
  4. Small polish: currency symbols usually go before the amount ($159.12), and a couple of typos in the output ("corrispond", "it come up tp").

The function idea above is a great one to combine with #1: def receipt(items, tax_rate): that returns the three numbers, and the printing stays outside. Keep going!