功能要求
编写一个简单的交互控制台应用程序,根据系统当前时间计算出当前是星期几,根据星期几判断应该做什么。当星期一时,显示“今天星期一,客户提案”;当星期二时,显示“今天星期二,菲菲的生日”;当星期三时,显示“今天星期三,我要shopping”;当星期四时,显示“今天星期四,去健身房”;当星期五时,显示“今天星期五,向Boss汇报工作”;当星期六,显示“今天星期六,我要吃”;当星期天,显示“今天星期日,周末狂欢”。
实例代码
import time # 导入time模块
week = int(time.strftime("%w", time.localtime())) # 获取当前时间的日期
if week == 1: # 星期一
print("今天星期一,客户提案")
elif week == 2: # 星期二
print("今天星期二,菲菲的生日")
elif week == 3: # 星期三
print("今天星期三,我要shopping")
elif week == 4: # 星期四
print("今天星期四,去健身房")
elif week == 5: # 星期五
print("今天星期五,向Boss汇报工作")
elif week == 6: # 星期六
print("今天星期六,我要吃")
elif week == 0: # 星期天
print("今天星期日,周末狂欢")
else: # 其他数字(错误日期)
print("日期错误")
运行结果
知识说明
time.localtime()方法:用于将自纪元以来的时间(以秒为单位)转换为time.struct_time对象在当地时间。
time.strftime(format[, t]):format – 格式字符串,t – 可选的参数t是一个struct_time对象。返回以可读字符串表示的当地时间。其中“%w”星期(0-6),星期天为星期的开始。
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.