Python3宠物店折扣计算函数实现及交互式程序开发求助
宠物店折扣规则实现任务
需求说明
某宠物店执行如下折扣规则:客户购买1只及以上宠物,且至少购买5件非宠物商品时,可享受非宠物商品总价20%的折扣(宠物商品不参与折扣)。需完成以下任务:
- 实现函数
def discount(prices, isPet, nItems),该函数接收交易信息:prices[i]为第i件商品原价,isPet[i]标记该商品是否为宠物; - 编写交互式程序,让收银员依次输入每件商品价格(输入-1作为终止符)、是否为宠物(输入Y/N),将输入存入列表后调用上述函数,展示折扣金额及最终应付金额。
现有代码
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()
代码问题修复与优化
现有代码存在逻辑错误与功能缺失,以下是修正后的完整实现:
1. 修复价格校验逻辑
原negativeNumber函数逻辑完全失效,替换为有效的价格校验函数,同时在输入阶段即时拦截无效数据:
def validate_price(price): if price < -1: print("无效价格:价格不能低于-1") return False return True
2. 完善折扣计算函数
原函数未输出折扣金额,且未处理不满足折扣条件的场景,优化后:
def discount(prices, isPet, nItems): pet_cost = 0 pet_count = 0 non_pet_cost = 0 non_pet_count = 0 for i in range(nItems): if isPet[i]: pet_cost += prices[i] pet_count += 1 else: non_pet_cost += prices[i] non_pet_count += 1 discount_amount = 0.0 final_amount = pet_cost + non_pet_cost if pet_count >= 1 and non_pet_count >= 5: print("您可享受折扣") discount_amount = 0.2 * non_pet_cost final_amount = pet_cost + non_pet_cost - discount_amount print(f"折扣金额: {discount_amount:.2f}") print(f"最终应付金额: {final_amount:.2f}")
3. 优化交互式输入流程
将输入提示改为中文,删除冗余调用,同时增加非数字输入的异常处理:
完整优化后代码
def discount(prices, isPet, nItems): pet_cost = 0 pet_count = 0 non_pet_cost = 0 non_pet_count = 0 for i in range(nItems): if isPet[i]: pet_cost += prices[i] pet_count += 1 else: non_pet_cost += prices[i] non_pet_count += 1 discount_amount = 0.0 final_amount = pet_cost + non_pet_cost if pet_count >= 1 and non_pet_count >= 5: print("您可享受折扣") discount_amount = 0.2 * non_pet_cost final_amount = pet_cost + non_pet_cost - discount_amount print(f"折扣金额: {discount_amount:.2f}") print(f"最终应付金额: {final_amount:.2f}") def validate_price(price): if price < -1: print("无效价格:价格不能低于-1") return False return True def main(): prices = [] isPet = [] while True: try: price = int(input("请输入商品价格(输入-1结束录入):")) if not validate_price(price): continue if price == -1: break prices.append(price) # 若无需记录商品名称,可删除此行 item_name = input("请输入商品名称:") choice = input("是否为宠物(Y/N)?") isPet.append(choice.strip().upper() == 'Y') print() except ValueError: print("无效输入:请输入有效的数字价格") nItems = len(prices) discount(prices, isPet, nItems) if __name__ == "__main__": main()
功能说明
- 输入阶段:支持逐个录入商品信息,自动校验价格有效性,对非数字、低于-1的输入给出错误提示;
- 计算阶段:自动统计宠物与非宠物商品的数量及总价,满足折扣条件时自动计算20%优惠;
- 输出阶段:清晰展示折扣金额与最终应付金额,保留两位小数提升可读性。
内容的提问来源于stack exchange,提问作者pevans590
相关产品推荐
相关产品推荐

