【发布时间】:2011-11-01 01:24:01
【问题描述】:
我正在学习 python,我正在挑战自己编写一个小程序,询问用户汽车的基本价格,然后将基本价格保存到一个名为 base 的变量中。它还有另外两个变量,称为tax 和license,它们是百分比。因此,它获取基本价格,得到base 价格的seven (7) 百分比,并将其添加到基本价格中。许可费等也是如此。
但是,我想知道的是,它何时运行:
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 的结果保存到一个名为 after_tax 的变量中,这样我就可以编写:
print "\nAfter taxes and license fee the price is: ", after_tax + (base*license /100)
(我希望第一个print 命令也将数学计算的结果保存到一个名为after_tax 的变量中,这样我就可以重复使用结果,而不必重新输入整个计算以再次获得结果)。
下面是完整的代码:
#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.")
【问题讨论】:
标签: python variables printing command