Q:

If statement only iterating over 1st line

When printing row in a csv file, how do you get it to iterate over the entire csv instead of one line at a time when performing an if statement?  For example:
import csv
nameid = raw_input("ID please")
with open('cards.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for row in reader:
        row = list(row)
        if nameid == row[0]:
            print row
        else:
            print "sorry"

with this csv:
101,8
102,10
103,5
104,0

will use only the first line, then the second line, and so on, giving me a false statement 3 out of 4 times.  So if I search for "102" in my raw_input, it will print:
"sorry"
['102', '10']
"sorry"
"sorry

A:

You're actually very close to your intended functionality. You don't want to print "sorry" for every line that doesn't match, so you don't want that else block where it is. But you do want to print "sorry" if nothing at all in the file matches. As it turns out, you can just scoot that else block over one level to the left and have it correspond to the for block instead of the if. You'll also need one extra line:
import csv
nameid = raw_input("ID please")
with open('cards.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for row in reader:
        row = list(row)
        if nameid == row[0]:
            print row
            break
    else:
        print "sorry"

Now the else block is only triggered if the for block runs to completion (more info on this syntax here). To make sure it only runs to completion if there are no matches, add the break to immediately exit the loop when you see a match.

