User avatar
sayhello
Posts: 89
Joined: Sat Mar 05, 2016 1:02 pm

need some help with classes in Python

Wed May 01, 2019 10:22 am

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?

User avatar
B.Goode
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: need some help with classes in Python

Wed May 01, 2019 11:58 am

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.

User avatar
sayhello
Posts: 89
Joined: Sat Mar 05, 2016 1:02 pm

Re: need some help with classes in Python

Wed May 01, 2019 12:09 pm

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

scotty101
Posts: 3753
Joined: Fri Jun 08, 2012 6:03 pm

Re: need some help with classes in Python

Wed May 01, 2019 12:25 pm

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.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Python”