1

Having trouble with the scores_on_pretest function.

If I input two names, say Joe and Bob, when the script reaches this function it asks for the score for Question #1 for Joe and then Question #1 for Bob, then Question #2 for Joe etc.

What I want to do is have all the questions for just Joe first and then move on to Bob. I know it must be an expression in the wrong place but can't seem to find which one.

Thanks for any suggestions!

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()
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
George
  • 13
  • 3
  • Please add the "python" tag to your question. – Bacon Jul 11 '15 at 01:48
  • Did you try reversing the order of your loops? So you iterate over the students in the outer loop then iterate over the questions in the inner loop? Also note as you store your students in a dict it wont preserve the order you entered the student names in. – Paul Rooney Jul 11 '15 at 02:47

2 Answers2

0

You need to change the order of your while and for loop. Here's a working refactoring of your code:

 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()
Bacon
  • 1,814
  • 3
  • 21
  • 36
0

Just reverse the order of your two loops. So you first iterate over the students and then for each student you iterate for each question.

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)

Note that iterating over the pretest_scores dict, you will not get the students back in the order you entered them, if that matters? If it does you could use an OrderedDict from the collections module.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
  • Makes sense - I knew it had something to do with the order. I had one question about the code you entered. I understand what you did with changing some of the contents of the functions and cleaning things up. Could you explain to me the block starting with if _name_ == '_main_"? Is this referencing a class? – George Jul 13 '15 at 02:51
  • No prob. That's not strictly necessary. It can be taken away and it wont stop it working. All it does is allows a script to be used as either a module or a self contained script see [here](http://stackoverflow.com/questions/419163/what-does-if-name-main-do) for full explanation. Seeing as this script might have limited reuse value as a module its really not adding much to the script. – Paul Rooney Jul 13 '15 at 02:57