I have the following error when I run this program: NameError: name create is not defined. I`m trying to creeate a library program to add/remove/see all the books and everything will be stored in a .dat file
I defined the function, but I'm not sure why it's not working. I copied the code below:
import pickle
import os
class Library:
def __init__(self,book ,title,author,pubdate,status,retdate,location):
self.book = book
self.title = title
self.author = author
self.pubdate = pubdate
self.status = status
self.retdate = retdate
self.location = location
def details(self):
self.details = " Title: " + self.title + " Author: " + self.author + " Publication date: " + str(self.pubdate) + " Status" + self.status + " Return date : " + str(self.retdate) + "location" + str(self.location)
print(self.details)
def create(self):
harvard_lib = Library(self.title, self.author, self.pubdate, self.status, self.retdate, self.location)
self.location = 1
while index !="Q" :
book = str(input("Book:"))
title = str(input("Enter the title of the book: "))
author = str(input("Enter the author of the book: "))
pubdate = int(input("Enter the publication date of the book: "))
status = str(input("Enter the status of the book(A / N): ")) # a-available, n- not
retdate = int(input("Enter the return date of the book: "))
index = str(input("Press any key to continue,or pres Q to quit"))
location +=1
booklist = 'booklist.dat'
newitem = []
if os.path.exists(booklist):
with open(booklist,'rb') as rfp:
newitem = pickle.load(rfp)
newitem1 = book,title,author,pubdate,status,retdate,index,location
newitem.append(newitem1)
with open(booklist,'wb') as wfp:
pickle.dump(newitem, wfp)
with open(booklist,'rb') as rfp:
newitem = pickle.load(rfp)
print(newitem)
harvard_lib.create()
create() is a method of Library class, not a function.
If you want to access a method of a class, you need an instance of that class.
Try something like that:
harvard_lib = Library("bookname", "title", ...)
harvard_lib.create()
You need to create an instance of the class first before you can make use of that method. It is a method of a class so you should not treat it as a normal function.
hash_lib = Library("Lord of the Rings","J.R Tolkien",...) #creating a class instance
hash_lib.create() #making use of the class method
with this you should be good to go
Related
I have been tasked with creating an inventory manager in python, and one of the required is to define the following function within the class:
__str__ - This function returns a string representation of a
class.
The class refers to a file (inventory.txt) which has the following format:
Country,Code,Product,Cost,Quantity
my code thus far is as follows:
# Creating the class:
class Shoes():
# Constructor:
def __init__(self,country,code,product,cost,quantity):
self.country = country
self.code = code
self.product = product
self.cost = cost
self.quantity = quantity
# Methods:
def get_cost():
inventory = open('inventory.txt','r')
inventory_list = inventory.readlines()
code = input("What is the code of the product:")
for line in inventory_list:
split_lines = line.split(",")
if code == split_lines[1]:
print("This product costs R{}".format(split_lines[3]))
inventory.close()
def get_quantity():
inventory = open('inventory.txt','r')
inventory_list = inventory.readlines()
code = input("What is the code of the product:")
for line in inventory_list:
split_lines = line.split(",")
if code == split_lines[1]:
print("There are {} units in inventory".format(split_lines[4]))
inventory.close()
def __str__(self):
pass
I haven't come across the str so I really am not sure how it works, and how to use it. Any advice would be greatly appreciated.
Here is an example:
def __str__(self):
return f'{self.self.country},{self.self.code},{self.self.product}, {self.cost},{self.quantity })'
This way, when you assign values to a class, you can print its string representation
print(new_shoe)
More info here
https://www.pythontutorial.net/python-oop/python-str/
I don't really know what I have done wrong here. I get the error "student has no attribute name" when it gets to the output data function. Any ideas are greatly appreciated.
class Person:
def __init__(self):
self.ID=""
self.name=""
self.address=""
self.Phone_number=""
self.email_id=""
self.student=self.Student()
def read_data(person):
person.ID=input("please enter ID:")
person.name=input("Please enter name:")
person.address=input("Enter address:")
person.Phone_number=input("Enter Phone Number:")
class Student:
def __init__(self):
self.class_status=""
self.major=""
def read_data(student):
student.class_status=input("Enter class status:")
student.major=input("Enter student major:")
def output_data(student):
information=(student.name + " " + student.ID + " " + student.address + " " + student.Phone_number + " " + student.class_status + " " + student.major + "\n")
print(information)
studentFile.write(information)
def StudentDetails():
person=Person()
person.read_data()
student=person.student
student.read_data()
student.output_data()
studentDetails()
The attributes of an outer class aren't passed to an inner class. It looks like you're trying to model an inheritance relationship, which you can do by using subclassing rather than nesting classes. For example, you could do something like the following:
class Person:
def __init__(self):
self.ID=""
self.name=""
self.address=""
self.Phone_number=""
self.email_id=""
def read_data(person):
person.ID=input("please enter ID:")
person.name=input("Please enter name:")
person.address=input("Enter address:")
person.Phone_number=input("Enter Phone Number:")
class Student(Person):
def __init__(self):
super().__init__()
self.class_status=""
self.major=""
def read_data(self):
super().read_data()
self.class_status=input("Enter class status:")
self.major=input("Enter student major:")
def output_data(self):
information=(self.name + " " + self.ID + " " + \
self.address + " " + self.Phone_number + " " + \
self.class_status + " " + self.major + "\n")
print(information)
def studentDetails():
student = Student()
student.read_data()
student.output_data()
studentDetails()
If you are absolutely sure that you must use a nested class, then the relationship you're trying to describe doesn't make sense. I could see something like a student ID class being an inner class of Person to store some additional attributes, but I don't think the relationship as currently described makes much sense.
I had an assignment to create a python code using class to create a group with 3 members (aggregation relationship). This is my code so far:
class Member:
def __init__(self,name,age):
self.name = name
self.age = age
def getInfo(self):
memberInfo = "Name: " + str(self.name) + "." + "Age: " + str(self.age)
return memberInfo
class Group:
def __init__(self,name):
self.name = name
self.memlist = []
def addMember(self,member):
self.memlist.append(member)
def getInfo(self):
info = "Member List: \n"
for i in range(len(self.memlist)):
info += self.memlist[i].getInfo() + "\n"
print(info)
break
mem1 = Member("Chi",20)
mem2 = Member("Bach",7)
mem3 = Member("Gen", 22)
group1 = Group("Siblings")
group1.addMember(mem1)
group1.addMember(mem2)
print(group1.getInfo())
print(mem2.getInfo())
print(group1.memList)
But it has shown an error: AttributeError: 'Group' object has no attribute 'memList'. Is there anything I can do to fix this?
I wrote little function for listing members and their ages.
class member:
def __init__(self, name, age):
self.name = name
self.age = age
def member_Info(self):
memberInfo = f"Name: {str(self.name)}-->Age: {str(self.age)}"
return memberInfo
class Group:
def __init__(self, name):
self.name = name
self.memlist = []
def addMember(self, name):
self.memlist.append(name)
def getInfo(self):
for i in range(len(self.memlist)):
info = self.memlist[i].member_Info() + "\n"
print(info)
This all_members function is basically getting the information stored in the member class and return to list. I print using memlist in Group but it didn't work out so I made a new list using all_member function and get information from memlist in group1 with the code that you used for getting information in memlist at group1.getInfo .
def all_members():
all_mems = []
for i in range(len(group1.memlist)):
all_mems.append(group1.memlist[i].member_Info())
print(all_mems)
mem1 = member("Chi", "20")
mem2 = member("Bach", "7")
mem3 = member("Gen", "22")
group1 = Group("Siblings")
group1.addMember(mem1)
group1.addMember(mem2)
group1.addMember(mem3)
print(group1.getInfo())
print(mem2.member_Info() + "\n")
print(all_members())
I guess this isn't the best answer you can get but I think it will work and also I learn many things while trying to correct it so thank you for posting that.
change
print(group1.memList)
to
print(group1.memlist)
I am currently trying make a python script which opens a URL in web browser and now I am facing a error that NameError: name 'song' is not defined
import webbrowser
class Halsey:
def__init__(self):
self.song = song
def Badland():
print(" 1.Gasloine"
"2.castle"
"3.hold me down "
"4.control")
song =int(input("Plase select a number from above list"))
if song == 1 :
url="https://www.youtube.com/watch?v=jU3P7qz3ZrM";
webbrowser.open(url,new=0)
Well, your song variable visible only in method Badland of your class. So you can use this variable in the same method where you defined it, like this:
import webbrowser
class Halsey:
SONGS = (
("Gasloine", "https://www.youtube.com/watch?v=jU3P7qz3ZrM"),
("Castle", "url2"),
("Hold me down ", "url3"),
("Control", "url4"),
)
def __init__(self):
for i, song_name in enumerate(self.SONGS, 1):
print("{}. {}".format(i, song_name[0]))
song = int(input("Plase select a number from above list: "))
url = self.SONGS[song - 1][1]
webbrowser.open(url, new=0)
... or you can return user's input from class method and then use it, like this:
class Halsey:
SONGS = (
("Gasloine", "https://www.youtube.com/watch?v=jU3P7qz3ZrM"),
("Castle", "url2"),
("Hold me down ", "url3"),
("Control", "url4"),
)
def get_user_input():
for i, song_name in enumerate(self.SONGS, 1):
print("{}. {}".format(i, song_name[0]))
return int(input("Please select a number from above list: "))
instance = Halsey()
users_choice_int = instance.get_user_input()
url = self.SONGS[song - 1][1]
webbrowser.open(url, new=0)
Also, you can save your variable in class attribute: self.song = int(input(...) and after that you will be able to access it inside class methods by self.song, or outside your class by instance.song.
Note: don't forget about potential invalid user input, you can wrap your song = int(input... in try/except.
Good luck!
I'm working with Inheritance in python but i'm getting an error i don't know how to fix, 'finalStore' object has no attribute 'marone'. I get this when i try create an object.
from ClassFile import studStore
class finalStore (studStore):
grandAve = 0
numStu = 0
def __init__(self, name, marone, martwo, marthree, marfour, corone, cortwo, corthree, corfour):
studStore.__init__(self, name, marone, martwo, marthree, marfour)
self.corone = corone
self.cortwo = cortwo
self.corthree = corthree
self.corfour = corfour
finalStore.numStu += 1
self.holder = finalStore.numStu
self.average = (marone + martwo + marthree + marfour)/4
finalStore.grandAve += self.average
self.storit = finalStore.grandAve
My initializing for the child class
class studStore:
def __init__(self, name, marone, martwo, marthree, marfour):
self.newname = name
self.Ave = 0
self.marone = marone
self.martwo = martwo
self.marthree = marthree
self.marfour = marfour
And the initializing for the parent class. My main line is just a loop where i create multiple objects for but it errors on this line:
listIn.append(finalStore(name, gradeone, gradetwo, gradethree, gradefour, courseOne, courseTwo, courseThree, courseFour))
I'm not sure what the error is but I have a similar program that works, I'm just not using the from * import *
I'm outputting it like this
for i in range (0,len(listIn)):
print(str(listIn[i].returnName()).ljust(20," "), end = " ")
print(str(listIn[i].returnOne()).ljust(20, " "))
print(str(listIn[i].returnTwo()).ljust(20, " "))
print(str(listIn[i].returnThree()).ljust(20, " "))
print(str(listIn[i].returnFour()).ljust(20, " "))
Your call to the super class's init function is incorrect. Here is how you should do it:
class finalStore(studStore):
def __init__(self, name, ...):
super(finalStore, self).__init__(name, marone, ...)