为什么我不能将字符串对象附加到字典中

时间:2016-05-30 17:58:03

标签: python dictionary

我是python的新手,只是创建一个管理学生记录的程序,比如数据库,但由于某种原因,我无法将字符串对象附加到字典数据结构中。有什么理由吗?

def main():

# Create an empty dictionary

database = {'FIRST_NAME':'Default', 'LAST_NAME':'Default', 'ID_NUMBER':'Default'}

# Create a menu

print('\t\t\t Student Database program')
print('K: Enter student information')
print('D: Display student information')

option = input('Enter an option')

# Create a decision structure

if option == 'k' or option == 'K':
    # ask the user to enter in a student first name + last name + id number
    firstName = input('enter the students first name: ')
    lastName = input('enter the students last name: ')
    idNumber = int(input('enter the students ID number: '))

    # append these into the empty database
    database['FIRST_NAME'].append(firstName) // The error is most likelyhere
    database['LAST_NAME'].append(lastName) // The error is most likelyhere
    database['ID_NUMBER'].append(idNumber) // The error is most likely here

    print('Success!')

elif option == 'D' or option == 'd':
    # print out the values of each key
    print('Here are the students information')               
    print('')
    print(database['FIRST_NAME'])
    print(database['LAST_NAME'])
    print(database['ID_NUMBER'])

main()

1 个答案:

答案 0 :(得分:1)

您无法“追加”字典,您可以使用其键:值进行更新或添加。

更新

dict_name.update({'item1': 1})

添加:

dict_name['item3'] = 3