+ 1
Beginner looking for Help with Python OOP
https://code.sololearn.com/cA19A18A18A7 here is my code Im looking for some guidance and help I am pretty comfortable with functions but having trouble trying to get all of the employee info into a dictionary so that it can be used in the program. Also Im not sure If I am creating the class correctly The employee info should be obtained by the add function but each employee should be added with a randomly generated 5 digit ID which will be the key in the dictionary to be used for each employee
14 Réponses
+ 2
Check this out, ive added a few methods and changed the way you reference the attributes in your get methods.
OOP is great because you can reference anything in a class, inside said class no matter where!
enter for example:
2
Sales
Manager
# then hit SUBMIT
https://code.sololearn.com/cujis4KlSsCA/?ref=app
+ 2
Slick https://imgur.com/gallery/WOICfTk this is the prompt i was trying to follow
+ 2
Thanks Slick ill try right now !
+ 2
Just looked it over and looks way better!
There's no making things private in Python, only the suggestion of privacy. Usually done with the double underscore. It's more to tell other programmers you shouldn't mess with that code.
class Dog:
def __init__(self, name):
self.name = name
self.__feelings = "like a dog..."
rex = Dog("Rex")
print(f"{rex.name} feels {rex.__feelings}")
# output
Rex feels like a dog...
+ 1
Slick thanks for taking your time to help. So i was wondering where should i create an an employee dictionary and make it to where it will store employee info so that i can can use the dic for all my functions?
+ 1
You already have an employee class that holds all the data. For a short program, you can make an empty list, and append the Employee Object instance to the list. You could also store all the info in a file, then read it back into the program when its ran.
+ 1
Slick oh i see yeah if it was a file it would be easier for me im used to usinf read and write functions but our professor gave us this prompt saying she wanted it all in one file with no txt files or imports from the outside besides random to set the id .. so lets pretend i run the program and add some employes right .. how do i access that data so that i can print those employees and search for them using their IDs and stuff.. im just a little confused still its our very first lesson in Classes lol so im a little bit lost
+ 1
okay no worries, the easiest way to do that to me would be to make an empty list outside of the class or any function.
Then use the same code AND add a line that appends the Employee to the list.
Say you have 3 employee objects in the list called employee_list:
search_id = int(input("Enter employee id to search: "))
for employee in employee_list:
if employee.emp_id == search_id:
employee.display_employee_info()
else:
print("No employee found.")
+ 1
Slick i updated my code i think im getting closer but i have to tweak a few things now to follow the prompt and clean it up i just need to figure out how to creat a display all function that will print all the employees separated by a new line
+ 1
think about it like this:
create a method that prints out the information of a single Employee class instance.
Then all you'd need to do is loop through a list (or dictionary) of the employees and call the method on each instance. mabey printing a new line after each one.
+ 1
Slick how about now is that better? All i need to do now is make my methods private i think
+ 1
Thats correct! But whatever prof said
0
Slick so basically when running in the IDE you can start by adding employees with the add function then you could search for the employees using their ids and update their attributes or print all of them ... it doesnt have to write the data anywhere just needs to store while the program is running so that it can be accessed .. once the program ends it would just restart basically thats why every function calls back to main until user selects to quit. If that makes sense