我对python很新,我遇到了一些基本问题。
我似乎无法将大部分计算放在模块中。如果我这样做,结果将无法转移,它们将始终显示为0.0。
一旦我能够将计算放入模块中,我就可以将模块放入循环中并询问用户是否要重复操作。
这也是我的主要问题::我想“存储”每个项目的输出(displayResults)(项目编号,价格等),并在取消循环后打印所有项目。 / p>
谢谢!我正在努力解决这个问题。
这是我的代码:
#Mateo Marquez
#Oct. 8th, 2012
#P.O.S information system assigment
#
#Define Global Variables
TAX = 0.6
YELLOW_TAG = 0.10
BLUE_TAG = 0.20
RED_TAG = 0.25
GREEN_TAG = 0
#Main Module
def main():
tax_fee = 0.0
total_price = 0.0
introUser()
# I want this to be the mainCalc() function and return results.
productNumber=raw_input("Please enter the Product Number: ")
cost=float(raw_input("Please enter the cost of the selected product: "))
print " "
print "As you might have noticed, our discounts are color coded"
print "Yellow is 10%, Blue is 20% & Red is 25%"
print " "
tagDiscount=raw_input("Please enter the color tag: (yellow, blue, red or none)")
if tagDiscount == 'yellow':
print " "
print "You get a 10% discount"
total_price = (YELLOW_TAG*cost)
if tagDiscount == 'blue':
print " "
print "You get a 20% discount"
total_price = (BLUE_TAG*cost)
if tagDiscount == 'red':
print " "
print "You get a 25% discount"
total_price = (RED_TAG*cost)
if tagDiscount == 'none':
print " "
print "No discount for you!"
total_price = 0
print " "
print "~Remember~ this weekend is Tax Free in most of the country"
print "Green Tags designate if the product is tax free"
tagDiscount=raw_input("Does your product has a Green Tag? (yes or no)")
if tagDiscount == 'yes':
print " "
print "Good! your product is tax free"
tax_fee = 0
if tagDiscount == 'no':
print " "
print "I'm sorry, product", productNumber, "requires regular tax"
tax_fee = (TAX*total_price)
#I want this to be the end of the mainCalc() function
displayResults(total_price, tax_fee, cost, productNumber)
#Introduction function
def introUser():
print "Welcome to Wannabee's"
print "I'll gladly help you with your price question"
print "Let's start"
print " "
#Display results function
def displayResults(total_price, tax_fee, cost, productNumber):
print " "
print "Your Product Number: ", productNumber
print "Listed price of your product: $", cost
print "Your discount: $", total_price
print "Your Tax amount: $", tax_fee
print "Your grand total: $", (cost - total_price - tax_fee)
print " "
print "Your savings: ", ((cost-total_price)/cost*100),"%!"
main()
答案 0 :(得分:0)
为了保存相关例程使用的值,请将变量和使用它们的例程放在类中。以下代码定义了一个“POS”类和两个共享其变量的方法例程。自己。”方法中的表示法表示保存在实例“p”中的类变量,该实例是在使用p = POS()
实例化类时创建的。
该示例说明了如何存储变量;你需要根据需要调整输入和打印语句(这里是Python 3)。如果要在输入时存储项目并在最后打印它们,请在__init__
中创建一个空列表,在mainCalc()
中的列表中添加一个元组,然后打印出每个列表项在displayResults()
。
class POS:
def __init__(self):
self.taxrate = 0.06
self.disc = {"": 0.0, "yellow": 0.10, "blue": 0.20, "red": 0.25}
self.total = 0
self.savings = 0
self.tax = 0
def mainCalc(self, item, qty, cost, tag, taxable):
print(qty, "* Item", item, "@ ${:.2f}".format(cost),
"= ${:.2f}".format(qty*cost))
discount = cost * self.disc[tag]
if (tag):
print(" You get a", int(100*self.disc[tag]),
"% discount ${:.2f}".format(discount), "for", tag, "tag")
self.total += qty * (cost - discount)
self.savings += discount
if (taxable == "yes"):
tax = qty * cost * self.taxrate
self.tax += tax
print(" tax ${:.2f}".format(tax))
else:
print(" tax free")
def displayResults(self):
print("----------------")
print("Your total: ${:.2f}".format(self.total))
print("Your savings: ${:.2f}".format(self.savings))
print("Your total tax: ${:.2f}".format(self.tax))
print("Grand total: ${:.2f}".format(self.total + self.tax))
return
p = POS()
# Item Qty Price Tag Taxable
items = [(1492, 1, 1.95, "yellow", "yes"),
(1524, 2, 4.50, "blue", "no"),
(2843, 1, 6.95, "blue", "yes"),
(1824, 3, 2.29, "", "yes")]
for i in items:
p.mainCalc(*i)
p.displayResults()
运行示例会产生:
1 * Item 1492 @ $1.95 = $1.95 You get a 10 % discount $0.20 for yellow tag tax $0.12 2 * Item 1524 @ $4.50 = $9.00 You get a 20 % discount $0.90 for blue tag tax free 1 * Item 2843 @ $6.95 = $6.95 You get a 20 % discount $1.39 for blue tag tax $0.42 3 * Item 1824 @ $2.29 = $6.87 tax $0.41 ---------------- Your total: $21.39 Your savings: $2.49 Your total tax: $0.95 Grand total: $22.33
答案 1 :(得分:0)
您应该考虑以下约束:
def
声明(您对displayResults
的号召)后才能调用 。要改进代码,要么考虑程序应该从整体的角度出发,要么使用Dave答案中建议的类。