我是学习 python 的新手,运行程序(我的代码)时遇到了困难,cmd 识别出我有 3.12 Python 版本,但是当我运行使用 VS StudioCode 编写的代码时,该文件不执行任何操作。我已经修复了该目录,但它仍然保持原样(无法识别 python 文件)。 为了能够按预期运行我的代码,这是我所做的代码:
import os
from datetime import datetime, timedelta
def read_task(file1):
tasks = {}
if os.path.exists(file1):
with open(file, 'r')as file:
for line in file:
task, completed, creation_date = line.strip().split(',')
tasks[task] = {'completed': bool(int(completed)), 'creation_date': datetime.strptime(creation_date, '%Y-%m-%d')}
return task
def write_tasks(file1, tasks):
with open(file1, 'w') as file:
for task, details in task.items():
completed = details['completed']
creation_date = details[creation_date].strftime('%Y-%m-%d')
file.write (f'{task},{int(completed)},{creation_date}\n')
def main():
file1 = 'task.txt'
tasks = read_task(file1)
current_date = datetime.now()
for task, details in tasks.items():
completed = details['completed']
creation_date = details['creation_date']
if completed and (current_date - creation_date).days >= 7:
tasks[task][completed]= False
print ("Current tasks:")
completed = details['completed']
print (f'{task}: {"Done" if completed else "To do"}')
completed_task = input("Enter a completed task: ")
tasks[completed_task]['completed'] = True
write_tasks(file1, tasks)
if __name__ == "__main__":
main()
以下内容的缩进错误:
if name == "main": 主要()
它不应该在主要功能下