Q:

Why is my code printing more than once?

number = raw_input("Please enter the fixture number: ")
    myfile = open('file.txt')
    for line in myfile:
        item = line.split(',')
        if item[0] == number:
            title = '{0:20} {1:20} {2:20} {3:20} {4:20} {5:20}'.format('Fixture number', 'date', 'nickname1', 'nickname2', 'Fixture Played', 'Winning Nickname')
            print(title)
            result = '{0:20} {1:20} {2:20} {3:20} {4:20} {5:20}'.format(item[0],item[1],item[3],item[4],item[5],item[6])
            print(result)
        elif item[0] != number:
            print("not exist")

Hi there I am new with python, so recently I encountered a problem my 
elif item[0] != number:

is printing "not exist" for the amount of lines in my file
how do I get it to print only once if it is wrong?

A:

Set a flag when item[0] == number is True. Check this flag after the loop terminates, and print "not exist" if the flag is set:
myfile = open('file.txt')
found = False
for line in myfile:
    item = line.split(',')
    if item[0] == number:
        found = True
        title = '{0:20} {1:20} {2:20} {3:20} {4:20} {5:20}'.format('Fixture number', 'date', 'nickname1', 'nickname2', 'Fixture Played', 'Winning Nickname')
        print(title)
        result = '{0:20} {1:20} {2:20} {3:20} {4:20} {5:20}'.format(item[0],item[1],item[3],item[4],item[5],item[6])
        print(result)

if not found:
    print("not exist")

An alternative is to use the else option with the for loop, however, that requires that you want to print only the first time that item[0] == number is True (because you would need to break out of the loop to prevent the else clause triggering).

