2016-05-30 25 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' のようなものを作り、文字列です[ ]、 'ID_NUMBER':[]} '(これは私の目には少なくともここで見られる最も基本的な間違いです) –

+0

あなたは文字列に物を付けることはできません。 – BrenBarn

+0

ありがとうございます。私はこれが動作することを確認して確認します。 – plee

答えて

1

辞書に「追加」することはできません。そのキー:値を使用して更新または追加することができます。

更新:

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

を追加します。[]、 'LAST_NAME':

dict_name['item3'] = 3 
+0

更新ありがとうございます – plee

+1

@plee:この回答が役に立った場合は、答えの左上にある上向き矢印をクリックして感謝の意を表してください。それが最も役に立つ答えなら、答えの左上隅にあるチェックマークをクリックして表示してください。これはコメントであなたの感謝を述べるよりも良いです。 –

関連する問題