Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Any way to make my code shorter to perhaps to 2 variebles while still keeping it to 2 decimals?

Trying to make my code shorter just a few points away from #1.

I tried shrinking it but its just too complicated.

Taxers = int(input("Will you be eating in 1 For Yes 2 For No:"))
TotalCostA = TotalCostB * 0.05
Tax = TotalCostA
TotalCostF = "{:.2f}".format(Tax)
TotalCostT = Tax + TotalCostB
if Taxers == 2:
    print("Tax = 0")
    print("Your total pizza cost it", TotalCostB, "kd")
else:
    print("Tax = ", TotalCostF)
    print("Your total pizza cost it", TotalCostT, "kd")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can get rid of the extra variables and use f-strings for a slightly more fluent experience:

Taxers = int(input("Will you be eating in 1 For Yes 2 For No:"))
TotalCostA = TotalCostB * 0.05
TotalCostT = TotalCostA + TotalCostB
if Taxers == 2:
    print("Tax = 0")
    print(f"Your total pizza cost it {TotalCostB} kd")
else:
    print(f"Tax = {TotalCostA:.2f}")
    print(f"Your total pizza cost it {TotalCostT} kd")

Shortening it even further will reduce code readability. On that note, I strongly recommend you to follow the Python naming conventions and improve the variable names to something more descriptive, e.g. cost_with_tax.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading