gpt4 book ai didi

python - 类型错误: 'Aircraft' 对象不可调用

转载 作者:太空宇宙 更新时间:2023-11-04 10:22:37 24 4
gpt4 key购买 nike

我正在编写 Python 教程,但一直遇到 TypeError: 'Aircraft' object is not callable

这是完整的错误信息:

    Traceback (most recent call last):
File "/usr/lib/python3.4/code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "/home/johanvergeer/Documenten/Training/Pluralsight/Python- Fundamentals/airtravel.py", line 29, in aircraft
return self._aircraft()
TypeError: 'Aircraft' object is not callable

这是我正在处理的 Python 代码:

class Flight:
"""A flight with a particular passenger aircraft"""

def __init__(self, number, aircraft):
if not number[:2].isalpha():
raise ValueError("No airline code in '{}'".format(number))
if not number[:2].isupper():
raise ValueError("Invalid airline code '{}'".format(number))
if not (number[2:].isdigit() and 4 >= len(number[2:]) >= 3):
raise ValueError("Invalid route number '{}'".format(number))
self._number = number
self._aircraft = aircraft

rows, seats = self._aircraft.seating_plan()
self._seating = [None] + [{letter: None for letter in seats} for _ in
rows]

def number(self):
return self._number

def airline(self):
return self._number[:2]

def aircraft(self):
return self._aircraft()

def _parse_seat(self, seat):
"""Parse a seat designator into a valid row and letter.

Args:
seat: a seat designator such as 12F.

Returns:
A tuple containing an integer and a string for row and seat.
"""
row_numbers, seat_letters = self._aircraft.seating_plan()

letter = seat[-1]
if letter not in seat_letters:
raise ValueError("Invalid seat letter {}".format(letter))

row_text = seat[:-1]
try:
row = int(row_text)
except ValueError:
raise ValueError("Invalid seat row {}".format(row_text))

if row not in row_numbers:
raise ValueError("Invalid row number {}".format(row))
return row, letter

def allocate_seat(self, seat, passenger):
"""Allocate a seat to a passenger.

Args:
seat: A seat designator such as '12C' or '21F'.
passenger: The passenger name.

Raises:
ValueError: If the seat is unavailable.
"""

row, letter = self._parse_seat(seat)

if self._seating[row][letter] is not None:
raise ValueError("Seat {} already occupied".format(seat))

self._seating[row][letter] = passenger

def relocate_passenger(self, from_seat, to_seat):
"""Relocate a passenger to a different seat.

Args:
from_seat: The existing seat assigned to the passenger to be moved.
to_seat: The new seat designator.
"""

from_row, from_letter = self._parse_seat(from_seat)

if self._seating[from_row][from_letter] is None:
raise ValueError(
"No passenger to relocate in seat {}".format(from_seat))

to_row, to_letter = self._parse_seat(to_seat)

if self._seating[to_row][to_letter] is not None:
raise ValueError("Seat {} is already occupied".format(to_seat))

self._seating[to_row][to_letter] = self._seating[from_row][from_letter]
self._seating[from_row][from_letter] = None

def num_available_seats(self):
return sum(sum(1 for s in row.values() if s is None)
for row in self._seating
if row is not None)

def make_boarding_cards(self, card_printer):
for passenger, seat in sorted(self._passenger_seats()):
card_printer(passenger, seat, self.number(), self.aircraft())

def _passenger_seats(self):
""" An iterable series of passenger seating allocations"""
row_numbers, seat_letters = self._aircraft.seating_plan()
for row in row_numbers:
for letter in seat_letters:
passenger = self._seating[row][letter]
if passenger is not None:
yield (passenger, "{}{}".format(row, letter))


class Aircraft:
def __init__(self, registration, model, num_rows, num_seats_per_row):
self._registration = registration
self._model = model
self._num_rows = num_rows
self._num_seat_per_row = num_seats_per_row

def registration(self):
return self._registration

def model(self):
return self._model

def seating_plan(self):
return (range(1, self._num_rows + 1),
"ABCDEFGHJK"[:self._num_seat_per_row])


def make_flight():
f = Flight("BA758", Aircraft("G-EUPT", "Airbus A319", num_rows=22,
num_seats_per_row=6))
f.allocate_seat("12A", "Guido van Rossum")
f.allocate_seat("15F", "Bjorne Stroustrup")
f.allocate_seat("15E", "Anders Hejlsberg")
f.allocate_seat("1C", "John McCarthy")
f.allocate_seat("1D", "Richard Hickey")
return f


def console_card_printer(passenger, seat, flight_number, aircraft):
output = "| Name : {0}" \
" Flight : {1}" \
" Seat: {2}" \
" Aircraft : {4}" \
" |".format(passenger, flight_number, seat, aircraft)
banner = "+" + "-" * (len(output) - 2) + "+"
border = "|" + " " * (len(output) - 2) + "|"
lines = [banner, border, output, border, banner]
card = "\n".join(lines)
print(card)
print()

如果有人能帮助我,我将不胜感激。

最佳答案

问题出在你的类 Flight 中:

def aircraft(self):
return self._aircraft()

self._aircraft() 试图将 self._aircraft 用作函数,但它是 Aircraft 类型的对象。将您的 Flight.aircraft 方法更改为

def aircraft(self):
return self._aircraft

这将返回实际对象。

关于python - 类型错误: 'Aircraft' 对象不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31434015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com