r/learnpython • u/Asia_00 • 17d 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
1
u/OldZookeepergame9304 17d 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/TheRNGuy 16d 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 16d 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/Discordbothost 17d 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 17d 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:
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)Name the tax rate.
TAX_RATE = 0.20at the top instead of0.20in the middle, so it's obvious what the number is and there's one place to change it.Format money to two decimals. Your numbers happen to print cleanly, but try a price like
0.1with quantity3and you'll get0.30000000000000004.f"{subtotal:.2f}"fixes the display. For real money later on, look up thedecimalmodule, which avoids the rounding issue entirely.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!
1
u/whensthebeef 17d ago
Make me an ODBC connection that doesn’t timeout 😂