Alina - 4 months ago 14

Python Question

I need to calculate the total purchase price of the books. The discount is determined by the amount of books purchased. Those are split within the if statements. How can I have my code fully work? I am currently getting this error message:

Here's my code:

`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()

Answer

Here is the edited code. The first error was that you were returning the values from the functions, but there was no variable being assigned to the return value. Variables created inside a function lives only inside the function. Study the code below and see what changes I made. Ask if you have questions. Also you had another bug. You have numBooks <=0 as a condition. It should be numBooks >= 0. One last thing, thanks for the copy and paste instead of screenshot.

```
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()
```

Here is what I get when I ran it with various inputs:

```
>>>
===== 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
>>>
```