I'm teaching myself python from lecture slides but I'm struggling to get this code to work, I can't see what I did wrong! Also how would I implement address so that it is in a different class? I'm not sure the best to do this. This my attempt so far
Error that appears:
self.phone = phone
IndentationError: unexpected Indent
Simple Python phonebook
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]()
adresswith one 'd' but then expecting to access it asaddresswith two. Your title says you're getting an IndentError. Indentation looks ok in your StackOverflow rendering of the code, but If that's the error, verify that the indentation is consistent throughout and you're not mixing spaces and tabs.