Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So, I decided to work off of your second example, since it was the easiest for me to follow.</p> <p>Here is a modified file, with explanations, which does what I think you want. I assumed the student answers were in a text file called <code>student_answers.txt</code> and had one answer per line.</p> <pre><code>#!/usr/bin/env python def read_student(path): with open(path, 'r') as infile: contents = infile.read() # Automatically gets rid of the newlines. return contents.split('\n') def pass_fail(answers, student): correct_list = [] wrong_list = [] # Use the 'enumerate' function to return the index. for index, (ai, bi) in enumerate(zip(answers, student)): # since problems start with 1, not 0 problem_number = index + 1 if ai == bi: correct_list.append(problem_number) else: wrong_list.append(problem_number) # Print the length of the list, not the list itself. print(len(correct_list), 'were answered correctly') print(len(wrong_list), 'questions were missed') if len(correct_list) &gt;= 15: print('Congrats, you have passed') else: print('Sorry, you have failed. Please study and try again in 90 days') print('Any attempt to retake test before 90 days will result in suspension of any licenses') # Display each missed number on a separate line print ('You missed questions numbers:') for num in wrong_list: print(' ' + str(num)) def main( ): key = ['B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'] student_answers = read_student('student_answers.txt') pass_fail(key, student_answers) if __name__ == '__main__': main() </code></pre> <p>Some general comments:</p> <ul> <li>In your <code>main</code> function, you created a key, printed it, and tried to access it inside the <code>pass_fail</code> function. That won't work -- when you create a variable inside a function, it is not automatically accessible outside of the function. There were several solutions you could have done: <ul> <li>Made <code>key</code> a global variable. This is an <a href="https://stackoverflow.com/q/484635/646543">undesirable solution</a></li> <li>Pass the <code>key</code> variable into the <code>pass_fail</code> function. This is what I did.</li> </ul></li> <li><p>Your code to read the student's file was a bit too complex. You could use the <code>with</code> statement, which automatically opens and closes the file object (<code>infile</code>) for you. Similarly, Python comes with built-in ways to help split the string in your textfile up and remove the newline character. In addition, for the future, you can also iterate over each line in a file like the below instead of manually looping through it:</p> <pre><code>with open('file.txt') as my_file: for line in my_file: print(line) # The newline is still a part of the string! </code></pre></li> <li><p>I'm not sure why you had both the <code>wrong_list</code> and <code>missed_list</code> variables inside <code>pass_fail</code>. I deleted <code>missed_list</code>.</p></li> <li>The problem with your <code>for</code> loop was that you were storing the correct answers, not the problem numbers. To solve that, I used the built-in <a href="http://docs.python.org/2/library/functions.html#enumerate" rel="nofollow noreferrer"><code>enumerate</code></a> function which is quite useful for this sort of thing. Alternatively, you could have used a variable and incremented it once per loop.</li> <li>Your <code>print</code> statements were a little malformed, so I cleaned that up</li> <li>When printing the results for the user, you don't want to print the list. Rather, you want to print the <em>length</em> of the list.</li> <li>I don't know if this was intentional or not, but your key didn't have strings in it, and instead had just the letter names. </li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload