Advertisement
DrAungWinHtut

todo_file.py

Jun 3rd, 2024
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | None | 0 0
  1. import os #os.system() #cls
  2.  
  3. #global variable
  4. todo = []
  5.  
  6.  
  7. def addTasks():
  8.     global todo
  9.     while True:
  10.         task  = input('task to add: ')
  11.         if task == 'exit':
  12.             print('bye')
  13.             break
  14.         todo.append(task)  
  15.  
  16. def printTodo():
  17.     num = 0
  18.     for task in todo:
  19.         print(f'{num} - {task}')
  20.         num = num + 1
  21.     input('...')
  22.  
  23. def printMenu():
  24.     print('0-Exit')
  25.     print('1-Add Todo')
  26.     print('2-Print Todo')
  27.     print('3-Update Todo')
  28.     print('4-Delete Todo')
  29.     print('5-Save To file')
  30.  
  31. def updateData():
  32.     printTodo()
  33.     ind = input('Enter index to Update: ')
  34.     ind = int(ind)
  35.     value = input('Enter the todo you want to update: ')
  36.     todo[ind] = value
  37.     print('task is updated')
  38.  
  39. def deleteData():
  40.     global todo
  41.     printTodo()
  42.     ind = input('Enter index to Delete: ')
  43.     ind = int(ind)
  44.     del todo[ind]
  45.     print('task is deleted')
  46.  
  47. def saveToFile():
  48.     out_file = open('todo.txt','w')
  49.     for t in todo:
  50.         out_file.write(t+'\n')
  51.     out_file.close()
  52.  
  53. def readFromFile():
  54.     global todo
  55.     in_file = open('todo.txt','r')
  56.     data = in_file.readlines()
  57.     in_file.close()
  58.     for d in data:
  59.         d = d.rstrip() #remove '\n'
  60.         todo.append(d)
  61.        
  62. #Real program START HERE
  63. if __name__ == '__main__': #တိုက်ရိုက်ခေါ်သုံးတာ ဖြစ်ခဲ့ရင် အလုပ်လုပ်မယ်
  64.     readFromFile()
  65.     while True:
  66.         os.system('cls')
  67.         printMenu()
  68.         ans = input('Wht is your choice: 0,1,2,3,4,5: ')
  69.         if ans == '0':
  70.             print('bye')
  71.             saveToFile()
  72.             exit(0)
  73.         elif ans == '1':
  74.             addTasks()
  75.         elif ans == '2':
  76.             printTodo()
  77.         elif ans == '3':
  78.             updateData()
  79.         elif ans == '4':
  80.             deleteData()
  81.         elif ans == '5':
  82.             saveToFile()
  83.        
  84.  
  85.  
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement