記錄#
- 今天的練習是:使用讀取和存儲文件的方式保存想法。
CODE#
import os, time, random
while True:
menu=int(input("新增一個想法還是查看隨機想法? \n1. 新增\n2. 閱讀\n3. 隨機\n"))
time.sleep(1)
os.system("clear")
if menu == 1:
f = open("idea.list","a+")
text = input()
f.write(f"{text}\n")
f.close
elif menu == 2:
f =open("idea.list","r")
text = f.read()
print(text)
f.close
elif menu == 3:
f=open("idea.list","r")
idealist=[]
while True:
contents=f.readline().strip().split()
if not contents:
break
idealist.append(contents)
idea = random.choice(idealist)
print(idea)