功能中的流量控制

时间:2015-07-11 01:44:32

标签: python function while-loop controls

scores_on_pretest功能出现问题。

如果我输入两个名字,比如Joe和Bob,当脚本到达此功能时,它会询问Joe的问题#1的分数,然后是Bob的问题#1,然后是Joe等的问题#。

我想要做的就是先让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()

2 个答案:

答案 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()

答案 1 :(得分: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_scores dict上进行迭代,如果重要的话,您不会按照您输入的顺序让学生回来吗?如果是,您可以使用OrderedDict模块中的collections