create a list from class user input - python

Create an employee class with the following members: name, age, id, salary
setData() - should allow employee data to be set via user input
getData()- should output employee data to the console
create a list of 5 employees. You can create a list of objects in the following way, appending the objects to the lists.
emp_object = []
for i in range(5):
emp_ object.append(ClassName())
I'm trying to do this exercise and this is what I got:
class employee:
def __init__(self, n = None, a = None, i = None, s = None):
self.name = n
self.age = a
self.id = i
self.salary = s
def setData(self):
self.n = input("Enter name: ")
self.a = int(input("Enter age: "))
self.i = int(input("Enter id: "))
self.s = int(input("Enter salary: "))
self.getData()
def getData(self):
print("Name:", self.name, self.age, self.id, self.salary)
e1 = employee()
e1.setData()
e2 = employee()
e2.setData()
e3 = employee()
e3.setData()
e4 = employee()
e4.setData()
e5 = employee()
e5.setData()
emp_object = []
for i in range(5):
emp_object.append(employee())
print(emp_object)
It prints the employee details as "None" and I need help to create a list
Expected Output:
Name id Age Salary
AAA 20 1 2000
BBB 22 2 2500
CCC 20 3 1500
DDD 22 4 3500
EEE 22 5 4000

Change the instance variable self.n ( in the setData method) to self.name to match the declaration your class init method ...and do the same for the self.a, self.i... variables .

I beleive the problem is that you are not setting the parameters to the ones you want in the setData function.
You need to do this:
class employee:
def __init__(self, n = None, a = None, i = None, s = None):
self.name = n
self.age = a
self.id = i
self.salary = s
def setData(self):
self.name = input("Enter name: ")
self.age = int(input("Enter age: "))
self.id = int(input("Enter id: "))
self.salary = int(input("Enter salary: "))
self.getData()
def getData(self):
print("Name:", self.name, self.age, self.id, self.salary)
The __init__ and setData are two separate functions.

First you want to separate some responsabilities for a better reading.
We will divide the problem in two parts :
Employee model
Input/output problem
Employee
Create a class who contains only employee data (we can use dataclasses but, I assume you're a beginner, so I'll keep simple)
class Employee:
def __init__(self, uid=None, name=None, age=None, salary=None):
self.name = name
self.age = age
self.id = uid
self.salary = salary
Output and Input
To display the employee's data in console, we can use __str__ function. It is used when you class need to be converted into a str (in print for isntance).
We then add an other method in charge to set employee's data.
Our Employee class become :
class Employee:
def __init__(self, uid=None, name=None, age=None, salary=None):
self.name = name
self.age = age
self.id = uid
self.salary = salary
def __str__(self):
return f"Name: {self.name}, {self.age}, {self.id}, {self.salary}"
def set_data(self):
self.name = input("Enter name: ")
self.age = int(input("Enter age: "))
self.id = int(input("Enter id: "))
self.salary = int(input("Enter salary: "))
Our class is complete. Now we will write the algorithm in charge to create 5 employees.
So under the Employee class :
if __name__ == '__main__':
# Empty list containing our employees
employees = []
# We loop 5 times.
for i in range(5):
# We create an employee
employee = Employee()
# We set the data
employee.set_data()
# We append our brand-new employee into the list
employees.append(employee)
# Now we display our data :
for employee in employees:
# We just need to print the object thanks to __str__ method
print(employee)
Tell me if I answered correctly to your problem !

Related

AttributeError Python OOP

from typing import List
class Customer:
def __init__(self, name:str,age:int, phone_number: List['PhoneNumber']):
self.name = name
self.age = age
self.phone_number = phone_number
def check_ownership(self):
for number in self.phone_number:
if number.owner == self.name:
print(f"Number: {number} is owned by {self.name}")
else:
print(f"Number: {number} is not owned by {self.name}")
class PhoneNumber:
def __init__(self,credit: int,phone_number: int, owner:Customer):
self.credit = credit
self.phone_number = phone_number
self.owner: Customer = owner
def add_credit(self, amount):
self.credit += amount
customer1 = Customer("Jake",20,[4321,5374])
phonenum1 = PhoneNumber(12,4321,"Jake")
phonenum2 = PhoneNumber(12,5374,"John")
print(customer1.check_ownership())
Im trying to check if the customer "Jake" is really set as a owner for the phone numbers he owns but i keep getting AttributeError: 'int' object has no attribute 'owner'. What is the right way to check if the customer really owns those numbers.

print and sort a list of class in oop python

I have a class name Student like this:
class Student:
def __init__(self, name = '', age = 0, test_score = 0):
self.name = name
self.age = age
self.test_Scores = test_score
def __str__(self):
return "({0},{1},{2})".format(self.name,self.age, self.test_Scores)
and class name Students:
class Students():
stds = list()
def __init__(self) -> None:
pass
def Input(self):
while True:
inputName = input('Enter name: ')
inputAge = int(input('Enter age: '))
inputTestScore = int(input('Enter test score: '))
std = Student(inputName, inputAge, inputTestScore)
if inputAge == 0:
break
self.stds.append(std)
def __str__(self):
return str(self.stds)
Here are some code print out a list of students:
stds = Students()
stds.Input()
print(stds)
With 2 elements in the list, the result after excute look like this:
[<main.Student object at 0x0000026EDEBC5FA0>, <main.Student object at 0x0000026EDEBC5CD0>]
I can't print out stds under a string, how can i fix it. And how to sort stds by the decreasing of age ?
Pls help me !
You shouldn't have class Students, you should have a list of Student. To get the data from the class variables override the __repr__ method. To sort the list you can use lambda with the attribute to sort by as key in sort() function
class Student:
def __init__(self, name='', age=0, test_score=0):
self.name = name
self.age = age
self.test_Scores = test_score
def __repr__(self):
return "({0},{1},{2})".format(self.name, self.age, self.test_Scores)
if __name__ == '__main__':
stds = list()
while True:
inputName = input('Enter name: ')
inputAge = int(input('Enter age: '))
inputTestScore = int(input('Enter test score: '))
std = Student(inputName, inputAge, inputTestScore)
if inputAge == 0:
break
stds.append(std)
stds.sort(key=lambda s: s.name)
print(stds)
You need to write the function str for students class.
def __str__(self):
return '\n'.join(self.stds)
try this =>
class Student:
def __init__(self, name = '', age = 0, test_score = 0):
self.name = name
self.age = age
self.test_Scores = test_score
def __str__(self):
return "({0},{1},{2})".format(self.name,self.age, self.test_Scores)
def __repr__(self):
return "({0},{1},{2})".format(self.name,self.age, self.test_Scores)
def __lt__(self, other):
return self.age < other.age
class Students():
stds = list()
def __init__(self) -> None:
pass
def Input(self):
while True:
inputName = input('Enter name: ')
inputAge = int(input('Enter age: '))
inputTestScore = int(input('Enter test score: '))
std = Student(inputName, inputAge, inputTestScore)
if inputAge == 0:
break
self.stds.append(std)
def __str__(self):
return str(self.stds)
stds = Students()
stds.Input()
print(stds)
stds.stds.sort(reverse=True)
print(stds)
Define a natural order for the students based on their age using #functools.total_ordering:
import functools
#functools.total_ordering
class Student:
def __init__(self, name='', age=0, test_score=0):
self.name = name
self.age = age
self.test_Scores = test_score
def __lt__(self, other):
return self.age < other.age
def __str__(self):
return "({0},{1},{2})".format(self.name, self.age, self.test_Scores)
def test_sorting_students():
alice: Student = Student("Alice", 21, 86)
bob: Student = Student("Bob", 19, 75)
caroline: Student = Student("Caroline", 20, 93)
students = [alice, bob, caroline]
students.sort()
assert students[0] == bob
assert students[1] == caroline
assert students[2] == alice
Then just sort them:
print(stds.sort())

How can I print each item in an unknown length list that is a class attribute as a string in python?

I have a class (Student) with different attributes, such as studentId, address, and courses. My str method for the class returns all the information that the user put in. However, for the attributes that are lists, such as courses, the location of the information is printed out instead of the actual information. Here is the code (sorry it's a little long, there's a bunch of classes):
class Person:
__name = None
__age = None
__address = None
def __init__(self, name, age=0, address=None):
self.set_name(name)
self.set_age(age)
self.set_address(address)
def __str__(self):
return 'Name: ' + self.__name + '\n' + \
'Age: ' + str(self.__age) + '\n' + \
'Address: ' + str(self.__address)
def set_name(self, name):
self.__name = name
def get_name(self):
return self.__name
def set_age(self, age):
self.__age = age
def get_age(self):
return self.__age
def set_address(self, address):
self.__address = address
def get_address(self):
return self.__address
class Student(Person):
def __init__(self, name, studentID= None, age= 0, address= None):
super(Student, self).__init__(name, age, address)
self.set_studentID(studentID)
self.__courses =[]
def __str__(self):
result = Person.__str__(self)
result += '\nStudent ID:' + self.get_studentID()
for item in self.__courses:
result += '\n ' + str(item)
return result
def set_studentID(self, studentID):
if isinstance(studentID, str) and len(studentID.strip()) > 0:
self.__studentID = studentID.strip()
else:
self.__studentID = 'NA'
def get_studentID(self):
return self.__studentID
def add_course(self, course):
print('in add_course')
self.__courses.append(course)
def get_courses(self):
for i in range(len(self.__courses)):
return self.__courses[i]
class Course:
__courseName = None
__dept = None
__credits = None
def __init__(self, courseName, dept= 'GE', credits= None):
self.set_courseName(courseName)
self.set_dept(dept)
self.set_credits(credits)
def __str__(self):
return self.get_courseName() + '/' + self.get_dept() + '/' + str(self.get_credits())
def set_courseName(self, courseName):
if isinstance(courseName, str) and len(courseName.strip()) > 0:
self.__courseName = courseName.strip()
else:
print('ERROR: Name must be a non-empty string')
raise TypeError('Name must be a non-empty string')
def get_courseName(self):
return self.__courseName
def set_dept(self, dept):
if isinstance(dept, str) and len(dept.strip()) > 0:
self.__dept = dept.strip()
else:
self.__dept = "GE"
def get_dept(self):
return self.__dept
def set_credits(self, credits):
if isinstance(credits, int) and credits > 0:
self.__credits = credits
else:
self.__credits = 3
def get_credits(self):
return self.__credits
students = []
def recordStudentEntry():
name = input('What is your name? ')
age = input('How old are you? ')
studentID= input('What is your student ID? ')
address = input('What is your address? ')
s1 = Student(name, studentID, int(age), address)
students.append(s1)
s1.add_course(recordCourseEntry())
print('\ndisplaying students...')
displayStudents()
print()
def recordCourseEntry():
courses = []
for i in range(2):
courseName = input('What is the name of one course you are taking? ')
dept = input('What department is your course in? ')
credits = input('How many credits is this course? ')
c1 = Course(courseName, dept, credits)
print(c1)
courses.append(c1)
displayCourses(courses)
return courses
def displayCourses(courses):
print('\ndisplaying courses of student... ')
for c in range(len(courses)):
print(courses[c])
def displayStudents():
for s in range(len(students)):
print()
print(students[s])
recordStudentEntry()
This is how the code above prints out the 'displaying students...' part:
displaying students...
Name: sam
Age: 33
Address: 123 st
Student ID:123abc
[<__main__.Course object at 0x000002BE36E0F7F0>, <__main__.Course object at
0x000002BE36E0F040>]
I know that it is printing out the location because I need to index into the list. However, the length of the list will be different every time. Normally if I wanted to index into a list, for example, to print a list of names, I would do:
listOfNames = ['sam', 'john', 'sara']
for i in range(len(listOfNames)):
print(listOfNames[i])
or
listOfNames = ['sam', 'john', 'sara']
for i in listOfNames:
print(i)
(not sure what if any difference there is between the 2 ways since they both print out the same way:)
sam
john
sara
How can I write something like the indexing into a list technique shown here in my str method for my class so that it prints the information and not the location?
It would be good to keep to the standard conventions for Python, such as naming
private attributes for objects with single underscores, not double underscores.
The latter are reserved for Python "internal" attributes and methods.
Also, it is convention to use object attributes for objects with get/set methods,
not class attributes. This will make it easier to inspect your objects, while
still maintaining data hiding. Example:
class Course:
def __init__(self, courseName, dept= 'GE', credits= None):
self._courseName = None
self._dept = None
self._credits = None
self.set_courseName(courseName)
...
Your question about why the courses don't print out the way you expected
is rooted in a programming error with the way you programmed the recording
of courses. In recordCourseEntry(), you record two courses and put them
in a list. However, you pass that to your Student object using a method
intended for one course at a time. My suggested fix would be:
...
# s1.add_course(recordCourseEntry())
courses = recordCourseEntry()
for course in courses:
s1.add_course(course)
...
This will probably be enough to get you going. An example output I got was:
Name: Virtual Scooter
Age: 33
Address: 101 University St.
Student ID:2021
ff/GE/3
gg/GE/3

how to take the average of grades of a student class in student.py

I have a python class called student that includes variables and methods.
Each student has a name, age, and a list of grades.
I have a method AVGofGrades() that takes the list of grades and returns the average.
I do not know how to create a function that will take this list of grades from the student object and loop through it to return the avgerage.
So far I have this:
class student():
def __init__(self,name,age, *grades):
self.stdName=name
self.stdAge = age
self.stdGrade = grades
def getName(self):
return self.stdName
def setName(self,Name):
self.stdName = Name
def getAge(self):
return self.stdAge
def setAge(self, Age):
self.stdAge = Age
def getGrade(self):
gradeLevel = None
if AVGofGrades(self.stdGrade) >=20:
gradeLevel = "A"
elif self.stdGrade >=18:
gradeLevel = "B"
elif self.stdGrade >=15:
gradeLevel = "C"
else:
gradeLevel = "F"
print("the student {0}, have a grade {1}".format(self.stdName, gradeLevel))
def setGrade(self,*Grades):
self.stdGrade = Grades
def AVGofGrades(self, *student):
result = 0
for i in student:
student[2]
std1 = student("georges", 17, 80,23,50,34,80,78)
You have grades already in self.stdGrade
So, you don't need to pass anything in the method AVGofGrades, as you are providing all the arguments needed when you create the Student Object.
Your AVGofGrades can be as below:
def AVGofGrades(self):
return sum(self.stdGrade)/len(self.stdGrade)
You can now create an instance of the class and get the AVGofGrades as below:
std1 = Student("georges", 17, 80,23,50,34,80,78)
print(std1.AVGofGrades())
It's a simple math expression:
def AVGofGrades(self, *student):
return sum(student) / len(student)
You should call it as self.AVGofGrades() though, not just AVGofGrades().

How to use for loop in class for user inputs?

I have created employee details using class by giving pre defined inputs. I'm not able to store the results into a dict. I need to write it as a csv. I would be thankful if you could help me, as I'm a novice in python
Here are my codes:
Is it correct way to use for loop in classes?
class Employee():
def main(self,name,idno,position,salary):
self.name=name
self.idno=idno
self.position=position
self.salary = salary
def input(self):
n=int(raw_input("Enter the number of employees:"))
for i in range(n):
self.name=raw_input("Name:")
self.idno=raw_input("Idno:")
self.position=raw_input("position:")
self.salary=raw_input("salary:")
print("Name:", self.name, "Idno:", self.idno, "position:", self.position,
"salary:", self.salary)
if __name__=='__main__':
result=Employee()
result.input()
First of all, I don't think you're class will be working like you intend it to. Since you're constantly overwriting the class variables, there is no point in entering more than one employee, as the class can currently only save information on one employee. I would consider saveing employees as dictionarys and have those dictionaries saved as a list in your Employee(s) class.
class Employees():
all_employees = [{...}, {...}]
dict_keys = ["name", "idno", "position",...]
def input(self):
counter = 0
n = input("Number of employees: ")
while counter < n:
new_employee = dict()
for key in dict_keys:
new_employee[key] = raw_input("{}: ".format(key))
all_employees.append(new_employee)
if __name__ == "__main__":
e = Employees()
e.input()
This illustrates what I was saying in my comment about using a for loop outside of the class:
class Employee(object):
def __init__(self, name, idno, position, salary):
self.name=name
self.idno=idno
self.position=position
self.salary = salary
def print_data(self):
print("Name:", self.name, "Idno:", self.idno, "position:", self.position,
"salary:", self.salary)
if __name__=='__main__':
def input_employee_data():
print('Enter data for an employee')
name = raw_input("Name:")
idno = raw_input("Idno:")
position = raw_input("position:")
salary = raw_input("salary:")
print('')
return name, idno, position, salary
employees = list()
n = int(raw_input("Enter the number of employees:"))
for i in range(n):
name, idno, position, salary = input_employee_data()
employee = Employee(name, idno, position, salary)
employees.append(employee)
print('List of employess')
for employee in employees:
employee.print_data()

Categories

Resources