2016-04-02 12 views
-5

これは私の最初の投稿です! 私はこの素晴らしい本でラズベリーパイのコーディングPythonを学び始めました。 "ラズベリーパイのラーニングPython"初心者の本からこのpythonコードを説明してください

私は基本的な章で始まり、次に夏の運動がありました。 前の章で説明していたことはありませんでした。コードの流れを理解していただきたいと思います。

私は自分のDropboxにアップロードする自由を取った: click here.
と私も入力この記事の最後に、私は理解していないものを

でコード:

私は彼の定義を理解しないし:{
1)あなたはそれらの流れを説明できますか?
2)prev_gradesとは何ですか?

私はこれ以上の頭を砕いています。このリンクに

students = [["Ben", {"Maths": 67, "English": 78, "Science": 72}], 
      ["Mark", {"Maths": 56, "Art": 64, "History": 39, "Geography": 55}], 
      ["Paul", {"English": 66, "History": 88}]] 

grades = ((0, "FAIL"),(50, "D"),(60,"C"),(70, "B"), (80,"A"), (101, "CHEAT!")) 

def print_report_card(report_student = None): 
    for student in students: 
     if (student[0] == report_student) or (report_student == None): 
      print("Report card for student ", student[0])    
      for subject, mark in student[1].items(): 
       for grade in grades: 
        if mark < grade[0]: 
         print(subject, " : ", prev_grade) 
         break 
        prev_grade = grade[1] 


def add_student(student_name): 
    global students 
    for student in students: 
     if student[0] == student_name: 
      return "Student already in database" 
    students.append([student_name, {}]) 
    return "Student added sucessfully" 

def add_mark(student_name, subject, mark): 
    global students 
    for student in students: 
     if student[0] == student_name: 
      if subject in student[1].keys(): 
       print(student_name, " already has a mark for ", subject) 
       user_input = input("Overwrite Y/N? ") 
       if user_input == "Y" or user_input == "y": 
        student[1][subject] = mark 
        return "Student's mark updated" 
       else: 
        return "Student's mark not updated" 
      else: 
       student[1][subject] = mark 
       return "Student's mark added" 
    return "Student not found" 

while True: 
    print("Welcome the the Raspberry Pi student database") 
    print("What can I help you with?") 
    print("Enter 1 to view all report cards") 
    print("Enter 2 to view the report card for a student") 
    print("Enter 3 to add a student") 
    print("Enter 4 to add a mark to a student") 
    print("Enter 5 to exit") 

    try: 
     user_choice = int(input("Choice: ")) 
    except ValueError: 
     print("That's not a number I recognise") 
     user_choice = 0 

    if user_choice == 1: 
     print_report_card() 
    elif user_choice == 2: 
     enter_student = input("Which student? ") 
     print_report_card(enter_student) 
    elif user_choice == 3: 
     enter_student = input("Student name? ") 
     print(add_student(enter_student)) 
    elif user_choice ==4: 
     enter_student = input("Student name? ") 
     enter_subject = input("Subject? ") 
     num_error = True 
     while num_error: 
      num_error = False 
      try: 
       enter_mark = int(input("Mark? ")) 
      except ValueError: 
       print("I don't recognise that as a number") 
       num_error = True 
     print(add_mark(enter_student, enter_subject, enter_mark)) 
    elif user_choice == 5: 
     break 
    else: 
     print("Unknown choice") 
    input("Press enter to continue") 
print("Goodbye and thank you for using the Raspberry Pi Student database") 
+0

完了!あなたは今それを直接見ることができます – Gwynbleidd

+0

そして、正確に何を理解していませんか? –

+0

私が言ったように、私は彼の定義の流れを理解していません – Gwynbleidd

答えて

0

ゴー:http://pythontutor.com/visualize.html#mode=editあなたのコードを貼り付けし、視覚化の実行を押すと、私はあなたがすべてを理解すると思います。

enter image description here

+0

ありがとう! これは私にとってteコードの流れを説明するのが難しいので、これが最も有益でした。 – Gwynbleidd

関連する問題