Reputation: 31
I have been doing anything I can think of, and it is now getting to the point where I have no clue where to go. After googling errors and general dictionary help I have ended up here...
I have not finished it, the bottom portion of main is not taken care of so disregard it. The preliminary testing has found this error and I do not like to build a program with the start not working as I am fairly new to Python. (this is also my first post on this forum, so if something is not up to snuff, please let me know)
def create(name, age):
contact = {
'name' : name,
'age' : age,
'email' : 'email',
'phone' : 'phone',
}
def getName(contact):
name = input("What's their name? ")
return contact[name]
def getAge(contact):
age = int(input("What's their age? "))
return contact ["name"]
def getPhone(contact):
phone = input("What's their phone number? ")
return contact['phone']
def getEmail(contact):
email = input("What's their email? ")
return contact['email']
def setName(contact, name):
contact['name'] = name
def setAge(contact, age):
contact['age'] = age
def setPhone(contact, phone):
contact['phone'] = phone
def setEmail(contact, phone):
contact['email'] = email
def birthday(contact):
a = getAge(contact)
a +=1
setAge(contact, a)
def show(contact):
print("Name: ", name)
print("Phone number: ", phone)
print("Age: ", age)
print("Email: ", email)
def main():
ann = create('Ann', 21)
roland = create('Roland', 18)
john = create ('John' , 19)
print(getName("contact"))
getAge()
getPhone()
getEmail()
setName()
setAge()
setPhone()
setEmail()
show()
if __name__ == "__main__":
main()
Upvotes: 2
Views: 71
Reputation: 3471
The first thing you should do is return the contact dict
from create()
, like so:
def create(name, age):
return {'name': name,
'age': age,
'email': None,
'phone': None}
That way you'll have access to the dict
that was created by the function call:
>>> john = create('John', 42)
>>> john
{'name': 'John', 'age': 42, 'email': None, 'phone': None}
>>> john['name'] == 'John'
True
>>> john['age'] == 42
True
Doing otherwise would result in a TypeError
. You would see the same error when attempting to call any of your setter functions on the return value of your current implementation of create()
. Take the following function:
def set_name(contact, name):
contact['name'] = name
set_name()
is expecting a dict
which it will modify by changing the value of the 'name'
key to the value of name
. This won't work if contact
is None
.
The error you are asking about is a result of your implementation of the following function:
def get_name(contact):
name = input("What's their name? ")
return contact[name]
If we walk through the body we will see the following, assuming we called it the way you did in your example code: (get_name('contact')
)
>>> name == '<user input>'
True
>>> contact == 'contact'
True
So, when the function tried to read they key name
from contact
it was actually trying to read the character index of the string "contact"
which only takes integers as indices. The function should probably read something like:
def get_name(contact):
return contact['name']
Which would allow you to do the following: (using the same john
as above)
>>> get_name(john)
'John'
Upvotes: 1