[英]__init__() takes 3 positional arguments but 4 were given when run
你好
我有這個代碼 python 代碼,試圖制作一個父 class 出版物和 2 個子類(書籍和雜志)。
然后它嘗試創建一個 object 並根據列表中的用戶選擇將其添加到列表中
class Publication:
title=""
price=0.0
def __init__(self,title,price):
self.title=title
self.price=price
def display(self):
print("title: "+self.title)
print("\nprice: "+str(self.price))
class Book(Publication):
author=""
def __init__(self,title,price,author):
super().__init__(self,title,price)
self.author=author
def display(self):
print("title: "+self.title)
print(", price: "+ str(self.price))
print(", author: "+self.author)
class Magazine(Publication):
issue = ""
def __init__(self, title, price, issue):
super().__init__(self, title, price)
self.issue = issue
def display(self):
print("title: " + self.title)
print(", price: " + str(self.price))
print(", issue: " + self.issue)
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
choice=1;
bookList=[]
MagazineList=[]
while(choice!=0):
print("Publication System")
print("Select a service:")
print("1) Add Book")
print("2) Add Magazine")
print("3) Display Book Lists")
print("4) Display Magazine Lists")
print("5) Exit")
choice = int(input("Enter choice: "))
if choice == 1:
bookTitle=str(input("Enter the book title: "))
bookAuthor = str(input("Enter the book author: "))
bookPrice = float(input("Enter the book price: "))
book=Book(bookTitle,bookPrice,bookAuthor)
bookList.append(book)
elif choice == 2:
MagazineTitle=str(input("Enter the magazine title: "))
MagazineIssue = str(input("Enter the magazine issue: "))
MagazinePrice = float(input("Enter the magazine price: "))
magazine=Magazine(MagazineTitle,MagazinePrice,MagazineIssue)
MagazineList.append(magazine)
elif choice == 3:
for i in range(len(bookList)):
print(bookList[i]+"\n")
elif choice == 4:
for i in range(len(MagazineList)):
print(MagazineList[i]+"\n")
elif choice == 5:
print("Exiting!")
else:
print("Invalid choice!!")
當我運行它時,它向我顯示了這個錯誤:
Publication System
Select a service:
1) Add Book
2) Add Magazine
3) Display Book Lists
4) Display Magazine Lists
5) Exit
Enter choice: 1
Enter the book title: python fundamentals
Enter the book author: eric
Enter the book price: 40
Traceback (most recent call last):
File "/Users/zahraaal-nemer/Desktop/University/Level 10/Advanced Programming Language/Lab/Q1OtherGroups/main.py", line 64, in <module>
book=Book(bookTitle,bookPrice,bookAuthor)
File "/Users/zahraaal-nemer/Desktop/University/Level 10/Advanced Programming Language/Lab/Q1OtherGroups/main.py", line 19, in __init__
super().__init__(self,title,price)
TypeError: __init__() takes 3 positional arguments but 4 were given
我該如何解決? 我見過類似的問題,但沒有找到答案
super().__init__
是一個綁定方法,已經包含self
。 你不需要明確地傳遞self
。 例如,
class Book(Publication):
def __init__(self, title, price, author):
super().__init__(title, price)
self.author = author
# etc
在調用 class 方法時,您不需要顯式傳遞self
。
它應該是
super().__init__(title, price)
代替
super().__init__(self, title, price)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.