我的代码遇到AttributeError,我不知道如何修复它
这是我的课:
class Reservation:
def __init__(self, passengerName, departureCity, destinationCity, dateOfTravel, timeOfTravel, numberOfTickets):
self.passengerName = passengerName
self.departureCity = departureCity
self.destinationCity = destinationCity
self.dateOfTravel = dateOfTravel
self.timeOfTravel = timeOfTravel
self.numberOfTickets = numberOfTickets
reservationList = list()
^{pr2}$
File "C:/Users//Desktop/pld/ticket_reservation5.py", line 183, in <module>
main()
File "C:/Users//Desktop/pld/ticket_reservation5.py", line 176, in main
reservation.write(reservation.passengerName + "," + reservation.departureCity + "," + reservation.destinationCity +
AttributeError: type object 'Reservation' has no attribute 'write'
您的单个保留对象没有写入属性。您需要调用文件的write方法并使用对象的数据填充字符串。在
另请注意,由于您使用的是上下文管理器(open()作为u),所以您不必执行
file.close()
。经理会为你做的。在另外,
file
是一个内置项,所以您不想覆盖它。您需要在变量名后附加一个下划线来区分它as described in PEP8reservation只是遍历list reservationList的迭代器,它没有定义任何写函数。 从文本文件中读取数据有三种方法。在
以下是打开并读取文件的示例:
^{pr2}$这样做,并确保列表中的最后一个字符串有一个
\n
,这样每当不同的用户在新行中输入信息时,该字符串就会出现:相关问题 更多 >
编程相关推荐