我正在尝试获取用户输入并交叉引用它以查看它是否在文件中。但是,它只运行一次 for 循环;如果我第一次做对了,如果我第一次做错了,那么它永远不会做,因为 for 循环只运行一次。当它重新运行 while 循环时,它永远不会重新运行 for 循环。为什么python只允许你运行一次循环,我该如何解决这个问题?这是代码,
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")