二手产品经理

二手产品经理

THIS IS RENO

操作文件 - 51天 - 在线学python100天

记录#

  1. 今天学习文件的读写操作。
  2. 比较特别的意思是将数组或字典写入到文件的时候,需要使用 str () 函数把这种数据转为字符串。
  3. 那再读取的时候,就需要用 eval () 函数把字符串转为动态可执行的代码。
  4. 今天这节课刚开始的时候有点迷迷糊糊,在 eval () 的使用上纠结了很久。直到在练习今天编码的时候才明白其中的逻辑,为什么要 str 和 eval。
  5. 还是有一点,写文件和 RAM 有什么关系?这节课 RAM 的用法或知识点在什么地方体现了?我还是不明白。
  6. 今天的练习是对 45 的编码进行完善,增加写文件和读文件的操作。第一次运行代码会报错,光头大哥说明天解决这个问题,哈哈哈!

CODE#

import os

print("🌟Life Organizer🌟")
print()
todolist = []
todoinfo = []
f = open("to.do", "r")
todolist = eval(f.read())
f.close


def todoprint():
  for row in todolist:
    for name in row:
      print(f"{name: ^10}", end=" | ")
    print()


select = "again"
while True:
  os.system("clear")
  if select == "again":
    print("Menu: ")
    print("1: Add ")
    print("2: Remove ")
    print("3: View ")
    print("4: Edit ")
    menu = input("")
    if menu.strip().lower()[0] == "a":
      task = input("What is the task? > ")
      due = input("When is it due by? > ")
      priority = input("What is the priority? > ")
      todoinfo = [task, due, priority]
      todolist.append(todoinfo)
      print("Thanks, this task has been added.")
      todoprint()
    elif menu.strip().lower()[0] == "r":
      name = input("Which one do you want remove? > ")
      for row in todolist:
        if name in row:
          todolist.remove(row)
          print("Thanks, this task has been removed.")
        else:
          print(f"{name} is not in the todolist")
    elif menu.strip().lower()[0] == "v":
      todoprint()
    elif menu.strip().lower()[0] == "e":
      name = input("Which one do you want remove? > ")
      for row in todolist:
        if name in row:
          todolist.remove(row)
          print("Thanks, this task has been removed.")
        else:
          print(f"{name} is not in the todolist")
      task = input("What is the task? > ")
      due = input("When is it due by? > ")
      priority = input("What is the priority? > ")
      todoinfo = [task, due, priority]
      todolist.append(todoinfo)
      print("Thanks, this task has been added.")
      todoprint()

    f = open("to.do", "w")
    f.write(str(todolist))
    f.close()

  elif select == "quit":
    break

  select = input("Do you want to see the menu again or quit? ")

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。