Develop a Python program. Define a class in Python and use it to create an objec
ID: 3721900 • Letter: D
Question
Develop a Python program. Define a class in Python and use it to create an object and display its components. Define a Book class where a book contains the following components (attributes): an author, a title, and number of pages. PLEASE DO NOT HARD CODE ANY CONSTANT VALUE IN THE SOURCE CODE: THIS MUST BE DONE WITH USER INPUT.
This class includes several methods: to change the values of these attributes, and to display their values. Separately, the “main program” must:
request the corresponding data from input and create a book object.
invoke a method to change the value of one of its attributes
invoke a method that displays the value of each attribute.
I've provided the code I have so far. I am stuck on the changing attribute method as I was trying to use validation to confirm if they wanted to change the title, author, or page numbers but don't know how to proceed.
-Book Book.py * ÷ ! *.- Book.py x Book ~/Py 1 Book.py 2 IlI External Lib3 class Book: #Book class def-init--(self): #Method creating the Book object with it's attributes self.title = "" self.author self.pages ". def get Information (self): #Method to display the value of each attribute info-"%s , by As has As pages." % (self.title, self·author, self.pages) return info def main(): book-1 = Book() #Main method requesting data from input book 1.title-raw input("What is the title of your book? ") book_ 1.author raw_input("Who is the author?") book_1.pages = raw..input ("How many pages are in your book? ") print book-1.getInformation() #Invoking method to display attributes 14 if -name-- "--main--": =: 17 main) 19 # def changeAttribute(): #method to change attributes #print ("Would you like to change an attribute? Answer True while Answer =wrcus ans input ("") =Explanation / Answer
Screenshot
--------------------------------------------------------------------------------------------
Code:-
#class declaration
class Book:
#costructor attribute initialization
def __init__(self,title="",author="",pages=""):
self.title =title
self.author =author
self.pages =pages
#get information function to display attribute values
def getInformation(self):
info="%s, by %s has %s pages ."%(self.title,self.author,self.pages)
return info
#main method
#def main():
#object of book class
b1=Book()
#prompt user for each attribute values
b1.title=input("What is the title of the book ? ")
b1.author=input("What is author's name of the book ? ")
b1.pages=input("How many pages in the book ? ")
#display information
print (b1.getInformation())
#ask user if they want to chane attribute
answer=input("Would you like to change attribute ? ")
if answer=="yes":
#change each attribute
val=input("which attribute do you want to change ? ")
if val=="title":
t=input("Change the title ? ")
setattr(b1,val,t)
print (b1.getInformation())
elif val=="author":
t=input("Change the author name ? ")
setattr(b1,val,t)
print (b1.getInformation())
elif val=="title":
t=input("Change the attribute ? ")
setattr(b1,val,t)
print (b1.getInformation())
#if __name__=="__main__":
# main()
-----------------------------------------------------------------------------------------------------------------------
Note:-
I am using visual studio python programming,so small changes in format
If you have any further change or doubt please let me know
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.