手动关联和参数化操作,添加对座位类型(First头等/Business商务/Coach经济)的随机选择以及对不同客机编号及机票价格的随机选择(见下图)。运行时设置不变,运行回放脚本(脚本无错误,参数值替换正常),利用行程菜单“Itinerary”查看所订座位类型以及客机编号和价格
时间: 2024-09-30 09:14:08 浏览: 21
手动关联和参数化操作通常是指在程序设计中,将特定的行为或数据与输入参数关联起来,以便在运行时根据不同的参数值动态地执行不同的任务。在这个场景中,你可以创建一个函数或者类方法,它接受座位类型、客机编号和机票价格作为输入参数。例如:
```python
class FlightBooking:
def __init__(self, seat_type, flight_number, ticket_price):
self.seat_type = seat_type
self.flight_number = flight_number
self.ticket_price = ticket_price
def book_ticket(self):
print(f"Booked {self.seat_type} seat on Flight {self.flight_number} with a price of {self.ticket_price}")
def random_booking():
seat_types = ["First", "Business", "Coach"]
flight_numbers = range(1001, 1010) # 示例,实际应从数据库获取
prices = [random.randint(500, 5000) for _ in seat_types] # 随机价格范围
seat_type = seat_types[random.randint(0, len(seat_types) - 1)]
flight_number = random.choice(flight_numbers)
ticket_price = prices[seat_types.index(seat_type)]
booking = FlightBooking(seat_type, flight_number, ticket_price)
booking.book_ticket()
# 替换`random_booking()`中的参数
run_playback_script(random_booking)
# 查看行程信息
def view_itinerary(booked_flight):
print("Itinerary:")
print(f"Seat Type: {booked_flight.seat_type}")
print(f"Flight Number: {booked_flight.flight_number}")
print(f"Ticket Price: {booked_flight.ticket_price}")
view_itinerary(random_booking()) # 运行时显示预定的座位信息
阅读全文