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

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条回答 默认 最新

  • 陈年椰子 2022-03-27 17:38
    关注

    看不出层次, 建议用插入代码块功能,贴一下代码

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

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

悬赏问题

  • ¥20 Windows10系统命令行调用
  • ¥15 php环境如何实现国密SM2相关功能
  • ¥15 关于#单片机#的问题:K210 例程里面提示我iomem.h: No such file or directory
  • ¥15 LSPI算法的大问题
  • ¥15 java导出word 动态填充数据
  • ¥20 扑克的算牌公式及软件制作
  • ¥20 如何通过云图中RPG去计算云图上不同位置的值?
  • ¥15 请问不小心下载到了钓鱼软件怎么办?
  • ¥15 求国博抢票 求国博抢票 有的私
  • ¥50 swiftui @query 报错