I am learning python and I am challenging myself to write a little program that asks the user the base price of a car, then saves the base price to a variable called base. It has two other variables called tax and license that are percentages. So it gets the base price, gets seven (7) percent of the base price, and adds it to the base price. It does the same for the license fee, etc.
However, what I want to know is that when it runs:
print "\nAfter taxes the price is: ", (base * tax / 100 + base)
how can I save the result to another variable so that the next line I dont have to write:
print "\nAfter taxes and license fee the price is: ", (base*tax / 100)+(base*license /100) + base? Rewriting it feels very redundant and like I’m wasting time by calculation something that has already been calculated.
I want to save the result of the first print line to a variable called after_tax so that then I could write:
print "\nAfter taxes and license fee the price is: ", after_tax + (base*license /100)
(I want the first print command to also save the result of the math calculation to a variable called after_tax so that I can reuse the result without having to retype out the entire calculation to get the result again).
Below is the code in it’s entirety:
#Car salesman calculations program.
base = int(raw_input("What is the base price of the car?" + "\n"))
tax = 7
license = 4
dealer_prep = 500
destination_charge = 1000
print "\nAfter taxes the price is: ", (base * tax / 100 + base)
print "\nAfter taxes and license fee the price is: ", (base*tax / 100)+(base*license /100) + base
print "\nAfter taxes and license fee and dealer prep the price is: ", (base*tax / 100)+(base*license /100) + base + dealer_prep
print "\nAfter taxes, license fees, dealer prep, and destination charge, the total price is: ", (base*tax / 100)+(base*license /100) + base + dealer_prep + destination_charge
raw_input("\nPress the enter key to close the window.")
you could do all your calculations up front. i recommend giving the variables (a, b, c, etc) smarter names than I did here, but this is illustrative enough.