python中不正确的数据测试
我试图测试用户输入,但它似乎只是第一次通过。如果我第一次给出正确的数据,它会工作,但如果我最初给出错误的数据,然后在重新提示后使用正确的数据,它将永远不会识别它是正确的。你知道为什么它第一次起作用,但之后就不起作用了吗?这是密码python中不正确的数据测试,python,testing,time,Python,Testing,Time,我试图测试用户输入,但它似乎只是第一次通过。如果我第一次给出正确的数据,它会工作,但如果我最初给出错误的数据,然后在重新提示后使用正确的数据,它将永远不会识别它是正确的。你知道为什么它第一次起作用,但之后就不起作用了吗?这是密码 testDate = open("Sales.txt") def DateTest(Date, Position): firstTry = True validSyntax = False Done = False while
testDate = open("Sales.txt")
def DateTest(Date, Position):
firstTry = True
validSyntax = False
Done = False
while Done == False:
while validSyntax == False:
if firstTry == True:
print "debug 2"
try:
Date = Date.strip().split(',')
Year = int(Date[0])
Month = int(Date[1])
Day = int(Date[2])
Date = (Year, Month, Day)
except:
print "That is invalid input."
firstTry = False
else:
validSyntax = True
print "ok got it"
elif firstTry == False:
NewDate = raw_input("Please input the desired %s date in the form YYYY,MM,DD: " % Position)
try :
NewDate = startDate.strip().split(',')
Year = int(NewDate[0])
Month = int(NewDate[1])
Day = int(NewDate[2])
NewDate = (Year, Month, Day)
except:
print "That is invalid input."
else:
validSyntax = True
print" ok got it"
if validSyntax == True:
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 Year == yearTest:
if Month == monthTest:
if Day == dayTest:
Done = True
print "success"
解决当前无法工作的原因:您从未设置过
startDate
,因此尝试strip().split()
将无法工作:
NewDate = raw_input("Please input the desired %s date in the form YYYY,MM,DD: " % Position)
try :
NewDate = startDate.strip().split(',') // startDate isnt set
你可以试试
startDate = raw_input("Please input the desired %s date in the form YYYY,MM,DD: " % Position)
try :
NewDate = startDate.strip().split(',')
我同意评论者的观点,即您可以尝试重构代码以合并重复部分。以下是重构版本:
import time
DATE_FORMAT = '%Y,%m,%d'
def parse_date(date_str, fmt=DATE_FORMAT):
try:
return time.strptime(date_str.strip(), fmt)
except ValueError:
return None
def get_date(adj='', fmt=DATE_FORMAT, prompt='Please enter the {}date (like {}): '):
prompt = time.strftime(prompt.format(adj.strip()+' ' if adj.strip() else '', fmt))
# repeat until a valid date is entered
while True:
inp = raw_input(prompt)
res = parse_date(inp, fmt)
if res is None:
print('Invalid input')
else:
return res
def get_test_date(date, adj='', fmt=DATE_FORMAT):
return parse_date(date, fmt) or get_date(adj, fmt)
def find_date_in_file(fname, date_str='', adj=''):
test_date = get_test_date(date_str, adj)
with open(fname) as inf:
for line in inf:
if test_date == parse_date(line):
print('Found!')
return test_date
def main():
find_date_in_file('Sales.txt', '2012,01,05', 'closing')
if __name__=="__main__":
main()
你的代码非常重复。尝试创建函数来包装重复的块。这样,,你可以更快地找出错误。你应该使用一个循环,而不是重复相同的代码。我第一次输入正确的输入,它是错误的。除了@ AKF的注释之外,还可以使用@ TabChas的副本。你可以考虑使用Python的模块将字符串解析成一个可以使用的<代码>时间>代码>对象。检查。它将抛出一个
ValueError
异常,如果时间格式不正确,您可以捕获该异常。我已修复了大部分代码,但出现了一个问题。正如您所看到的,我的程序中有一个for循环,问题是for循环只运行一次。如果我必须重新运行for循环,您知道如何让它再次运行吗?