2017-11-10 16 views
0

私はpython 3でプログラムを作成しようとしています。ここでは、学生名(氏名)とID番号(ID)をカンマで区切って入力します。そして、次の形式の例では、すべての辞書値を提示: 名前:ジョンID:123 名:メアリーID:234 名:イヴID:345 ユーザー入力qをしない限り、永久に実行するプログラムを終了さ生徒用記録用のPython辞書

今まで私は以下を作成することができました:

addName = raw_input("New Name:") 
addid = raw_input("ID:") 
dictionary = {} 
dictionary[addName] = addid 
print('Name: ' + addName + ' ID: ' + addid) 

私が終了するためにqを入力しない限り、プログラムを続けて実行したいと思います。 1行で入力を行うにはどうすればいいですか?ユーザはカンマで区切ってその名前とIDを入力します。辞書に含まれているすべての名前とIDも表示します。

+0

whileループを使用します。 – xaav

+0

プログラミングには全く新しいものでなければならない – xaav

+0

はい私は午前中です。あなたは何を意味するのか教えていただけますか? – john

答えて

0

私はかなり後悔していますが、私は自分自身をコーディングするのはかなり新しいですが、ここに私が協力して助けてくれるものがあります。コード全体で多くのコメントを使用して、私が何をしようとしているのか知っています。

# Initilize values 
i = 0 
StuName = None 
ID = None 
students = {} 

# Loop structure that says "While this is true, do this" 
while 1: 
    i+=1 
    student=input('Enter student name and ID or q to exit: ') 

# if the user enters 'q' break out of the loop 
    if student=='q': 
     break 

    else: 

# This line takes "John,123" and splits it on the comma into "John" and "123". 
# It stores these under the variables StuName and ID 
     StuName, ID = student.split(',') 

# This line updates the "students" dictionary with whatever value "StuName" is and "ID" is 
     students.update({StuName:ID}) 

# This line iterates through the "students" dictionary and prints according to how we say  
for StuName, ID in students.items(): 

#This line is mostly fancy formatting. the \t is a tab, {} is where your variables (StuName, ID) will go 
    print ('Name: {}\tID: {}'.format(StuName, ID))