Python 游泳消费系统代码示例 - 简单易懂
下面是一个简单的游泳消费系统的 Python 代码示例:
class SwimmingPool:
def __init__(self, name, price_per_hour):
self.name = name
self.price_per_hour = price_per_hour
def calculate_cost(self, hours):
return self.price_per_hour * hours
class Customer:
def __init__(self, name, age):
self.name = name
self.age = age
def can_swim(self):
return self.age >= 18
def main():
pool = SwimmingPool('ABC Swimming Pool', 10)
name = input('请输入顾客姓名:')
age = int(input('请输入顾客年龄:'))
customer = Customer(name, age)
if customer.can_swim():
hours = int(input('请输入游泳时间(小时):'))
cost = pool.calculate_cost(hours)
print('顾客姓名:', customer.name)
print('游泳时间(小时):', hours)
print('消费金额:', cost)
else:
print('未满18岁,不可游泳')
if __name__ == '__main__':
main()
这个系统中,SwimmingPool 类表示一个游泳池,包括名称和每小时的价格。calculate_cost 方法用于计算游泳消费金额。
Customer 类表示一个顾客,包括姓名和年龄。can_swim 方法用于判断顾客是否满足游泳条件(年龄大于等于 18 岁)。
在主函数 main 中,首先创建了一个 SwimmingPool 对象,然后通过输入获取顾客的姓名和年龄。根据顾客的年龄判断是否满足游泳条件,如果满足,则通过输入获取游泳时间,并调用 SwimmingPool 的 calculate_cost 方法计算消费金额。最后输出顾客的姓名、游泳时间和消费金额。如果不满足游泳条件,则输出提示信息。
原文地址: https://www.cveoy.top/t/topic/qqCl 著作权归作者所有。请勿转载和采集!