如何从if语句中提取折扣率

时间:2016-09-18 23:00:47

标签: python if-statement module

我需要计算这些书的总购买价格。折扣由购买的书籍数量决定。这些在if语句中分开。我怎样才能让我的代码完全正常工作?我目前收到此错误消息:

Error message that is displayed

这是我的代码:

BOOK_PRICE = 100

def main():
    numBooks = int(input("Enter the number of books you will be purchasing: "))
    discountPrice(numBooks)
    subTotalPrice(numBooks)
    #I have a problem getting the returned subTotal price
    totalPrice(theSubTotalPrice,discountRate)

def subTotalPrice (numBooks):
    theSubTotalPrice = numBooks * BOOK_PRICE
    print("Your subTotal is: $ ",theSubTotalPrice)
    return theSubTotalPrice
def totalPrice (theSubTotalPrice):
    theTotalPrice = theSubTotalPrice * discountRate
    print("Your Total Price is: $ ", theTotalPrice)
def discountPrice (numBooks):
    if numBooks <= 0 and numBooks <= 9:
        print(" ")
        discountRate = .00
        return discountRate
    if numBooks >= 10 and numBooks <= 19:
        print("Since you are ordering",numBooks,"books you will receive a 20% discount!")
        #how do any of these discountRates get put back to call them in a different module?
        discountRate = .20
        return discountRate
    if numBooks >= 20 and numBooks <= 49:
        print("Since you are ordering",numBooks,"books you will receive a 30% discount!")
        discountRate = .30
        return discountRate
    if numBooks >= 50 and numBooks <= 99:
        print("Since you are ordering",numBooks,"books you will receive a 40% discount!")
        discountRate = .40
        return discountRate
    if numBooks >= 100:
        print("Since you are ordering",numBooks,"books you will receive a 50% discount!")
        discountRate = .50
        return discountRate
main()

3 个答案:

答案 0 :(得分:1)

这是编辑过的代码。第一个错误是您从函数返回值,但没有为返回值分配变量。在函数内创建的变量仅存在于函数内部。研究下面的代码,看看我做了哪些改变。问你是否有问题。你还有另一个bug。你有numBooks&lt; = 0作为条件。它应该是numBooks&gt; = 0.最后一件事,感谢复制和粘贴而不是截图。

BOOK_PRICE = 100

def main():
    numBooks = int(input("Enter the number of books you will be purchasing: "))
    discountRate = discountPrice(numBooks)
    theSubTotalPrice = subTotalPrice(numBooks)
    #I have a problem getting the returned subTotal price
    totalPrice(theSubTotalPrice,discountRate)

def subTotalPrice (numBooks):
    theSubTotalPrice = numBooks * BOOK_PRICE
    print("Your subTotal is: $ ",theSubTotalPrice)
    return theSubTotalPrice
def totalPrice (theSubTotalPrice, discountRate):
    totalDiscount = theSubTotalPrice * discountRate
    theTotalPrice = theSubTotalPrice - totalDiscount
    print("Your Discount is: $ ", totalDiscount)
    print("Your Total Price is: $ ", theTotalPrice)
def discountPrice (numBooks):
    if numBooks >= 0 and numBooks <= 9:
        print(" ")
        discountRate = 0.00
        return discountRate
    if numBooks >= 10 and numBooks <= 19:
        print("Since you are ordering",numBooks,"books you will receive a 20% discount!")
        #how do any of these discountRates get put back to call them in a different module?
        discountRate = .20
        return discountRate
    if numBooks >= 20 and numBooks <= 49:
        print("Since you are ordering",numBooks,"books you will receive a 30% discount!")
        discountRate = .30
        return discountRate
    if numBooks >= 50 and numBooks <= 99:
        print("Since you are ordering",numBooks,"books you will receive a 40% discount!")
        discountRate = .40
        return discountRate
    if numBooks >= 100:
        print("Since you are ordering",numBooks,"books you will receive a 50% discount!")
        discountRate = .50
        return discountRate
main()

这是我用各种输入运行时得到的结果:

>>> 
===== RESTART: C:\Users\Joe\Desktop\scripts\Stack_overflow\book_price.py =====
Enter the number of books you will be purchasing: 9

Your subTotal is: $  900
Your Total Price is: $  0.0
>>> 
===== RESTART: C:\Users\Joe\Desktop\scripts\Stack_overflow\book_price.py =====
Enter the number of books you will be purchasing: 9

Your subTotal is: $  900
Your Total Price is: $  0.0
>>> 
===== RESTART: C:\Users\Joe\Desktop\scripts\Stack_overflow\book_price.py =====
Enter the number of books you will be purchasing: 19
Since you are ordering 19 books you will receive a 20% discount!
# I need to take these two values and subtract them. 1900(subtotal) - 380 (discountprice)
Your subTotal is: $  1900
Your Total Price is: $  380.0
>>> 
===== RESTART: C:\Users\Joe\Desktop\scripts\Stack_overflow\book_price.py =====
Enter the number of books you will be purchasing: 50
Since you are ordering 50 books you will receive a 40% discount!
Your subTotal is: $  5000
Your Total Price is: $  2000.0
>>> 

答案 1 :(得分:0)

您的上一个功能目前写为

def totalPrice(theTotalPrice):

但你的论点应该是

def totalPrice(numBooks):

实际上,你似乎在main中将正确的参数传递给了这个函数,这只是实际函数定义中的一个错误。

通常,如果您看到“(某些变量)未定义”等错误,请通过传递正确的函数参数来确保变量在范围内。

答案 2 :(得分:0)

基本上,你没有给你的功能提供他们需要的所有变量。在eligibleDiscounts中,您引用了变量totalPrice。这个价值在哪里定义?它应该是BOOK_PRICE * numBooks吗?

totalPrice中的类似内容。如果您想了解numBooks,则需要传递numBooks

我认为您要做的是计算totalPrice,并将其传递给eligibleDiscount。然后让eligibleDiscount返回discountRate。即以return discountRate结束。应在每个if / elif声明下定义哪个。不只是最后一个。

然后将所有内容传递到totalPrice