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