2016-05-30 51 views
0

我是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() 
+0

原來默認的數據串,讓他們空列表像'數據庫= { 'FIRST_NAME':[], 'LAST_NAME': ],'ID_NUMBER':[]}'(這是可以在這裏看到的最基本的錯誤,至少在我眼中) –

+0

你不能將東西附加到字符串。 – BrenBarn

+0

謝謝。我會檢查並確保它有效。 – plee

回答

1

您不能「附加」到字典中,您可以使用其鍵:值進行更新或添加。

更新:

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

地址:

dict_name['item3'] = 3 
+0

感謝您的更新 – plee

+1

@plee:如果此答案對您有所幫助,請點擊答案左上方的向上箭頭表示您的讚賞。如果這是最有用的答案,請通過單擊答案左上角附近的複選標記來顯示。這比在評論中表達你的感謝要好。 –

相關問題