缩进错误Python初学者

时间:2016-01-15 15:12:38

标签: python syntax

我正在通过演讲幻灯片教自己python,但我很难让这些代码工作,我看不出我做错了什么!另外,我如何实现地址以使其处于不同的类中?我不确定最好这样做。这是我到目前为止的尝试

出现错误:

self.phone = phone

IndentationError:意外缩进

简单的Python电话簿

class Person:

    def __init__(self, name, age, phone, address):
        # instance variables, unique to each Person
        self.name = name
        self.age = age
        self.phone = phone
        self.address = address

    def __str__(self):
        # instance variables, unique to each Person

        return "Name: " + self.name + "\n" + "Age: " + self.age + "\n" + "Phone: " + self.phone + "\n" + "Address: " + self.address
persons = []

def createPerson():
    print("\nNew person")
    name = input('  | Name : ')
    age  = input('  | Age  : ')
    phone = input('  | Phone  : ')
    adress = input('  | Address  : ')
    print("Creating person...\n")
    person = Person(name, age, phone, address)
    persons.append(person)

def searchPerson():
    print("\nSearch person")
    key = input('  | Keyword : ')
    print("Searching...\n")
    # Find names that match given keyword
    match = [p for p in persons if p.name.startswith(key)]
    print(str(len(match)) + ' user(s) found :\n')
    for p in match:
        print(p)

if __name__ == '__main__':
    choices = { "1" : createPerson , "2" : searchPerson }
    x = "0"
    while x != "3":
        print(' (1) Create new person')
        print(' (2) Search for a person')    
        print(' (3) Quit')
        x = input('Select an option -> ')
        if x == "1" or x == "2": choices[x]()

2 个答案:

答案 0 :(得分:4)

嗯,首先,有一些错别字阻止您的代码完全正常工作。这一行,例如:adress = input(' | Address : ')应该用address编写(注意双D)。

要将地址添加为新类,只需按照Person的方式进行操作即可。您可以在同一个文件中包含任意数量的类:

class Address:
    def __init__(self, address='No address defined'):
        # You could create separate fields for each part of the address, 
        # for better results in a bigger system
        self.address = address

    def __str__(self):
        return str(self.address)

您还需要更改Person对象的构建方式:

class Person:
    def __init__(self, name, age, phone, address):
        # instance variables, unique to each Person
        self.name = name
        self.age = age
        self.phone = phone
        self.address = Address(address)

    def __str__(self):
        # instance variables, unique to each Person
        return "Name: {}\nAge: {}\nPhone: {}\nAddress: {}".format(self.name, self.age, self.phone, self.address)

def createPerson():
    print("\nNew person")
    name = input('  | Name : ')
    age  = input('  | Age  : ')
    phone = input('  | Phone  : ')
    address = input('  | Address  : ')
    print("Creating person...\n")
    person = Person(name, age, phone, address)
    persons.append(person)

请注意,在覆盖__str__的{​​{1}}方法时,您应该使用Person而不是使用format运算符连接字符串和值。如果您处理多个值,那么性能会有所提高,例如,您也不必担心使用字符串连接数字时会出现问题。

我还建议您使用其他方法搜索用户。我没有检查用户的名字+是否是一个密钥,而是相信检查用户的名字是否包含密钥是一个更好的选择,因为它搜索整个字符串,而不仅仅是搜索:

startswith

最后,我对您的切入点进行了一些更改,以使其对用户更直观并防止不必要的输入:

match = [p for p in persons if key in p.name]

当然,大多数这些变化都是建议,您可以接受或不接受。还有一些其他的修改,我确信可以将代码转换成更“pythonic”的东西,但如果需要的话,可能会被其他人在其他地方解决。唯一真正的答案是将地址转换为单独的类。希望它有所帮助:)

答案 1 :(得分:0)

将输入扫描更改为raw_input

raw_input

为我工作。

private void table_Order_EKeyPressed(java.awt.event.KeyEvent evt) { int row = table_Order_E.getSelectedRow(); if (evt.getKeyCode() == KeyEvent.VK_INSERT) { } try{ if ( evt.getKeyCode()==KeyEvent.VK_DELETE && row<0 ) { System.err.println("No Row has been selected..."+row); }else if(evt.getKeyCode()==KeyEvent.VK_DELETE && row >-1) { model.removeRow(row);//remov with delete key. } }catch(ArrayIndexOutOfBoundsException e){ JOptionPane.showMessageDialog(null, e); } } 给出了您期望的字符串。

还要确保没有缩进标签。用4个空格替换所有标签。