项目3:按照以下要求定义⼀个游乐园门票类,并创建实例调⽤函数,完成⼉童和⼤⼈的总票价统计(⼈数不定,由键盘输⼊的⼈数个数来决定) 1)平⽇票价100元 2)周末票价为平日票价的120% 3)儿童票为成人平日票价的半价
时间: 2024-03-02 15:49:54 浏览: 15
好的,我明白了。以下是游乐园门票类的定义和实例调用函数的代码:
```python
class AmusementParkTicket:
def __init__(self, adults=0, children=0):
self.adults = adults
self.children = children
def get_total_price(self):
weekday_price = 100
weekend_price = 120
child_discount = 0.5
total_adult_price = self.adults * weekday_price
total_child_price = self.children * (weekday_price * child_discount)
# Check if it's a weekend and apply the weekend rate if necessary
is_weekend = datetime.datetime.today().weekday() in [5,6] # 5 and 6 are Saturday and Sunday
if is_weekend:
total_adult_price *= (weekend_price / 100)
return total_adult_price + total_child_price
```
这个类有两个属性:`adults` 和 `children`,分别代表成人和儿童的数量。构造函数 `__init__` 初始化这两个属性为 0。
`get_total_price` 函数计算总票价。平日票价是 100 元,周末票价是平日票价的 120%,儿童票是成人平日票价的半价。如果今天是周末,就会应用周末票价。
现在可以使用这个类来统计总票价,下面是一个示例程序:
```python
import datetime
# 输入成人和儿童数量
adults = int(input("请输入成人数量: "))
children = int(input("请输入儿童数量: "))
# 创建门票对象并计算总票价
ticket = AmusementParkTicket(adults, children)
total_price = ticket.get_total_price()
# 输出总票价
print("总票价为: {} 元".format(total_price))
```
这个程序会要求用户输入成人和儿童的数量,然后创建门票对象并计算总票价。最后输出总票价。