python如何循环执行_如何在python中多次运行for循环?

我正在尝试获取用户输入并交叉引用,以查看它是否在文件中。但是,它只运行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")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值