2015-07-11 45 views
1

scores_on_pretest功能發生故障。功能中的流量控制

如果我輸入兩個名字,說喬和鮑勃,當腳本達到此功能,它要求的分數問題#1喬,然後問題#1鮑勃,則問題2喬等

我想要做的就是首先爲Joe提出所有問題,然後轉向Bob。我知道它一定是在錯誤的地方表達,但似乎無法找到哪一個。

感謝您的任何建議!

students_pretest = [] 

print "First we will collect the student names on the pre-test." 

def student_names_pretest(): 
    while True: 
     name = raw_input("Type in a name or type 'done':") 
     if name == "done": 
      break 
     students_pretest.append(name) 

    students_pretest.sort() 
    print students_pretest 

student_names_pretest() 


pretest_scores = {} 

for name in students_pretest: 
    pretest_scores['%s' % name] = [] 

question_number = int(raw_input("How many questions are on this assessment?: ")) 

def scores_on_pretest(): 
    current_question = 1 
    while current_question <= question_number: 
     for student in pretest_scores: 
      print "Pre-test scores for %s" % student 
      score = int(raw_input("Enter a score for Question #%d: " % current_question)) 
      pretest_scores[student].append(score) 
     current_question =+ current_question + 1 


    print pretest_scores 

scores_on_pretest() 
+0

請將「python」標籤添加到您的問題中。 – Bacon

+0

你是否嘗試顛倒你的循環順序?所以你迭代外循環中的學生然後迭代內循環中的問題?另外請注意,當您將學生存儲在字典中時,它不會保留您輸入學生姓名的順序。 –

回答

0

您需要更改while和for循環的順序。這裏有一個工作重構你的代碼:

students_pretest = [] 

    print "First we will collect the student names on the pre-test." 

    def student_names_pretest(): 
     while True: 
      name = raw_input("Type in a name or type 'done':") 
      if name == "done": 
       break 
      students_pretest.append(name) 

     students_pretest.sort() 
     print students_pretest 

    student_names_pretest() 


    pretest_scores = {} 

    for name in students_pretest: 
     pretest_scores['%s' % name] = [] 

    question_number = int(raw_input("How many questions are on this assessment?: ")) 

    def scores_on_pretest(): 
     current_question = 1 
     for student in pretest_scores: 
      print student 
      while current_question <= question_number: 
       print "Pre-test scores for %s" % student 
       score = int(raw_input("Enter a score for Question #%d: " % current_question)) 
       pretest_scores[student].append(score) 
       current_question =+ current_question + 1 
      current_question = 1                                              

     print pretest_scores 

    scores_on_pretest() 
0

只是顛倒你的兩個循環的順序。因此,您首先迭代學生,然後針對每個問題迭代每個學生。

print("First we will collect the student names on the pre-test.") 

def student_names_pretest(): 
    students_pretest = [] 
    while True: 
     name = raw_input("Type in a name or type 'done':") 
     if name == "done": 
      break 
     students_pretest.append(name) 

    students_pretest.sort() 
    return students_pretest 

def scores_on_pretest(num_questions): 
    pretest_scores = {} 

    for name in students_pretest: 
     pretest_scores[name] = [] 

    for student in pretest_scores: 
     print ("Pre-test scores for %s" % student) 
     for current_question in range(num_questions): 
      score = int(raw_input("Enter a score for Question #%d: " % (current_question + 1))) 
      pretest_scores[student].append(score) 

    return pretest_scores 

if __name__ == '__main__': 
    students_pretest = student_names_pretest() 
    print(students_pretest) 

    num_questions = int(raw_input("How many questions are on this assessment?: ")) 
    pretest_scores = scores_on_pretest(num_questions) 
    print(pretest_scores) 

注意遍歷pretest_scoresdict,你會不會讓學生早在您輸入的順序,如果該事項?如果是這樣,您可以使用collections模塊中的OrderedDict

+0

有意義 - 我知道它與訂單有關。我對你輸入的代碼有一個問題。我明白你在改變函數的某些內容和清理東西方面所做的一切。你能否向我解釋if _name_ =='_main_「開頭的塊?這是否引用了一個類? – George

+0

沒有什麼問題,這不是必須的,它可以被帶走並且不會停止它的工作。腳本被用作模塊或自包含腳本請參閱[這裏](http://stackoverflow.com/questions/419163/what-does-if-name-main-do)的完整解釋。看到這個腳本可能有限的重用值作爲一個模塊,它並沒有增加太多的腳本。 –