I got bored of revision last night, and decided to have another go at python!
So to refresh my memory and have a bit of a practice I wrote a quick phone-book program. I'd be grateful if y'all could give me some tips and ways to improve on it/make it more efficient :)

It only seems to run in idle though - when I run the actual python file - the menu doesn't work.

import os

class Phonebook:
    def __init__(self):
        self.phonebook = {}
        self.phonebook_file = 'Phonebook.txt'

    def loadAll(self):
        #Clear the phonebook dictionary
        self.phonebook.clear()
        
        #Load all of the items from the text file into the dictionary
        file = open(self.phonebook_file, 'r')
        for line in file.readlines():
            name, number = line.strip().split()
            self.phonebook[name] = number
        file.close()
        
    def addEntry(self):
        self.loadAll()

        #Prompt the user for the details of the new entry
        name = input("ENTER NAME: ")
        number = input("ENTER NUMBER: ")

        #Create a string to be written to the file
        new_entry = name + '\t' + number + '\n'

        #Write the string to the file
        file = open(self.phonebook_file, 'a')
        file.write(new_entry)
        file.close()
        
    def readAll(self):
        self.loadAll()

        #Print out the entire phonebook dictionary
        for name, number in self.phonebook.items():
            print(name, " : ", number)
        if len(self.phonebook) == 0:
            print("PHONEBOOK IS EMPTY")
            
    def searchEntry(self):
        self.loadAll()

        #Prompt the user for the name to search for, and search the phonebook dictionary 
        search = input("ENTER NAME TO SEARCH FOR: ")
        if search in self.phonebook.keys():
            print(search, " : ", self.phonebook[search])
        else:
            print("ENTRY NOT FOUND")
            
    def deleteEntry(self):
        self.loadAll()

        entry_to_delete = input("ENTER NAME OF ENTRY TO DELETE: ")
        if entry_to_delete in self.phonebook.keys():
            del self.phonebook[entry_to_delete]
            file = open(self.phonebook_file, 'w')
            for name, number in self.phonebook.items():
                string = name + '\t' + number + '\n'
                file.write(string)
            file.close()
            print("ENTRY DELETED SUCCESSFULLY")

        else:
            print("ENTRY NOT FOUND")

    def exitProgram(self):
        os._exit

    def menu(self):
        print("""\
       -MENU-
1) READ ALL ENTRIES
2) ADD AN ENTRY
3) DELETE AN ENTRY
4) LOOK UP AN ENTRY
5) Exit\n""")
        choice = input("ENTER CHOICE: ")
        choice_menu = {'1' : self.readAll,
                       '2' : self.addEntry,
                       '3' : self.deleteEntry,
                       '4' : self.searchEntry,
                       '5' : self.exitProgram}
        if choice not in choice_menu.keys():
            print("PLEASE ENTER A VALID CHOICE")
        else:
            choice_menu[choice]()
            
Book_1 = Phonebook()
Book_1.menu()

Recommended Answers

All 3 Replies

Here is your problem:

choice = input("ENTER CHOICE: ")

Changing 'input' to 'raw_input' will solve your problem.

You must probably try this to know the difference:

>>> input( "Enter something: " )
5
5
>>> input( "Enter something else: " )
britanicus

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'britanicus' is not defined
>>>

input expects a valid python expression. Hence back in your case, when the user enters a number from 1 to 5, input evaluates it to integer 1 which is not in choice_menu.keys()

Here is your problem:

choice = input("ENTER CHOICE: ")

Changing 'input' to 'raw_input' will solve your problem.

You must probably try this to know the difference:

>>> input( "Enter something: " )
5
5
>>> input( "Enter something else: " )
britanicus

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'britanicus' is not defined
>>>

input expects a valid python expression. Hence back in your case, when the user enters a number from 1 to 5, input evaluates it to integer 1 which is not in choice_menu.keys()

Your point is true only for Python version < 3.x Since OP doesn't tell us what version he uses, we can take a clue from print lines. His are in the 3.x style.

commented: good point +10

library management system projet source code in mysql. i hope i will get to your answer

d2a3e3e9aa25768e3511dd86d4d0cc3b.png

commented: Odd post there. Under a 9 year discussion where what may be a new question never to be found. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.