简体   繁体   中英

in Python how do I print the result of a calculation and then save the result to a variable?

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.")

In Python you can't do this in the same line. But what you can do is define the after_tax variable first, and then just print it out:

after_tax = base * tax / 100 + base
print "\nAfter taxes the price is: ", after_tax

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.

a = (base * tax / 100)
b = (base*license /100)
c = a + b + base
d = c + dealer_prep
e = d + destination_charge

print "\nAfter taxes the price is: ", a + base
print "\nAfter taxes and license fee the price is: ", c
print "\nAfter taxes and license fee and dealer prep the price is: ", d
print "\nAfter taxes, license fees, dealer prep, and destination charge, the total price is: ", e

raw_input("\nPress the enter key to close the window.")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM