+ 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

16th Apr 2021, 9:29 PM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
14 odpowiedzi
+ 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
17th Apr 2021, 12:30 AM
Slick
Slick - avatar
+ 2
Slick https://imgur.com/gallery/WOICfTk this is the prompt i was trying to follow
17th Apr 2021, 1:08 AM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
+ 2
Thanks Slick ill try right now !
17th Apr 2021, 1:41 AM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
+ 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...
17th Apr 2021, 5:44 PM
Slick
Slick - avatar
+ 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?
17th Apr 2021, 12:47 AM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
+ 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.
17th Apr 2021, 12:55 AM
Slick
Slick - avatar
+ 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
17th Apr 2021, 1:01 AM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
+ 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.")
17th Apr 2021, 1:11 AM
Slick
Slick - avatar
+ 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
17th Apr 2021, 3:32 AM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
+ 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.
17th Apr 2021, 3:45 AM
Slick
Slick - avatar
+ 1
Slick how about now is that better? All i need to do now is make my methods private i think
17th Apr 2021, 5:30 PM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
+ 1
Slick ahh sweet so in python its more of an honor code and the double underscore just is saying hey this data shouldnt be tampered with/accessed ?
17th Apr 2021, 5:49 PM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
+ 1
Thats correct! But whatever prof said
17th Apr 2021, 5:55 PM
Slick
Slick - avatar
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
17th Apr 2021, 1:05 AM
Gutz_X_73vEn
Gutz_X_73vEn - avatar