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

 2023-09-11 阅读 29 评论 0

摘要:我正在尝试获取用户输入并交叉引用,以查看它是否在文件中。但是,它只运行for循环一次;如果我在第一次运行时就正确了,如果第一次出错,它就永远不会运行,因为for循环只运行一次。当它为while循环重新运行时,它从不重新运行fo

我正在尝试获取用户输入并交叉引用,以查看它是否在文件中。但是,它只运行for循环一次;如果我在第一次运行时就正确了,如果第一次出错,它就永远不会运行,因为for循环只运行一次。当它为while循环重新运行时,它从不重新运行for循环。为什么python只允许您运行一次循环,我如何解决这个问题?这是密码testDate = open("Sales.txt")

python循环语法,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")

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/48607.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息