2016-11-23 11 views
0

私は学生名で記入する辞書を作成する出欠ロガーを作成しようとしています。名前はクラスの出席データ(クラスに参加したかどうかに関係なく)を追加するリストです。私はこれまでデフォルト値を持つ辞書の項目を自動的に追加するpython

#! /bin/python3 
#add student to dict 
def add_student(_dict): 
    student=input('Add student :') 
    _dict[student]=[] 
    return _dict 

#collect outcomes 
def collector(student,_dict, outcome): 
    _dict[student].append(outcome) 
    return _dict 

#counts target 
def count(_dict,target): 
    for i in _dict: 
     # records total attendance names 
     attendance_stat = len(_dict[i]) 
     # records total instances absent 
     freq_of_absence=_dict[i].count(target) 
     # records percentage of absence 
     perc_absence = float((freq_of_absence/attendance_stat)*100) 
     print(i,'DAYS ABSENT =',freq_of_absence) 
     print('TOTAL DAYS: ', i, attendance_stat) 
     print('PERCENTAGE OF ABSENCE:', i, str(round(perc_absence, 2))+'%') 

#main function 
def main(): 
    #date=input('DATE: ') 
    outcomes=['Y','N'] 
    student_names = {} 
    try: 
     totalstudents = int(input('NO. OF STUDENTS: ')) 
    except ValueError: 
     print('input an integer') 
     totalstudents = int(input('NO. OF STUDENTS: ')) 
    while len(student_names) < totalstudents: 
     add_student(student_names) 
    print(student_names) 
    i = 0 
    while i < totalstudents: 
     i = i + 1 
     target='Y' 
     student=str(input('student :')) 
     outcome=str(input('outcome :')) 
     collector(student,student_names,outcome) 
    count(student_names,target) 

if __name__=='__main__': 
    main() 

` below`コードが表示されているコードは、これまでのところうまく動作しますが、問題は、学生の数が多すぎる場合、入力にかかる時間は授業時間にで切断広範囲です。不在者の数は通常存在する数よりも少ないため、欠席した各選択肢に対して値Yを付加する欠席の辞書の生徒から選択することが可能であり、Nを残りのリストに追加することが可能である。

答えて

0

これはまさにあなたが求めていることではありませんが、私はそれが助けになると思います。ユーザーに2回目の名前のたびに名前を入力するように依頼するのではなく、名前を自分で印刷して結果だけを尋ねるのはなぜですか?

for student_name in student_names: 
    outcome = input("Outcome for {}: ".format(sudent_name)) 
    collector(student_name, student_names, outcome) 

ます。また、結果は空の文字列であるかどうかを確認するためにいくつかのロジックを追加し、そうであれば、「N」に設定することができます:あなたの最後のwhileループは、このように、代わりにループのためになるだろう。これにより、ほとんどの名前に対してenterキーを押すだけで、存在しない特定のものには 'Y'を入力するだけで済みます。これは次のようになります。

for student_name in student_names: 
    outcome = input("Outcome for {}: ".format(sudent_name)) 
    if outcome = "": 
     outcome = "N" 
    collector(student_name, student_names, outcome) 
+0

ありがとう、ありがとうございました。私は今生徒を追跡して、何も残さないようにすることができます。 –

+0

ようこそ。それがあなたを助けたら解決策としてマークしてもらえますか? – user3030010

関連する問題