简体   繁体   中英

Iterating over python object is 2.7. Showing error not found

The function calls in search_a_record, which in turn calls in find_a_record and display_a_record The self.__records is the list of Contact objects:

: [Contact(John,7589943,john@amail.com), Contact(Kelly,4344345,kelly@bmail.com), Contact(Nicky,8774104,nicky@cmail.com), Contact(Sam,5723943,sam@dmail.com)]

The items in self.__records are stored as "Contact" objects.

If the name is in the "Phonebook" then the "Contact" object needs to be returned by the find_a_record function.

I am iterating over the object and comparing the name but getting error, what am I doing wrong?

1. Look up a contact
2. Add a new contact
3. Change an existing contact
4. Delete a contact
5. Display all contacts
6. Quit the program
------------------------------------------------
Enter your choice: 1
------------------------------------------------
Enter the name: John
Traceback (most recent call last):
  File "/Users/rizwanrenesa/Documents/A1/A1Q1Resource/A1Q1rene001.py", line 91, in <module>
    main()
  File "/Users/rizwanrenesa/Documents/A1/A1Q1Resource/A1Q1rene001.py", line 87, in main
    menu(contacts)
  File "/Users/rizwanrenesa/Documents/A1/A1Q1Resource/A1Q1rene001.py", line 59, in menu
    contacts.search_a_record()
  File "/Users/rizwanrenesa/Documents/A1/A1Q1Resource/PhoneBook.py", line 82, in search_a_record
    name = input("Enter the name: ")
  File "<string>", line 1, in <module>
NameError: name 'John' is not defined


class Contact:
        def __init__(self, name, phone, email):
            self.__name = name                  # the full name
            self.__phone = phone                # the phone number
            self.__email = email                # the email address

        def get_name(self):
            return self.__name

#Finds a record
def find_a_record(self, name):
        for contact in self.__records:  # type: object
            if contact.get_name() == name:
                   return contact
            return None

 def display_a_record(self, item):
        print("Name:{}".format(item.get_name()))
        print("Phone:{}".format(item.get_name()))
        print("Email:{}".format(item.get_name()))

    # This function searches and displays a contact record.
    def search_a_record(self):
        name = input("Enter the name: ")
        isNameExist = self.find_a_record(name)
        if(isNameExist == None):
            print("{} is not found in the phone book.".format(name))
        else:
            self.display_a_record(isNameExist)


<type 'tuple'>: (<type 'exceptions.NameError'>, NameError("name 'John' is not defined",), None)

<type 'list'>: [Contact(John,7589943,john@amail.com), Contact(Kelly,4344345,kelly@bmail.com), Contact(Nicky,8774104,nicky@cmail.com), Contact(Sam,5723943,sam@dmail.com)]

As per the documentation :

  • input([prompt]) :

    Equivalent to eval(raw_input(prompt)) . [...] Consider using the raw_input() function for general input from users.

  • raw_input([prompt]) :

    If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

So instead of calling input , you should call raw_input in python 2.7.

By calling input , you are actually trying to execute what the user entered as if it was Python code. For instance, when typing John , the input function is trying to execute the code John , and that's why you're getting the error name 'John' is not defined . This is definitely not what you want, and this is also a security issue.

Instead, the raw_input is interpreting the user's John typing as the string "John" .


But you should note that raw_input was renamed input in python 3, so be careful if you intend to migrate your code to a newer version of python one day!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM