Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Unexpected Output using classes and inheritance
#1
I am a beginner at python and in the code below I get an error saying attribute was not defined or saying a list is not callable if I make a few changes. Please help me through this.
class User():
	"""Storing information about any user"""
	def __init__(self,first_name,last_name,age,location,gender):
		"""Holds the information"""
		self.first_name=first_name
		self.last_name=last_name
		self.age=age
		self.location=location 
	def describe_user(self):
		print(self.first_name.title() + " " + self.last_name.title() + " " +"aged " + str(self.age) +" " + "lives in " + self.location.title()) 
class Admin(User): 
	def __init__(self,first_name,last_name,age,location,gender):
		super(). __init__(first_name,last_name,age,location,gender)
		self.privileges=[]
		def privileges(self):
			self.privileges=[]
			self.privileges.append("can ban post")
			self.privileges.append("can revoke access")
			self.privileges.append("can ban user")
			print("The privileges of an admin are:")
			for privilege in self.privileges:
				print("\n\t" + privilege)
new_user=Admin('abc','bac', 48, 'vienna' , 'male')
new_user.describe_user()
new_user.privileges() 
Quote
#2
The priveleges function is defined inside the __init__ function. So, you are trying to invoke a list: new_user.privileges is a list (see line No. 14). I make some changes in your code, I suppose they help:

class User():
    """Storing information about any user"""

    def __init__(self, first_name, last_name, age, location, gender):
        """Holds the information"""

        self.first_name = first_name
        self.last_name = last_name
        self.age = age
        self.location = location 
    
    def __str__(self):
        return " ".join([self.first_name.title(), self.last_name.title(),
                         "aged", str(self.age), "lives in", self.location.title()])
        

class Admin(User): 
    def __init__(self,first_name, last_name, age, location, gender):
        super().__init__(first_name,last_name,age,location,gender)
        self.privileges = []

    def set_privileges(self):
        self.privileges = []
        self.privileges.append("can ban post")
        self.privileges.append("can revoke access")
        self.privileges.append("can ban user")
        print("The privileges of an admin are:")
        for privilege in self.privileges:
            print("\n\t" + privilege)

new_user = Admin('abc','bac', 48, 'vienna' , 'male')
print(new_user)
new_user.set_privileges()
new_user.privileges
Quote
#3
Oops. I noticed that. Indentation is key. Thanks a lot. Works perfectly now.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Unexpected output: if statement CabbageMan 1 73 Sep-04-2019, 04:12 PM
Last Post: ThomasL
  Problems with inheritance with classes internetguy 3 453 Jul-04-2019, 11:59 AM
Last Post: metulburr
  float multiplication - unexpected output inesk 3 448 Dec-11-2018, 10:59 AM
Last Post: DeaD_EyE
  Unexpected output when searching for a string from os.popen output FujiJean 3 633 Oct-02-2018, 11:39 AM
Last Post: volcano63
  Unexpected twice output Samo 3 509 Sep-29-2018, 10:00 PM
Last Post: Samo
  Unexpected simpy simulation output bakas 1 666 Sep-07-2018, 01:14 AM
Last Post: scidam
  unexpected output for global variable bhdebasish 1 494 Aug-22-2018, 04:55 PM
Last Post: Larz60+
  Inheritance Athul 7 701 Aug-11-2018, 06:48 PM
Last Post: yksingh1097
  A little unexpected output from a while loop linuxnoob 3 636 Jul-17-2018, 01:02 AM
Last Post: gontajones
  Unexpected output, TypeError and traceback error fier259 2 714 May-06-2018, 10:52 PM
Last Post: fier259

Forum Jump:


Users browsing this thread: 1 Guest(s)