-1

I am trying to get user input and cross-reference it to see if it is in a file. However, it only runs the for loop once; if I get it right the first time it works, if I get it wrong the first time then it never does because the for loop only runs once. When it re-runs for the while loop it never re-runs the for loop. Why does python only allow you to run loop once, and how do I fix this? Here is the code,

testDate = open("Sales.txt")


def DateTest(Position):


    validSyntax = False
    Complete = False
    DateIn = True
    while Complete == False:

            if DateIn == False:
                    print
                    print "That date is not in the file."
                    print
            Date = raw_input("Please input the desired %s date in the form YYYY,MM,DD: " % Position)

            try :
                Date = Date.strip().split(',')
                Year = int(Date[0])
                Month = int(Date[1])
                Day = int(Date[2])
                Date = (Year, Month, Day)
            except:
                print
                print "That is invalid input."
                print
            else:
                validSyntax = True

            if validSyntax == True:
                #It only runs this once, if I put a debug statement after the 'for' #then it never prints out, the loop never runs after the first time
                for line in testDate:
                    line = line.strip().split(',')
                    yearTest = int(line[0])
                    monthTest = int(line[1])
                    dayTest = int(line[2])
                    dateTest = (yearTest, monthTest, dayTest)
                    if Date == dateTest:
                        Complete = True
                        print 'success'

            DateIn = False
            validSyntax = False


Response = DateTest("start")
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user1443488
  • 37
  • 1
  • 2
  • 4
  • horrible code, you should take a look the the [Pep8](http://www.python.org/dev/peps/pep-0008/) and use [time.strptime](http://docs.python.org/library/time.html#time.strptime) – dav1d Jun 18 '12 at 12:33

1 Answers1

3

Treating a file as an iterable moves the read pointer to the end of each line as it's read. Once you reach the end of the file, there's no more data to read from it.

You have 2 options. If you're happy to keep the file open and read it repeatedly, you can do testDate.seek(0) before the for loop, which will move the pointer back to the beginning.

Or, if the file is relatively small and you want to avoid constant disk access, you can read the whole file into a list of lines at the beginning of the script, replacing the open call at the top with something like: testDate = open("Sales.txt").readlines()

Wooble
  • 87,717
  • 12
  • 108
  • 131
  • Thanks a lot, the readlines command fixed it. I actually used readlines later on in the program but never thought to use it here. – user1443488 Jun 18 '12 at 12:59