Python理发店程序添加Class类与循环方法求助 | 编程入门
理发店Python程序添加Class类的实现方案
你的现有代码已经实现了基础的输入输出、条件判断和数组存储,但缺少面向对象的封装(Class)和循环处理逻辑。下面直接给你改造后的完整代码,并拆解说明怎么把Class融入进去:
核心改造思路
用两个类来封装功能:
Customer类:专门存储单个客户的性别、年龄、发型类型等信息,把零散的变量变成统一的对象Barbershop类:管理理发店的客户列表、接待流程、发型推荐逻辑,同时加入循环实现多客户接待
完整改造代码
class Customer: def __init__(self, gender, age, hair_type): self.gender = gender self.age = age self.hair_type = hair_type def __str__(self): return f"{self.gender}, {self.age}岁, {self.hair_type}发质" class Barbershop: def __init__(self): # 初始化现有客户列表,转成Customer对象存储 self.customers = [ Customer("boy", "14", "curly"), Customer("girl", "12", "straight"), Customer("Man", "28", "wavy"), Customer("Woman", "26", "curly") ] self.name = "Sid's Barbershop" def welcome(self): print(f"Welcome to {self.name}") while True: choice = input("Would you like to get your haircut today? (yes/no) ").lower() if choice == "yes": print("Great come in and take a seat") return True elif choice == "no": print("Sounds good come back when you're ready") return False else: print("请输入yes或no") def get_new_customer(self): # 获取新客户信息并创建Customer对象 gender = input("Do you consider yourself a boy, girl, man or woman? ") age = input("How old are you? ") hair_type = input("What kind of hair type do you have? ") new_customer = Customer(gender, age, hair_type) self.customers.append(new_customer) print(f"已记录你的信息:{new_customer}") return new_customer def recommend_haircut(self): # 客户查询与发型推荐 while True: try: search = int(input('Select an index to search an entry (0到{}): '.format(len(self.customers)-1))) if 0 <= search < len(self.customers): selected_customer = self.customers[search] print(f"查询到客户:{selected_customer}") # 根据客户信息推荐发型 if search == 0: print("Alright so far so good would you like to get a fade and your hair cut by 2 inches?") elif search == 1: print("Ok so you have straight hair would you just like a trim?") elif search == 2: print("So you have wavy hair which is hard to deal with do you want to go bald?") elif search == 3: print("Ok you have super long and curly hair do you want to try something new and perm your hair straight?") else: # 针对新添加的客户,通用推荐 print(f"Based on your {selected_customer.hair_type} hair, we can offer a personalized haircut!") break else: print(f"请输入0到{len(self.customers)-1}之间的数字") except ValueError: print("请输入有效的数字索引") def run(self): # 循环接待客户 while True: if not self.welcome(): break self.get_new_customer() self.recommend_haircut() # 询问是否继续接待下一个客户 next_customer = input("Is there another customer? (yes/no) ").lower() if next_customer != "yes": print("Thanks for coming!") break # 启动理发店程序 if __name__ == "__main__": shop = Barbershop() shop.run()
关键部分说明
Customer类:
__init__方法:创建客户对象时自动初始化性别、年龄、发质属性__str__方法:定义打印客户对象时的显示格式,让输出更直观
Barbershop类:
__init__:初始化理发店名称和现有客户列表,把原来的二维数组转成Customer对象,更易管理welcome:加入循环处理用户的无效输入(比如输入非yes/no的情况)get_new_customer:把原来零散的input和append操作封装成方法,直接返回Customer对象,代码更整洁recommend_haircut:加入异常处理和边界判断,防止用户输入无效索引,同时支持新添加的客户推荐run:外层循环实现多客户接待,完成一个客户流程后询问是否继续
循环逻辑:
- 多处用while循环处理用户的无效输入,避免程序崩溃
- 外层循环支持连续接待多个客户,符合理发店的实际场景
为什么要这么改?
原来的代码用零散列表存储客户信息,逻辑分散,以后要加新功能(比如记录客户消费、添加更多服务)会很麻烦。用Class封装后:
- 代码模块化,每个类负责单一功能,可读性更强
- 客户信息变成对象,方便扩展属性(比如手机号、偏好发型)
- 重复逻辑可以封装成方法,减少代码冗余
内容的提问来源于stack exchange,提问作者Siddharth
相关产品推荐
相关产品推荐

