这个代码每次都返回不在文件中的日期,我不知道为什么。你知道吗
testDate = open("Sales.txt")
#Declaring variables for later in the program
printNum = 1
newcost = 0
startTestLoop = 1
endTestLoop = 1
#Creating a loop in case input is invalid
while startTestLoop > 0:
#Asking user for start date
#startDate = raw_input("Please input the desired start date in the form YYYY,MM,DD: ")
#Checking if input is valid, and arranging it to be used later
try :
startDate = startDate.strip().split(',')
startYear = startDate[0]
startMonth = startDate[1]
startDay = startDate[2]
startYear = int(startYear)
startMonth = int(startMonth)
startDay = int(startDay)
startDate = date(startYear, startMonth, startDay)
#Informing user of invalid input
except:
"That is invalid input."
print
#EndTry
#Testing to see if date is in the file, and informing user
if startDate not in testDate:
print "That date is not in the file."
#Exiting out of loop if data is fine
else:
startTestLoop -= 1
print "Aokay"
#EndIf
表达式
not in
测试iterable(列表、元组甚至字符串)中元素的成员身份。如果一个日期(或任何其他相关内容)在一个打开的文件中,它的工作原理与您在测试中假设的不一样。您必须逐行遍历文件并询问日期(作为字符串)是否在一行中,那里您可以使用not in
。你知道吗编辑:
如评论中所建议的,您可以使用:
。。。但是无论如何,您需要确保文件中的日期和代码中的日期都使用相同的字符串格式。你知道吗
相关问题 更多 >
编程相关推荐