Page 1 of 1

need some help with classes in Python

Posted: Wed May 01, 2019 10:22 am
by sayhello
hello dear Raspi-Experts

i am going to create a Phonebook class

efforts and parts: Design a Python class for a Phonebook. You class should have the following methods - see the little list that i have to implement:

init (self): creates an empty list to store names and numbers
Add(self, name, number): adds the name and number to the list
Display(self): displays the entire phonebook.
Find(self, name): this should find and also do the print of the number in the little phonebook that is associated with the name

last but not least i want to add more stuff: eg.
Add a main() method that ddoes more - eg. to the script that adds a couple of new phone entries and calls the display() and besides of this additionaly find() methods to see if they work.

so far so good: That's the assignment we have to do. to go further -. what was done this code and I'm not entirely sure where to go from there.

Code: Select all

class Phonebook:

def __init__(self):
    pass
def add(self,name, number):
    self.name=name
    self.numbers=number

def getName(self):
    return self.name
def getNumber(self):
    return self.number

def find(self, name):
    a = raw_input("What is their First Name?")
    return(self,name)
     
def display(self):
    return display
def main(): phone1 = Phonebook("foo bar", 13345444234214) phone2 = Phonebook("foo bar", 13345444234214)

print(display())
if name == 'main': main()

well what was done this code and I'm not entirely sure where to go from there.

one question: can someone give me some hints - I've tried looking in books and tried to find out how to use the add function and,
The question is: how to incorpoate find and display in the def main function. Thanks


i guess that

Code: Select all

__init__(self): creates an empty list to store names and numbers

def __init__(self):
    pass
That does not do what the instructions want.

Also:

Code: Select all

def getName(self):
    return self.name
def getNumber(self):
    return self.number



should i really use getters in python?

Re: need some help with classes in Python

Posted: Wed May 01, 2019 11:58 am
by B.Goode
That's the assignment we have to do


Credit to you for being honest about the reason for the question.

An assignment is usually set to check that the course material that has just been taught had actually been absorbed and learnt by the student.

So if you don't know how to proceed with answering this question I suggest you go back to the tutor or teacher and ask them to review the material with you. Maybe you are not the only person in the group who does not understand?




hello dear Raspi-Experts
There is nothing here that needs any Raspberry Pi expertise.

Re: need some help with classes in Python

Posted: Wed May 01, 2019 12:09 pm
by sayhello
good day

many many thanks for the quick reply. Thaniks for the tipps. I will do as adviced and do some more research.

Have a great day.

greetings
say

Re: need some help with classes in Python

Posted: Wed May 01, 2019 12:25 pm
by scotty101
Watch a few of Cory Schafer's videos on Youtube.

https://www.youtube.com/watch?v=ZDa-Z5JzLYM&vl=en

This first one explains how to create a class to store details about Employees. The rest of the series will help too.

At the moment you haven't created a PhoneBook class, you've created a class that only stores a single entry in a phone book.

Your Phonebook might contain a list where each item is a class instance that stores the name/telephone number of each person.