2016-09-18 224 views
0

我需要计算书籍的总购买价格。折扣取决于购买的书籍数量。这些被分成if语句。我如何让我的代码充分发挥作用?我目前收到此错误信息:如何从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() 
+0

我很好奇:是不是更多的工作来发布截图那么这将是粘贴文本? ;) – zvone

+0

不是,剪切工具需要几秒钟! – Alina

+0

我看到:)但文字更好。它在各种显示器上看起来更好,可以搜索内容,复制和粘贴作品,占用更少的存储空间和带宽......你应该粘贴文本;) – zvone

回答

1

这里是编辑的代码。第一个错误是您从函数中返回值,但没有将变量分配给返回值。在函数中创建的变量只存在于函数中。研究下面的代码并查看我所做的更改。询问你是否有问题。你也有另一个错误。作为条件,您有numBooks < = 0。它应该是numBooks> = 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 
>>> 
+0

这工作,谢谢。现在,如果我想从小计中扣除折扣价格,我会如何将它添加到它? – Alina

+0

我编辑了totalPrice函数。现在计算总折扣,然后我从SubTotalPrice中减去折扣。 – Joe

+0

是的这工作!非常感谢你的帮助! – Alina

0

你的最后一个功能目前写成

def totalPrice(theTotalPrice): 

但你的说法应该是

def totalPrice(numBooks): 

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

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

+0

更改后,我仍然收到相同的错误。 – Alina

+1

@Alina确保所有变量都在您的函数中可用。你从来没有通过'discountRate'。阅读错误信息,他们完全告诉你*问题是什么。 – CoryKramer

+0

我在每个if语句中声明了discountRate,但是没有通过。 – Alina

0

基本上,你没有给你的函数所需的所有变量。在eligibleDiscounts中,您引用了一个变量totalPrice。价值被定义在哪里?它的意思是BOOK_PRICE * numBooks

类似的事情在totalPrice。如果你想让它知道numBooks,你需要通过numBooks英寸

我想你想要做的是计算totalPrice,并将其传递到eligibleDiscount。然后有eligibleDiscount返回discountRate。即以return discountRate结尾。应在每个if/elif声明下定义。不只是最后一个。

然后将所有内容都传递给totalPrice

+0

好的,如果我能弄清楚,我会很快重写和更新。 – Alina

+0

我更新了我的原始代码,并在有问题的地方做了笔记。你有什么想法我可以修复它吗? – Alina