that I must modify since it tells me that it is out of range in python

-2
# -*- coding: utf-8 -*-

import csv


class Contact:


    def __init__(self, name, phone, email):
        self.name = name
        self.phone = phone
        self.email = email


class Agenda:

    def __init__(self):
        self._contacts = []

    def add(self, name, phone, email):
        contact = Contact(name, phone, email)
        self._contacts.append(contact)
        self._save()

    def visualizar(self,):
        for contact in self._contacts:
            self._print_contacto(contact)

    def borrar(self, name):
        for idx, contact in enumerate(self._contacts):
            if contact.name.lower() == name.lower():
                del self._contacts[idx]
                self._save()
                break

    def buscar(self, name):
        for contact in self._contacts:
            if contact.name.lower() == name.lower():
                self._print_contacto(contact)
                break
        else:
            self._not_found()

    def _save(self):
        with open('contacts.csv', 'w') as f:
            writer = csv.writer(f)
            writer.writerow( ('name', 'phone', 'email') )

            for contact in self._contacts:
                writer.writerow( (contact.name, contact.phone, contact.email) )


    def _print_contacto(self, contact):
        print('--- * --- * --- * --- * --- *--- *---')
        print('Nombre: {}'.format(contact.name))
        print('telefono: {}'.format(contact.phone))
        print('Email: {}'.format(contact.email))
        print('--- * --- * --- * --- * --- *--- *---')

    def _not_found(self):
        print('++++++++++++++')
        print('¡No encontrado!')
        print('+++++++++++++++++')

def run():

    libro_agenda = Agenda()

    with open('contacts.csv', 'r') as f:
        reader = csv.reader(f)
        for idx, row in enumerate(reader):
            if idx == 0:
                continue

            libro_agenda.add(row[0], row[1], row[2])


    while True:
        command = str(input('''
                ¿Que deseas hacer?

                [a]ñadir contacto
                [ac]tualizar contacto
                [b]uscar contacto
                [e]liminar contacto
                [l]istar contacto
                [s]alir
            '''))

        if command == 'a':
            name = str(input('Escribe el numero del contacto: '))
            phone = str(input('escribe el telefono de contacto: '))
            email = str(input('escribe el email del contacto: '))

            libro_agenda.add(name, phone, email)

        elif command == 'ac':
            pass

        elif command == 'b':
            name = str(input('Escribe el nombre del contacto: '))

            libro_agenda.buscar(name)

        elif command == 'e':
            name = str(input('Escribe el nombre del contacto: '))

            libro_agenda.borrar(name)


        elif command == 'l':

            libro_agenda.visualizar()

        elif command == 's':
            break
        else:
            print('comando no encontrado')

if __name__ == '__main__':
    print('B I E N V E N I D O  A  T U  L I S T A   D E  C O N T A C T O S')
    run()

and I get the following error:

B I E N V E N I D O  A  T U  L I S T A   D E  C O N T A C T O S
Traceback (most recent call last):
  File "C:\Termporal\contactos.py", line 122, in <module>
    run()
  File "C:\Termporal\contactos.py", line 75, in run
    libro_agenda.add(row[0], row[1], row[2])
IndexError: list index out of range
[Finished in 0.422s]

**
    
asked by lionronald 22.08.2018 в 21:25
source

1 answer

0

@lionronald Your code works if your file is empty. I was able to create, search and list contacts. Without having the csv file that you are using we can not replicate the error. Maybe you modified it by mistake. Move your file, and create an empty "contacts.csv" and add some contacts. After that compare the 2 csv files. I have the impression that the file that generates the error was created with an earlier version of your code.

AH! if you are trying to read a csv created by another program, this may also be the problem.

Good luck, R6

                ¿Que deseas hacer?

                [a]ñadir contacto
                [ac]tualizar contacto
                [b]uscar contacto
                [e]liminar contacto
                [l]istar contacto
                [s]alir
            b
Escribe el nombre del contacto: rolando
--- * --- * --- * --- * --- *--- *---
Nombre: rolando
telefono: 1111111111
Email: [email protected]
--- * --- * --- * --- * --- *--- *---

                ¿Que deseas hacer?

                [a]ñadir contacto
                [ac]tualizar contacto
                [b]uscar contacto
                [e]liminar contacto
                [l]istar contacto
                [s]alir
            a
Escribe el numero del contacto: test
escribe el telefono de contacto: 3333333333
escribe el email del contacto: [email protected]

                ¿Que deseas hacer?

                [a]ñadir contacto
                [ac]tualizar contacto
                [b]uscar contacto
                [e]liminar contacto
                [l]istar contacto
                [s]alir
            l
--- * --- * --- * --- * --- *--- *---
Nombre: rolando
telefono: 1111111111
Email: [email protected]
--- * --- * --- * --- * --- *--- *---
--- * --- * --- * --- * --- *--- *---
Nombre: mrx
telefono: 2222222222
Email: [email protected]
--- * --- * --- * --- * --- *--- *---
--- * --- * --- * --- * --- *--- *---
Nombre: test
telefono: 3333333333
Email: [email protected]
    
answered by 22.08.2018 / 22:17
source