Найти - Пользователи
Полная версия: Адресная книга на Python
Начало » Центр помощи » Адресная книга на Python
1 2
AL-ME
 Phone_Book = {}
class PhoneBook():
    """Creation Phone book"""
    def __init__(self, name, phone):
        """Inintiation"""
        self.name = name
        self.phone = phone
    def app(self):
        """append to book"""
        file = open('Phone_Book.org', 'rb')
        Phone_Book = pickle.load(file)
        file.close()
        Phone_Book[self.name] = self.phone
        file = open('Phone_Book.org', 'wb')
        pickle.dump(Phone_Book, file)
        file.close()
    def read_book(self):
        """Book reading"""
        file_1 = open('Phone_Book.org', 'rb')
        total = pickle.load(file_1)
        print(total)
        file_1.close()
    def change(self):
        """Record change"""
        name = input('Enter name to change phone: ')
        phones = input('Enter phone to change: ')
        a = False
        file_1 = open('Phone_Book.org', 'rb')
        Phone_Book = pickle.load(file_1)
        file_1.close()
        for key in Phone_Book:
            if key == name:
                a = True
                break
        if a is True:
            Phone_Book[name] = phones
            file = open('Phone_Book.org', 'wb')
            pickle.dump(Phone_Book, file)
            file.close()
        else:
            print('Error!!!')
    def del_book(self):
        """Delete record"""
        name = input('Enter name to del: ')
        a = False
        file = open('Phone_Book.org', 'rb')
        Phone_Book = pickle.load(file)
        file.close()
        for key in Phone_Book:
            if key == name:
                a = True
                break
        if a is True:
            del Phone_Book[name]
            file = open('Phone_Book.org', 'wb')
            pickle.dump(Phone_Book, file)
            file.close()
        else:
            print('Error!!!')
a = PhoneBook
init = input('Enter 1 to append, 2 to read, 3 to change, 4 to del, 5 to exit: ')
while str(init):
    if str(init) != str:
        break
    else:
        init = input('Enter 1 to append, 2 to read, 3 to change, 4 to del, 5 to exit: ')
while int(init) >= 1 or int(init) <= 5:
    if int(init) == 1:
        names = input('Enter name: ')
        phone = input('Enter phone: ')
        a = PhoneBook(names, phone)
        a.app()
        init = input('Enter 1 to append, 2 to read, 3 to change, 4 to del, 5 to exit: ')
    elif int(init) == 2:
        PhoneBook.read_book(Phone_Book)
        init = input('Enter 1 to append, 2 to read, 3 to change, 4 to del, 5 to exit: ')
    elif int(init) == 3:
        PhoneBook.change(Phone_Book)
        init = input('Enter 1 to append, 2 to read, 3 to change, 4 to del, 5 to exit: ')
    elif int(init) == 4:
        PhoneBook.del_book(Phone_Book)
        init = input('Enter 1 to append, 2 to read, 3 to change, 4 to del, 5 to exit: ')
    elif int(init) == 5:
        print('Good bye!')
        break
    else:
        print('Error!')
        init = input('Enter 1 to append, 2 to read, 3 to change, 4 to del, 5 to exit: ')[/size][code python][/code]
AL-ME
Это мой код по той же задаче. Громоздкий, но работает. ))))
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB