I am writing a program that uses a gathers information from a file to output later on, my problem right now is with testing the information given. I want to test if the code the user inputted is the same as a date in the file. The file used contains dates and sales in the form YYYY,MM,DD. I am attempting to use a for loop to test each line of the file against the user input but I am getting the error that datetime.date is not iterable. Any solutions/alternatives? Here is the code,
from datetime import date
def DateTest(Date, Position):
firstTry = True
validSyntax = False
if validSyntax == False:
if firstTry == True:
try:
Date = Date.strip().split(',')
Year = int(Date[0])
Month = int(Date[1])
Day = int(Date[2])
Date = date(Year, Month, Day)
except:
print "That is invalid input."
firstTry = False
else:
validSyntax = True
elif firstTry == False:
Date = raw_input("Please input the desired %s date in the form YYYY,MM,DD: " % Position)
try :
Date = startDate.strip().split(',')
Year = int(Date[0])
Month = int(Date[1])
Day = int(Date[2])
Date = date(Year, Month, Day)
except:
print "That is invalid input."
else:
validSyntax = True
print" ok got it"
if validSyntax == True:
for line in Date:
line = line.strip().split(',')
yearTest = int(line[0])
monthTest = int(line[1])
dayTest = int(line[2])
dateTest = date(yearTest, monthTest, dayTest)
if dateTest == Date:
"print debug"
startDate = raw_input("Please input the desired start date: ")
start = "start"
Response = DateTest(startDate, start)
As you can see I test for valid input and then test for the date being in the file, which tells me datetime is not iterable.
x == Falseorx == Truebut simplyxandnot x. Unless you are working with an existing codebase that uses the name style you are using I'd also suggest you to read the relevant part of PEP-8. Other python developers who'll work with your code at some point will thnak you for it!raiseto raise the error that triggered the except statement.