Simple Python Class Not Displaying/Printing Anything - python

I am trying to create this class that would display an Employee's name and his yearly salary. Everything seems to be working and I haven't encountered any errors, but when I run the program, nothing is printed.
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def Func(self):
print("Employee name is" + self.name)
employee1 = Employee("Adam Smith", "$47,000")
print(employee1.salary)
What am I doing wrong?

It is a indentation error, Use :
class Employee:
# Error is here
def __init__(self, name, salary):
self.name = name
self.salary = salary
def Func(self):
print("Employee name is" + self.name)
employee1 = Employee("Adam Smith", "$47,000")
print(employee1.salary)

Related

I'm doing some Python exercises and can't seem to get around this error

class Student:
def get_name(self):
"""Return the name of current Student object."""
return self.name
def get_age(self):
"""Return the age of the current Student object."""
return self.age
def say_hello(self, message=None) -> None:
if message is not None:
print(f"{self.name} says: {message}")
else:
print(f"Hello, my name is {self.name}. Nice to meet you!")
if __name__ == '__main__':
student = Student(name='Wes', age=10)
name = student.get_name()
age = student.get_age()
student.say_hello()
message = f"The student's name is {name}. Their age is {age}."
student.say_hello(message)
I get this error:
File “student.py”, line 21, in <module> student = Student(name=‘Wes’, age=10) TypeError: Student() takes no arguments
when this is supposed to be the result:
Hello, my name is Wes. Nice to meet you!
Wes says: The student's name is Wes. Their age is 10.
You have defined Student class without a def __init__ method required if you want to send arguments when initiating a new class instance. Add:
def __init__(self, name, age):
self.name = name
self.age = age
as a method at the top of your class definition.

Error message: "numpy.ndarray' object has no attribute 'set_volume" Python [duplicate]

I am new to python and while learning OOP in python i am getting errors like
AttributeError: 'Dog' object has no attribute 'sound'
for below code
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def description(self):
return print(f"name is {self.name} and age is {self.age}")
def speak(self, sound):
return print(f"{self.name} says {self.sound}")
tommy = Dog("tommy",10)
tommy.description()
tommy.speak("bow-bow")
Now my other doubt is related to inheritance where i am getting error like:
AttributeError: 'Bulldog' object has no attribute 'speed'
for below code :
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def description(self):
return print(f"name is {self.name} and age is {self.age}")
class Bulldog(Dog):
def run(self, speed):
return print(f"The speed of dog is {self.speed}")
tommy = Bulldog("tommy",10)
tommy.description()
tommy.run(5)
I believe you need to remove the self. when trying to return the print as these are passed through as parameters and not identified in the object itself.
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def description(self):
return print("name is {self.age} and age is {self.age})
class Bulldog(Dog):
def run(self, speed):
return print(f"The speed of dog is {speed}")
tommy = Bulldog("tommy",10)
tommy.description()
tommy.run(5)
This is the same for both speed and sound, note I also changed some formatting about how the print statement works

How to get the value of an instance stored in a list

I want to know how to get all the values ​​of student0 instance without using the print(student0.name, student0. age) method when I have the code below.
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
student0 = Student("Lee", "22")
student1 = Student("Kim", "23")
You can use the following code :
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def __repr__(self):
return "% s % s" % (self.name, self.age)
student0 = Student("Lee", "22")
student1 = Student("Kim", "23")
print(student0)

Python 3 OOP - where does "super().__init__()" go exactly?

I am trying to make my sub-class work, but something is wrong with the inheritance. The number of args passed in should be 5: name, age, gender, title & salary
However, Python is saying
TypeError: __init__() takes 4 positional arguments but 6 were given
and I don't know why or how to fix it. Here is my code:
class Person:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
class Employee(Person):
def emp_function(self, title, salary):
self.title = title
self.salary = salary
super().__init__()
#Is this wrong? Where should this 'super()' go?
George = Employee("George", 30, "male", "Manager", 50000)
super().__init__() should be in def __init__ of Employee class
You need to create constructor for Employee class too.
class Person:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
class Employee(Person):
def __init__(self,name,age,gender,title,salary):
self.title = title
self.salary = salary
super().__init__(name, age, gender)
George = Employee("George", 30, "male", "Manager", 50000)

TypeError: this constructor takes no arguments in python

How do i print e1 and e2 values. it does not return any values.
class Employee:
def __init__(self,name,age):
self.name=name
self.age=age
e1=Employee("xyz",'25')
e2=Employee("abc",'23')
print("Employee Details...")
print("Name:",e1.name,"age:",e1.age)
print("Name:",e2.name,"age:",e2.age)
return
t=Employee()
print t
What you are trying to do seems like reinventing the __str__ method. Here is a suggestion how you could do that:
class Employee:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return "Name: {0}, age: {1}".format(self.name, self.age)
t = Employee("uday", 25)
print t
I am making here some assumptions and am trying to guess your actual goal.
I hope that it still helps.
You should redefine __init__ method. That's because __init__ is called while object initialization.
class Employee:
def __init__(self, name,age):
self.name = name
self.age=age
e1=Employee("xyz",'25')
e2=Employee("abc",'23')
print("Employee Details...")
print("Name:",e1.name,"age:",e1.age)
print("Name:",e2.name,"age:",e2.age)
Edit:
You can use this code to achieve your desired output with a little update.
You can add commas , after print to allow next print statement start at the same line , change the code like that:
class Employee:
def __init__(self, name,age):
self.name = name
self.age=age
e1=Employee("xyz",'25')
e2=Employee("abc",'23')
print("Employee Details:"),
print("Name:",e1.name,"age:",e1.age),
print("Name:",e2.name,"age:",e2.age)
Output:
Employee Details: (' Name:', 'xyz', 'age:'25') (' Name:', 'abc', 'age:'23')

Categories

Resources