简体   繁体   English

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

[英]Why can't I append a string object to a dictionary

I am new to python and was just creating a program that manages student records like a database but for some reason I am not able to append a string object into a dictionary data structure. 我是python的新手,只是创建了一个程序来管理学生记录(例如数据库),但是由于某种原因,我无法将字符串对象附加到字典数据结构中。 Any reason why? 有什么原因吗?

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()

You cannot "append" to a dictionary, you can update or add by using its key:value. 您不能“追加”到字典,可以使用其key:value进行更新或添加。

update: 更新:

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

add: 加:

dict_name['item3'] = 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不能从字典中追加此值? - why can't I append this value from a dictionary? 为什么我不能为一个项目附加字典索引但我可以为任何其他项目? - Why can't I append a dictionary index for one item but I can for any other item? 不能 append 到字典 - Can't append to dictionary 在读取 txt 文件行时。 我不能“追加列表”或“更新字典”为什么? - While reading txt file lines. I can't “append list” or “update dictionary” why? 为什么我不能在我的字典中附加值? - Why aren't i able to append the values in my dictionary? 为什么我不能像 [...].append(...) 那样使用 append() 方法? - Why can't I use append() method in the way like that […].append(…)? 为什么不能从字符串中提取10位数字,然后用它在字典中找到相应的键? - Why can't I take a 10 digit number from a string and use it to find the corresponding key in a dictionary? 我似乎无法删除值包含某个字符串的字典的键,为什么? - I can't seem to delete keys of a dictionary whose value contains a certain string, why? 为什么我不能在列表中的对象上调用 __dict__ ,而该对象恰好在字典中的列表中的另一个对象中? - Why can't I call __dict__ on object in list that happens to be within another object within a list within a dictionary? 不能将 append 字符串添加到列表中 - can't append string to a list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM