这是我的个人计划。我用Python编写了一个简单的七天日程程序,它将任务存储为字符串,保存到.dat文件中,并在输入一周中的一天时返回该任务。这个在终端机上工作。我一般都是刚开始编程的。
这是Python3.8.2,32位,在虚拟环境中。
# Pickle module allows you to save data after program closes
import pickle
week = {"Sunday": [], "Monday": [], "Tuesday": [],
"Wednesday": [], "Thursday": [], "Friday": [], "Saturday": []}
def agenda():
option = input(
"What would you like to do? (A - look at agenda, B - create task, C - clear list, or D - quit) ").upper()
if option == "A":
look_at_agenda()
elif option == "B":
create_task()
elif option == "C":
clear_list()
elif option == "D":
print("Have a nice day!")
else:
print("Invalid option.")
agenda()
# global allows day variable in other functions to work even without declaring it
def valid_days():
global day
day = input("Which day? ").capitalize()
week_list = [day_list for day_list in week.keys()]
if day not in week_list:
print("Invalid day.")
valid_days()
# Loads whatever string from week.dat file
def look_at_agenda():
see_all = input("See entire week? Y - yes, N - no. ").upper()
if see_all == "Y":
week = pickle.load(open("week.dat", "rb"))
print(week)
elif see_all == "N":
valid_days()
week = pickle.load(open("week.dat", "rb"))
print(week[day])
else:
print("Invalid option.")
look_at_agenda()
agenda()
def create_task():
valid_days()
task = input("Describe your task. ")
# Loads string from week.dat file; when program restarts, it allows you to create a task without deleting the saved ones
week = pickle.load(open("week.dat", "rb"))
week[day].append(task)
# Saves string into week.dat file so it remembers when you open the program again
pickle.dump(week, open("week.dat", "wb"))
agenda()
def clear_list():
clear_all = input("Clear all lists? Y - yes, N - no ").upper()
if clear_all == "N":
valid_days()
week[day].clear()
print(f"List cleared for {day}!")
# saves the new list into a week.dat file
pickle.dump(week, open("week.dat", "wb"))
elif clear_all == "Y":
[week[day].clear() for day in week]
# saves the now empty list into a week.dat file
pickle.dump(week, open("week.dat", "wb"))
else:
print("Invalid option.")
clear_list()
agenda()
agenda()发布于 2020-07-19 05:03:19
欢迎来到代码评审社区。代码看起来很干净,并遵循PEP8样式指南。不过,以下是您可以提出的一些建议/修改:
agenda(),而是将其放在这个if __name__块中。message。如果没有提供valid\_inputs,则返回用户的实际值。“如果valid_inputs: valid_inputs = map(lambda : x.upper(),valid_inputs),而True: user_input = input(message)如果不是valid_inputs:返回user_input user_input = user_input.upper(),如果user_input在valid_inputs中返回user_input打印(“无效选项被选中。再试一次”)。上面的例子适用于您的情况,您需要的所有输入选项都是大写字母。它可以根据需要扩展到支持其他验证器。main是从if __name__ == "__main__"块内部调用的函数。https://codereview.stackexchange.com/questions/245683
复制相似问题