让用户输入周一到周五的课程名称,每天的课程依次输入,逗号隔开
程序输出课程表表格如下:

---------------------------------
周一  周二  周三  周四  周五
语文  数学  体育  英语  道德
数学  数学  语文  英语  体育
数学  数学  语文  英语  体育
语文  数学  体育  英语  道德
---------------------------------

用户可以查询某一天的第几节课。

实现

题目要求已实现,并且使用转为json格式存入本目录.cs文件。

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
import json
from functools import reduce
#Class name data file's location and name
CS_FILE="./.cs"
#I know that using two-dimension is better than this way,but I just want to practice the function of 'zip'.
CLASS_SCHEDULE={}
CLASS_NAME=[[],[],[],[],[]]
WEEK_NAME=['Monday','Tuesday','Wednesday','Thursday','Friday']
#Integrate date
def init():global CLASS_SCHEDULE#'zip' function will match location of each elements of list to integrate a object whose type is dict.CLASS_SCHEDULE=dict(zip(WEEK_NAME,CLASS_NAME))
def mainView():print(f"{'CLASS SCHEDULE BY JASON':#^50s}")print(f"{'':#^50s}")print(f"{'1.PREVIEW 2.INPUT 3.MODIFY 4.QUERY 5.SAVE 6.EXIT':#^50s}")print(f"{'':#^50s}")print(f"{'':#^50s}")
#Fill space
#return row count.
def addSpace(className):maxRow=len(max(*className,key=len))for x in className:while len(x)<maxRow:x.append(f"{'':^10s}")return maxRow
def preview():i=0row=addSpace(CLASS_NAME)print(f"{'':-^50}")#Day Titleprint(reduce(lambda x,y:f"{x:^10s}"+f"{y:^10s}",WEEK_NAME))#Using two-dimension will be better than this way.while i<row:for k,v in CLASS_SCHEDULE.items():print(f"{v[i]:^10s}",end='')print()i+=1print(f"{'':-^50}")
def inputClass():while True:#Actully,using 'if else' is better than this way,because the dict's 'get' function or 'in' keyword isn't to raise exception in default way.try:i=1tempClassLS=[]dayName=input(f"[ROOT/INPUT]{'>':#>2s}Please input the day name of that you want to add class:")if dayName not in CLASS_SCHEDULE:raise Exception('error by Jason.')while True:tempClassName=input(f"[ROOT/INPUT]{'>':#>2s}Please input the lesson-{i}'s name :")tempClassLS.append(tempClassName)userIntention=input(f"[ROOT/INPUT]{'>':#>2s}Continue to add on {dayName}?[y/n|yes/no]:")#Up to now,I haven't found the 'equal' function as Java in python environment.if userIntention=="yes" or userIntention=="y":i+=1continueelse:break#Don't assign value to this element directly,because it will cause address different between 'CLASS_SCHEDULE' and 'CLASS_NAME'CLASS_SCHEDULE[dayName].clear()CLASS_SCHEDULE[dayName].extend(tempClassLS)userIntention=input(f"[ROOT/INPUT]{'>':#>2s}Continue to add in other days?[y/n|yes/no]:")                                              #Up to now,I haven't found the 'equal' function as Java in python environment.if userIntention=="yes" or userIntention=="y":                          continueelse:breakexcept:print("Please input again!Such as 'Monday,Tuesday...'")continuefinally:pass
#To return a object address for modify function will be better than this way which only used by one operation,but...I don't want to restructure it. I am a lazy boy,emm...
def query():params=input(f"[ROOT/QUERY]{'>':#>2s}Please input the specific date[DayName,LessonNumber]:").split(',')if params[0] in CLASS_SCHEDULE:if int(params[1]) <= len(CLASS_SCHEDULE[params[0]]):print(f"Class Name:{CLASS_SCHEDULE[params[0]][int(params[1])-1]}" if not CLASS_SCHEDULE[params[0]][int(params[1])-1].strip()=='' else "You don't have classes at this point-in-time.")else:print("You don't have classes at this point-in-time.")else:print("Please input again!Such as 'Monday,Tuesday...'")def modify():params=input(f"[ROOT/MODIFY]{'>':#>2s}Please input the specific date[DayName,LessonNumber]:").split(',')CLASS_SCHEDULE[params[0]][int(params[1])-1]=input(f"[ROOT/MODIFY]{'>':#>2s}Please input class name:")def main():mainView()readCS()init()while True:try:#'match' is new keyword in the environment of Python 3.10,if raise error,please upgrade your python environment or modify the syntax by yourself.match input(f"[ROOT]{'>':#>2s}Please input instruction:"):case '1':preview()case '2':inputClass()case '3':modify()case '4':query()case '5':saveCS()case '6':print("Good Bye!")quit()case _:print("Please input again!")except Exception as e:print("Input error! Please input it again.")#print("Error:",e)continuefinally:pass#--FILE OPERATION---
def saveCS():#The keyword 'with' is similar to Csharp's keyword 'using' which will always  close IO streem automatically.with open(CS_FILE,"w",encoding="utf-8") as f:#Serilization:Transform python object to json's format string directly,and write to file subsequently.json.dump(CLASS_NAME,f)print("Already saved!")
def readCS():#bind variable scopeglobal CLASS_NAMEtry:with open(CS_FILE,'r',encoding="utf-8") as f:#Return python object directly.CLASS_NAME=json.load(f)except:passfinally:pass
#--------
#init()
#addSpace(CLASS_NAME)
#print(CLASS_NAME)
#preview()
#CLASS_NAME[0].append('hhhh')
#print(CLASS_SCHEDULE)
#saveCS()
main()

输出

#############CLASS SCHEDULE BY JASON##############
##################################################
#1.PREVIEW 2.INPUT 3.MODIFY 4.QUERY 5.SAVE 6.EXIT#
##################################################
##################################################
[ROOT]#>Please input instruction:2
[ROOT/INPUT]#>Please input the day name of that you want to add class:Monday
[ROOT/INPUT]#>Please input the lesson-1's name :Chinese
[ROOT/INPUT]#>Continue to add on Monday?[y/n|yes/no]:y
[ROOT/INPUT]#>Please input the lesson-2's name :Math
[ROOT/INPUT]#>Continue to add on Monday?[y/n|yes/no]:n
[ROOT/INPUT]#>Continue to add in other days?[y/n|yes/no]:y
[ROOT/INPUT]#>Please input the day name of that you want to add class:Friday
[ROOT/INPUT]#>Please input the lesson-1's name :Society
[ROOT/INPUT]#>Continue to add on Friday?[y/n|yes/no]:y
[ROOT/INPUT]#>Please input the lesson-2's name :English
[ROOT/INPUT]#>Continue to add on Friday?[y/n|yes/no]:y
[ROOT/INPUT]#>Please input the lesson-3's name :PPP
[ROOT/INPUT]#>Continue to add on Friday?[y/n|yes/no]:n
[ROOT/INPUT]#>Continue to add in other days?[y/n|yes/no]:n
[ROOT]#>Please input instruction:1
--------------------------------------------------Monday   Tuesday  Wednesday  Thursday   FridayChinese                                 SocietyMath                                  EnglishPPP
--------------------------------------------------
[ROOT]#>Please input instruction:4
[ROOT/QUERY]#>Please input the specific date[DayName,LessonNumber]:Friday,1
Class Name:Society
[ROOT]#>Please input instruction:A
Please input again!
[ROOT]#>Please input instruction:5
Already saved!
[ROOT]#>Please input instruction:6
Good Bye!

趣味Python-命令行的课程表相关推荐

  1. Python命令行可视化库

    我们通常都是在自己的电脑上跑程序,直接是可以可视化相应的结果.如果是在服务器上的话,使用终端,是不太方便查看结果.本文介绍4个可以在命令行中使用的Python库,分别是 Bashplotlib tqd ...

  2. python命令行参数解析OptionParser类用法实例

    python命令行参数解析OptionParser类用法实例 本文实例讲述了python命令行参数解析OptionParser类的用法,分享给大家供大家参考. 具体代码如下:     from opt ...

  3. 退出python命令行-退出python命令

    广告关闭 2017年12月,云+社区对外发布,从最开始的技术博客到现在拥有多个社区产品.未来,我们一起乘风破浪,创造无限可能. 在linux环境下退出python命令模式原创 2016年11月03日 ...

  4. python命令行大全-用什么库写 Python 命令行程序(示例代码详解)

    一.前言 在近半年的 Python 命令行旅程中,我们依次学习了 argparse . docopt . click 和 fire 库的特点和用法,逐步了解到 Python 命令行库的设计哲学与演变. ...

  5. 自己写的python软件可以在哪发布-如何发布一个Python命令行工具

    本文简介 上次写的一个终端里面斗鱼TV弹幕Python版本和Ruby版本,并且发布到PIP和RubyGems上面.在发布PIP包的时候,居然Google不到一篇可以非常好的讲解这个流程的文章.于是整理 ...

  6. python 基础命令-Python 命令行(CLI)基础库

    在 CLI 下写 UI 应用 前阵子看了一下自己去年写的 Python-视频转字符动画,感觉好糗..所以几乎把整篇文章重写了一遍.并使用 curses 库实现字符动画的播放. 但是感觉,curses ...

  7. Python命令行解析:sys.argv[]函数的简介、案例应用之详细攻略

    Python命令行解析:sys.argv[]函数的简介.案例应用之详细攻略 目录 sys.argv[]函数的简介 sys.argv[]函数的案例应用 1.基础测试 2.进阶用法 3.sys.argv[ ...

  8. Python命令行解析:IDE内点击Run运行代码直接得出结果、基于TF flags(或argparse、sys.argv)在Dos内命令行(一条命令)调用代码文件得出结果

    Python命令行解析:IDE内点击Run运行代码直接得出结果.基于TF flags(或argparse.sys.argv)在Dos内命令行(一条命令)调用代码文件得出结果 目录 命令行解析 T1.采 ...

  9. Python命令行参数解析模块getopt使用实例

    这篇文章主要介绍了Python命令行参数解析模块getopt使用实例,本文讲解了使用语法格式.短选项参数实例.长选项参数实例等内容,需要的朋友可以参考下 格式 getopt(args, options ...

  10. linux终端使用python3,3 个 Python 命令行工具 | Linux 中国

    原标题:3 个 Python 命令行工具 | Linux 中国 用 Click.Docopt 和 Fire 库写你自己的命令行应用. -- Jeff Triplett, Lacey Williams ...

最新文章

  1. 白话Elasticsearch16-深度探秘搜索技术之使用原生cross-fiedls技术解决搜索弊端
  2. 信息北航身份认证_信息北航丨北航第一服务平台,你值得关注!
  3. 汇编语言学习——第四章 第一个汇编程序
  4. (43)Verilog HDL 二分频设计
  5. linux磁盘fio压力测试,fio命令 – 对磁盘进行压力测试和验证
  6. realtek audio console无法连接rpc服务_笔记本网络连接图标不见了怎么办?
  7. [HZOI 2016]tree—增强版
  8. 蓝桥杯C语言基础题---01字串
  9. 环评师考各个科目有哪些备考的好方法?
  10. 【雕爷学编程】Arduino动手做(100)---MAX30102手腕心率
  11. 练习1000 scanf 用法
  12. 冬天跑步比夏天跑步减肥更快 冬天跑步减肥冷怎么办
  13. 移动流量转赠给好友_中国移动怎么才能转赠手机流量
  14. 仿微信朋友圈图片点击放大效果
  15. python 爬取懂车帝详情页“全部车型模块信息”
  16. linux服务器下搭建svn服务器仓库
  17. ildasm + ilasm + ilmerge 小试牛刀
  18. 2022最新影视小程序源码+支持JSON/卡密系统
  19. 钉钉企业开发的一些总结
  20. 【观察】不断打破手机行业创新边界,三个维度解读vivo NEX双屏版

热门文章

  1. 【b091z11】潜伏者
  2. 如何让庄家为自己抬轿子?
  3. MySQL用命令窗口打开
  4. 吃鸡最新界面怎么换服务器,2021《绝地求生》怎么切换服务器
  5. 【宝藏系列】如何取消LaTeX插入图片时边框出现虚线阴影?
  6. MX450和MX330的区别
  7. 商家开发微商城做会员制商城有哪些好处?
  8. 中晋最新消息2020年_热文:2020年退休人员养老金调整最新消息
  9. 为什么中位数(大多数时候)比平均值好
  10. MATLAB常用语句与函数