FightHawk16 2022-03-27 16:52 采纳率: 100%
浏览 202
已结题

Python:代码报错 AttributeError: 'list' object has no attribute 'get_name_harvard_format',各位帮我瞅瞅怎么解决

问题相关代码,请勿粘贴截图
 class Person:
    def __init__(self, name, age):
        self.__names=name
        self.__age=age
    def __str__(self):
        return "Person: Name='"+" ".join(self.__names)+"', Age="+repr(self.__age)
    def get_full_name(self):
        return " ".join(self.__names)
    def bithday(self):
        self.__age += 1
    def get_age(self):
        return self.__age
    def get_name_harvard_format(self):
        initial=[]
        for word in self.__names:
            for letter in word:
                if letter.isupper():
                    initial.append(letter)
        del initial[-1]
        return self.__names[-1],"".join(initial)

class Book:
    def __init__(self, authors, title, publisher, publishYear, publishPlace):
        self.__authors=authors
        self.__title=title
        self.__publisher=publisher
        self.__publishYear=publishYear
        self.__publishPlace=publishPlace
    def display_author(self):
        print(self.__authors)
    def get_harvard_reference(self):
        self.__authors.get_name_harvard_format()
        return "".join(self.__authors),f'{self.__publishYear}, {self.__title}, {self.__publisher}, {self.__publishPlace}'
    def __str__(self):
        return self.get_harvard_reference()

Output

programmer = Person(['Augusta', 'Ada', 'King'], 36)
engineer = Person(['Charles', 'Babbage'], 50)
actualAuthor = Person(['Sydney','Padua'], 30)
book = Book([programmer, engineer, actualAuthor],
'The Thrilling Adventures of Lovelace and Babbage',
'Penguin Books', 2015, 'Westminster')
print(book)
book.display_author()

运行结果及报错内容

Traceback (most recent call last):
File "c:\Users\rongt\OneDrive - University of South Australia\OOP\WEEK5\practical.py", line 62, in
print(book)
File "c:\Users\rongt\OneDrive - University of South Australia\OOP\WEEK5\practical.py", line 53, in str
return self.get_harvard_reference()
File "c:\Users\rongt\OneDrive - University of South Australia\OOP\WEEK5\practical.py", line 50, in get_harvard_reference
self.__authors.get_name_harvard_format()
AttributeError: 'list' object has no attribute 'get_name_harvard_format'

我的解答思路和尝试过的方法

导进class Person然后得到来自class Person.get_name_harvard_format的返回到class Book.get_harvard_reference
这里是两道题, Person是第一道题答案做出来的,所以并为考虑如何实现Book的答案

我想要达到的结果

King, AA, Babbage, C & Padua, S 2015, The Thrilling Adventures of Lovelace and
Babbage, Penguin Books, Westminster.

[King, AA, Babbage, C, Padua, S]

  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 系统已结题 4月11日
      • 已采纳回答 4月3日
      • 修改了问题 3月27日
      • 修改了问题 3月27日
      • 展开全部

      悬赏问题

      • ¥15 工具变量 时间序列相关问题
      • ¥15 正弦波的峰值查找算法
      • ¥15 关于lwip的pbuf数据提取问题
      • ¥50 请求关于BBS数据集的资源分享
      • ¥15 设计一份接口自动化测试报告
      • ¥15 手机安装kali后ifconfig 提示错误
      • ¥15 用C++求矩阵的特征值
      • ¥30 求解答(自动忽略本括号内容)
      • ¥15 根据C语言小型成绩管理系统画一个流程图
      • ¥15 Javaweb的增删改查