I'm very new to OOP, and I'm trying to do it correctly!
I have been doing a simple friendbook where you can add, remove and show the friends you have. However, I dont know if I should be satisfied or not with my code.
class Friendbook:
listOfFriends = {}
def __init__(self, name, lastname):
self.name = name
self.lastname = lastname
def addFriend(self):
self.listOfFriends[self.name] = self.lastname
def showFriends(self):
for firstname, lastname in self.listOfFriends.items():
print(firstname, lastname)
def removeFriend(self):
removeName = input("Enter name to remove\n")
del self.listOfFriends[removeName]
while True:
print("1. Add Friend\n2. Showfriends\n3. RemoveFriend\n9. Quit\n")
try:
selectedOption = int(input())
if selectedOption == 1:
name = input("Enter firstname\n") #Can i move this to a function?
lastname = input("Enter lastname\n") #Can i move this to a function?
makeFriends = Friendbook(name, lastname)
makeFriends.addFriend()
elif selectedOption == 2:
makeFriends.showFriends()
elif selectedOption == 3:
makeFriends.removeFriend()
elif selectedOption == 9:
break
else:
print("Not a valid option")
except:
print("Only integers allowed")
I'm happy with it except for the code related to selectedoption
1, can I do something to put it inside the function so it looks nicer?
What do you think about my code in general, appreciate feedback!