开发者

Problem assigning input to list

I know this is probably an easy question but after reviewing the documentation for python 2.6.4 I cannot seem to find out what is wrong. This is my file, in it's entirety. The problem I am having is in get_phone_number(). After asking for the amount of phone numbers, I get this error:

Traceback (most recent call last):
  File "/home/charles/workspace/HelloWorld/hello.py", line 27, in <module>
    c.get_phone_number()
  File "/home/charles/workspace/HelloWorld/hello.py", line 17, in get_phone_number
    self.phone_number[phone_count]
AttributeError: Contact instance has no attribute 'phone_number'

It seems that I am able to define an attribute on the fly, but not if it is a list? Please help!

class Name():
        def get_name(self):
            self.first = raw_input("First Name?\n")
            self.middle = raw_input("Middle Name?\n")
            self.last = raw_input("Last Name?\n")
        pass
    class Address():
        def get_address(self):
            self.street = raw_input("Street?\n")
            self.city = raw_input("City?\n")
            self.zip = raw_input("Zip Code?\n")
        pass
    class Phone_Number():
        def get_phone_number(self):
            count = 0
            phone_count = raw_input("How many phone numbers?\n")
          开发者_开发百科  self.phone_number[phone_count]
            while count < (phone_count - 1):
                self.phone_number[phone_count] = raw_input("Phone Number: ")
                phone_count -= 1
        pass
    class Contact(Name, Address, Phone_Number):
        pass
    c = Contact()
    c.get_name()
    c.get_address()
    c.get_phone_number()


replace

self.phone_number[phone_count]

with

self.phone_number = []

The first statement does nothing (actually, it tries to access the phone_count-th element of a list called phone_number in self, which does not exist, and hence the error).

The second statement defines a new list called phone_number.


Your code contains quite a few errors and is not Pythonic. Some points: you need to convert the first user input to an integer. You can use a for loop instead of a while loop. On Python 2.5 you can use xrange instead of range but for Python 3.0 use range. Use append to add an item to a list.

class Phone_Number():
    def get_phone_numbers(self):
        phone_count = int(raw_input("How many phone numbers?\n"))
        self.phone_numbers = []
        for _ in range(phone_count):
            self.phone_numbers.append(raw_input("Phone Number: "))

p = Phone_Number()
p.get_phone_numbers()
for phone_number in p.phone_numbers:
    print phone_number


You need to initialise self.phone_number

 def get_phone_number(self):
            count = 0
            phone_count = raw_input("How many phone numbers?\n")
            self.phone_number = []
            while count < (phone_count - 1):
                self.phone_number.append (raw_input("Phone Number: "))
                phone_count -= 1
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜