我一直在做我能想到的任何事情,現在已經到了我不知道要去哪裏的地步。谷歌搜索錯誤和一般字典幫助後,我已經在這裏結束了...Python 3 Dictionary「list indices need to be integers not str」
我還沒有完成它,主要的底部沒有照顧,所以無視它。初步測試發現了這個錯誤,我不喜歡在開始時沒有開發一個程序,因爲我對Python很新。 (這也是我在這個論壇上的第一篇文章,所以,如果事情是不及格,請讓我知道)
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()
你有什麼期望'的getName(「接觸」)'去做? – jwodder
預期產量是多少? –
你不是從'create()'函數返回創建的字典。另外,你在main()中的調用對我來說並不清楚,除了創建之外基本上沒有任何意義。您必須將創建的字符串作爲參數傳遞... –