1

我对python很陌生,我遇到了一些基本问题。

  1. 我似乎无法将大部分计算放在一个模块中。如果我这样做了,结果将无法转移,它们将始终显示为 0.0。

  2. 一旦我能够将我的计算放入一个模块中,我就可以将模块放入一个循环中并询问用户是否要重复该操作。

  3. 这也是我的主要问题 :: 我想“存储”每个项目(项目编号、价格等)的输出(显示结果),并在取消循环后打印所有项目。

谢谢!我很难弄清楚这一点。

这是我的代码:

#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()
4

2 回答 2

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
于 2012-10-08T14:54:38.500 回答
0

您应该考虑以下约束:

  • 一个函数只能在它被一个语句定义之后def才能被调用(你对displayResults.
  • 函数不能访问在另一个函数定义的主体中本地定义的变量。

要改进您的代码,请从整体角度考虑程序应该如何流动,或者使用 Dave 回答中建议的类。

于 2012-10-08T17:01:49.313 回答