只通过输入fi的第一行

2024-10-08 19:26:48 发布

您现在位置:Python中文网/ 问答频道 /正文

def optionFour(self):
 if self == 4:

    inFile = open('dates.csv', 'r')

    for line in inFile:
        gFile = line.strip()
        gFile = gFile.split(',')

        occurence = gFile[0]
        month = int(gFile[1])
        day = int(gFile[2])
        year = int(gFile[3])
        time = gFile[4]
        event = gFile[5]
        dates = [month, day, year]


        day5 = int(input('Enter a day:'))
        month5 = int(input('Enter a month:'))
        year5 = int(input('Enter a year:'))
        dateSelected = [month5, day5, year5]
        if dates == dateSelected:
            return str((occurence, ' appointment starting on (', dateSelected, '): ', time, ', ', event))
        else:
            return str('Nothing')
            break

    inFile.close()

它只通过输入文件的第一行,为什么会这样呢?它只检查第一行,其他什么都没有


Tags: selfinputiftimelineyearinfileint
1条回答
网友
1楼 · 发布于 2024-10-08 19:26:48

您的代码只读取一行,因为这是您让它执行的操作:读取一行,从用户处获取日期,然后比较这两行。如果匹配,则返回约会;如果不匹配,则返回“Nothing”

请注意,您既不能使用break语句,也不能使用file closure:您的返回在到达函数之前,强制流离开函数

我怀疑您需要的是读取所有行,存储信息,然后要求用户在该存储中查找日期

相关问题 更多 >

    热门问题