0

A pet shop wants to give a discount to its clients if they buy one or more pets and at least five other items. The discount is equal to 20 percent of the cost of the other items, but not the pets. Implement a function def discount(prices, isPet, nItems) The function receives information about a particular sale. For the ith item, prices[i] is the price before any discount, and isPet[i] is true if the item is a pet. Write a program that prompts a cashier to enter each price and then a Y for a pet or N for another item. Use a price of –1 as a sentinel. Save the inputs in a list. Call the function that you implemented, and display the discount.

def discount(prices, isPet, nItems):
    i = 0
    petCost = 0
    pets = 0
    items = 0
    itemsCost = 0
    while i < nItems:
        if isPet[i]:
            petCost += prices[i]
            pets += 1
        else:
            itemsCost += prices[i]
            items += 1
        i += 1
    if pets >= 1 and items >= 5:
        print("You receive discount")
        discountAmount = 0.2 * itemsCost
        finalAmount = petCost + itemsCost - discountAmount
        print("Final amount is", finalAmount)

def negativeNumber(price):
    while price > -1:
        if price < -1:
            print("Invalid price")
            return True
        else:
            return False
    
def main():
    prices = []
    isPet = []
    while True:
        price = int(input("Enter Price(-1 to quit): "))
        if price > -1:
            prices.append(price)
            itemName = input("Name of Item:")
            choice = input("Is it a pet(Y/N)? ")
            if choice == 'Y' or choice == 'y':
                isPet.append(True)
            else:
                isPet.append(False)
            print("")
        else:
            break
    nItems = len(prices)
    negativeNumber(price)    
    discount(prices, isPet, nItems)
main()
pevans590
  • 1
  • 2
  • 3
    Welcome to SO! Please [Take the Tour](https://stackoverflow.com/tour), read: [What types of questions should I avoid asking?](https://stackoverflow.com/help/dont-ask), [What topics can I ask about here?](https://stackoverflow.com/help/on-topic) and [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Also look at [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822). What is the question? What have you tried and how does the behavior differ from what you expect? – Michael Ruth Oct 22 '22 at 22:27
  • Clearly a homework question. Just seeking guidance on how to approach the setup. Is this really just a single function (def Discount) or would multiple functions get defined? – pevans590 Oct 22 '22 at 22:52
  • You need to write a function and a program which calls the function. So it will look something like: `def discount(prices, isPet, nItems):...; if __name__ == "__main__":...` – Michael Ruth Oct 23 '22 at 00:19
  • Thanks for the comment. Starting to tackle it now. – pevans590 Oct 23 '22 at 00:55
  • A [sentinel value](https://en.wikipedia.org/wiki/Sentinel_value) is an invalid value which the code can test for in order to know when to stop. In this case, -1 is excellent because nothing has a price of -1. – Michael Ruth Oct 23 '22 at 01:20
  • Thank you for that clarification. I was uncertain about the "-1" because it is not a natural number. I would have surmised that "0" could serve as the sentinel. – pevans590 Oct 23 '22 at 03:21
  • Please provide enough code so others can better understand or reproduce the problem. – Community Oct 23 '22 at 07:36
  • I'm stuck - because if the user enters a negative number other than "-1" the program is supposed to advise that an invalid price was entered and then continue on and allow the user to enter more items and prices. Instead, my program just stops. Not sure if I am supposed to edit my "def negativeNumber" or add something to main() - I've monkeyed around with the "def negativeNumber" by adding in the True/False lines, but same result. – pevans590 Oct 23 '22 at 21:42

0 Answers0