在Python中创建类时,如何在短时间内修复这些错误?
我需要完成一个与创建类有关的简短作业,我不确定我的代码有什么问题。下面是说明,下面是我的代码。请解释我的错误是什么,以及如何修复: 构建2个类。第一节课是“书本”课。book类有4个都是私有的变量。第一个是checkedOut,它是一个初始化为false的布尔值;title是一个由输入变量初始化的字符串;author是一个字符串,它由输入变量初始化;pages是一个整数,它也由输入变量初始化。该类还将有4个与is相关的函数。第一个将返回变量checkedOut。第二个将更改checkedOut的值。如果该值设置为true,则该值将更改为false,反之亦然。第三个函数将返回页数,最后一个函数将返回标题。打印图书对象时,其格式应为“标题作者页签出” 第二个类将被称为库。当库初始化时,它将创建一个名为collection的空字典。library类将有2个函数。第一个函数将被称为addBook,它将接受3个输入变量:title、author和pages。在该函数中,您将创建一个book对象,然后将其添加到字典中,标题作为键。第二个函数将获取一本书的标题,在字典中查找该书,并调用books函数来更改checkedOut状态。最后,当打印出库对象时,它将在单独的行上打印出库中的每本书 最后,库类将在名为main.py的python程序中实现 这是我的密码:在Python中创建类时,如何在短时间内修复这些错误?,python,class,Python,Class,我需要完成一个与创建类有关的简短作业,我不确定我的代码有什么问题。下面是说明,下面是我的代码。请解释我的错误是什么,以及如何修复: 构建2个类。第一节课是“书本”课。book类有4个都是私有的变量。第一个是checkedOut,它是一个初始化为false的布尔值;title是一个由输入变量初始化的字符串;author是一个字符串,它由输入变量初始化;pages是一个整数,它也由输入变量初始化。该类还将有4个与is相关的函数。第一个将返回变量checkedOut。第二个将更改checkedOut的
class Book:
title = str(input("Enter the title of the book. "))
author = str(input("Enter the author of the book. "))
pages = int(input("Enter the number of pages in the book. "))
checkedOut = False
def checked_Out(self):
print(checkedOut)
return checkedOut
def change_value_of_checkedOut(self):
if checkedOut == False:
checkedOut = True
print("Switched from False to True.")
elif checkedOut == True:
checkedOut = False
print("Switched from True to False.")
def return_pages(self):
print(pages)
return pages
def return_title(self):
print(title)
return title
class Library(Book):
def __init__(self):
collection = {}
def addBook(self, title, author, pages):
new_book = Book()
collection[title] = author
def change_checked_out_status(self, title):
if title in collection:
new_book.change_value_of_checkedOut(self)
else:
print("This book is not in the collection.")
我在这里做的有什么不对?当我尝试创建对象并在空闲状态下运行代码时,不断出现错误,结果是没有定义某个变量名。(0)下次,请粘贴错误
class Book:
title = str(input("Enter the title of the book. "))
author = str(input("Enter the author of the book. "))
pages = int(input("Enter the number of pages in the book. "))
checkedOut = False
def checked_Out(self):
print(checkedOut)
return checkedOut
(1) 就这一部分
checkedOut
未定义。你在哪里看到checkedOut?在功能的正上方已签出
。可以简短回答,添加self.
例如:
def checked_Out(self):
print(self.checkedOut)
return self.checkedOut
你真的不应该在那里写标题,作者之类的东西。它们成为类变量,而不是实例变量。这是不同的
(2) 如果您仍在使用2.x Python,请避免输入
使用raw_input
并去掉str
。这样更安全。在3.x中,您可以使用输入,它将始终是字符串(原始输入将始终返回字符串)。这也造成了问题
(3) 你有能力把一切都通缉起来。我通常很冷,但有点不好。不要称之为checked\u-Out
,这是非常不一致的,而且Python程序员更喜欢checked\u-Out
checkedOut
可以命名为checked\u out
并且,您可以冲突。不要将函数和变量命名得如此相似。(0)下次,请粘贴错误
class Book:
title = str(input("Enter the title of the book. "))
author = str(input("Enter the author of the book. "))
pages = int(input("Enter the number of pages in the book. "))
checkedOut = False
def checked_Out(self):
print(checkedOut)
return checkedOut
(1) 就这一部分
checkedOut
未定义。你在哪里看到checkedOut?在功能的正上方已签出
。可以简短回答,添加self.
例如:
def checked_Out(self):
print(self.checkedOut)
return self.checkedOut
你真的不应该在那里写标题,作者之类的东西。它们成为类变量,而不是实例变量。这是不同的
(2) 如果您仍在使用2.x Python,请避免输入
使用raw_input
并去掉str
。这样更安全。在3.x中,您可以使用输入,它将始终是字符串(原始输入将始终返回字符串)。这也造成了问题
(3) 你有能力把一切都通缉起来。我通常很冷,但有点不好。不要称之为
checked\u-Out
,这是非常不一致的,而且Python程序员更喜欢checked\u-Out
checkedOut
可以命名为checked\u out
并且,您可以冲突。不要将函数和变量命名得如此相似。分别创建类,并分别获取输入。此外,您还想指出,每个变量都是一个实例变量,不在本地范围内:
class Book:
def __init__(self, title, author, pages):
self.title = title
self.author = author
self.pages = pages
self.checkedOut = False
def checked_Out(self):
print(self.checkedOut) # it has to be the instance variable
return self.checkedOut
def change_value_of_checkedOut(self):
if self.checkedOut == False:
self.checkedOut = True
print("Switched from False to True.")
elif self.checkedOut == True:
self.checkedOut = False
print("Switched from True to False.")
def return_pages(self):
print(self.pages)
return self.pages
def return_title(self):
print(self.title)
return self.title
class Library:
def __init__(self):
collection = {}
def addExistingBook(self, book):
collection[book.title] = book.author
def addNewBook(self, title, author, pages): # create a book
new_book = Book(title, author, pages)
collection[title] = new_book.author # access the author
def change_checked_out_status(self, title):
if title in collection.keys():
title.change_value_of_checkedOut()
else:
print("This book is not in the collection.")
然后,在main
函数中添加其余部分:
def main():
# if you are using Python 2.x, change input() to raw_input()
title = str(input("Enter the title of the book. "))
author = str(input("Enter the author of the book. "))
pages = int(input("Enter the number of pages in the book. "))
myBook = Book(title, author, pages)
myLib = Library()
myLib.addExistingBook(myBook)
分别创建类,并分别获取输入。此外,您还想指出,每个变量都是一个实例变量,不在本地范围内:
class Book:
def __init__(self, title, author, pages):
self.title = title
self.author = author
self.pages = pages
self.checkedOut = False
def checked_Out(self):
print(self.checkedOut) # it has to be the instance variable
return self.checkedOut
def change_value_of_checkedOut(self):
if self.checkedOut == False:
self.checkedOut = True
print("Switched from False to True.")
elif self.checkedOut == True:
self.checkedOut = False
print("Switched from True to False.")
def return_pages(self):
print(self.pages)
return self.pages
def return_title(self):
print(self.title)
return self.title
class Library:
def __init__(self):
collection = {}
def addExistingBook(self, book):
collection[book.title] = book.author
def addNewBook(self, title, author, pages): # create a book
new_book = Book(title, author, pages)
collection[title] = new_book.author # access the author
def change_checked_out_status(self, title):
if title in collection.keys():
title.change_value_of_checkedOut()
else:
print("This book is not in the collection.")
然后,在main
函数中添加其余部分:
def main():
# if you are using Python 2.x, change input() to raw_input()
title = str(input("Enter the title of the book. "))
author = str(input("Enter the author of the book. "))
pages = int(input("Enter the number of pages in the book. "))
myBook = Book(title, author, pages)
myLib = Library()
myLib.addExistingBook(myBook)
粘贴输出,否则您将得不到帮助。粘贴输出,否则您将得不到帮助。