想用python做个输入年、月,显示当年当月日历的小程序,本人菜鸟,请教各位前辈。重重有赏
逻辑错误,就问你一个,若是闰年的2月29号呢,你进的是if month<3 这个逻辑
我的QQ:8198541 如脚本名为date.py
执行date.py 输出当日日历
执行date.py 2000 输出2000年1月日历
执行date.py 2000 5 输出2000年5月日历
#-*- coding: utf-8 -*-
"""
name: gcal.py
author: apache
function: print calendar for (year, month)
version: 1.0
uasge: python gcal.py [year] [month]
"""
import sys, time
MIN_YEAR = 0
MAX_YEAR = 100000
MIN_MONTH = 1
MAX_MONTH = 12
mdict = {"Jan" : 1, "Feb" : 2, "Mar" : 3, "Apr" : 4, "May" : 5, "Jun" : 6,"Jul" : 7, "Aug" : 8, "Sep" : 9, "Oct" : 10, "Nov" : 11, "Dec" : 12}
# get year and month
def getYearMonth():
"""
Get year and month from time moudle or user's input.
Return year, month
"""
argc = len(sys.argv)
if len(sys.argv) == 1:
temp = time.ctime().split()
return int(temp[4]), mdict[temp[1]]
else:
if not sys.argv[1].isdigit():
return -1, 0
else:
year = int(sys.argv[1])
if argc == 3:
if not sys.argv[2].isdigit():
return -2, 0
else:
return year, int(sys.argv[2])
else:
return year, 1
# is the year and month illegal? illegal return True and print information else return True
def isIllegal(year, month):
"""
Check the value of year and month.
If it's illegal return true and print illegal information else return false.
"""
info = {-1 : "The year should be an enterger!", -2 : "The month should be an enterger"}
if year == -1 or year == -2:
print info[year]
return True
else:
if year MAX_YEAR:
print "The year should between %d..%d" % (MIN_YEAR, MAX_YEAR)
return True
if month MAX_MONTH:
print "The month should between %s..%d" % (MIN_MONTH, MAX_MONTH)
return True
return False
# get the days of year, month
def getDays(year, month):
"""Return the days of year, month"""
if month in [1, 3, 5, 7, 8, 10, 12]:
return 31
if month in [4, 6, 9, 11]:
return 30
if month == 2:
if year % 4 == 0 or year % 400 == 0 and year % 100 != 0:
return 29
else:
return 28
# get the week of year, month, 1
def getWeek(year, month, day):
"""Return a number of week. 0 as Sunday."""
t=[0,3,2,5,0,3,5,1,4,6,2,4]
if month < 3:
year = year - 1
return (year + year / 4 - year / 100 + year / 400 + t[month - 1] + day) % 7
# print calendar
def printCalendar(year, month, days, week):
"""Print the calendar."""
print " %d %s" % (year, str(month).zfill(2))
print "Su Mo Tu We Th Fr Sa"
for i in range(week):
sys.stdout.write(" ")
count = week
temp = time.ctime().split()
cruyear = int(temp[4])
crumonth = int(mdict[temp[1]])
cruday = int(temp[2])
for i in range(1, days + 1):
if cruyear == year and crumonth == month:
if i + 1 == cruday:
sys.stdout.write(str(i).zfill(2).ljust(2))
elif i == cruday:
sys.stdout.write(("(" + str(i).zfill(2) + ")").ljust(3))
else:
sys.stdout.write(str(i).zfill(2).ljust(3))
else:
sys.stdout.write(str(i).zfill(2).ljust(3))
count = count + 1
if count % 7 == 0:
print
print
# Wrap all functions
def main():
"""Wrap all functions."""
year, month = getYearMonth()
if isIllegal(year, month):
sys.exit(1)
days = getDays(year, month)
week = getWeek(year, month, 1)
printCalendar(year, month, days, week)
if __name__ == "__main__":
main()
import calendar
def getYM():
''' 这是一个简单的年月输入方法 '''
year = raw_input('Input Year: ')
month = raw_input('Input Month: ')
return year, month
def saveGetYM():
''' 这是一个安全的年月输入方法 '''
while True:
try:
year_month = raw_input('Input year and month (year,mont): ')
year, month = year_month.split(',')
year, month = int(year), int(month)
if 1900<=year<=2200 and 1<=month<=12:
break
except:
continue
return year, month
year,month = saveGetYM()
c = calendar.Calendar(1)
print '-- %d --'%year
for w in c.monthdatescalendar(year,month)[:7:]:
print '|'.join([d.strftime('%m-%d') for d in w])
求大佬帮帮做一个python题目
day=day)week_day = week_dict[date.weekday()]# 输出带装饰的星期缩写(20个字符宽度)print(f"==={week_day.center(7)}==="程序先读入输入日期,然后使用 Python 的 datetime 库将其转换为星期的英文缩写。最后使用字符串的 center 方法,将缩写居中并加上两个等号装饰。运行程序,输入样例的...
Python能用来做什么?
Python 语言主要有以下用途:1) 简单:Python 是一种代表简单主义思想的语言。阅读一个良好的 Python 程序就感觉像是在读英语一样,尽管这个英语的要求非常严格。Python 的这种伪代码本质是其优点之一,使用户能够专注于解决问题而不是去搞明白语言本身。2) 易学:Python 有极其简单的语法,非常容易上手...
用Python写一个爬虫,做一个冷门行业的搜索引擎,能实现吗?
因为冷门行业,也存在的受众小、内容少(相对)的问题,完全可以自行架构网站,但是要想扩大你的影响力,那就需要下不少的功夫了,起码,本行业的人得认可你!当然,如果只是自用的话,那就简单了,哪怕你做个命令行版本的查询系统,都可以的,无非就是数据的整合,实时爬取等等!记得之前有想写个爬虫...
你都用Python 来做什么?
例如,通过 mod_wsgi 模块,Apache 可以运行用 Python 编写的 Web 程序。Python 定义了 WSGI 标准应用接口来协调 HTTP 服务器与基于 Python 的 Web 程序之间的通信。举个最直观的例子,全球最大的搜索引擎 Google,在其网络搜索系统中就广泛使用 Python 语言。另外,我们经常访问的集电影、读书、音乐于...
如何用Python编写一个素数环
此文主要目的,是向大家展示如何才能用python语言,来部署STARK算法。STARKs(可扩容的透明知识论证)是创建一种证明的技术,这项证明中f(x)=y,其中f可能要花很长的时间来进行计算,但是这个证明可以被很快验证。STARK是“双重扩容”:对于一个需要t步骤的计算,这会花费大约O(t * log(t))步骤才能完成这个证明,这可能...
python能做什么
python的用途:Python的优势有必要作为第一步去了解,Python作为面向对象的脚本语言,优势就是数据处理和挖掘,这也注定了它和AI、互联网技术的紧密联系。网络爬虫。顾名思义,从互联网上爬取信息的脚本,主要由urllib、requests等库编写,实用性很强,小编就曾写过爬取5w数据量的爬虫。在大数据风靡的时代...
新手python入门教程
新手python入门教程包括了解Python编程基础、Python函数及流程控制、利用Python做些事情、深入Python编程。1、了解Python编程基础:首先第一点,要能够看懂了解变量、基础语法、编程规范等,这些事能够上手编写Python代码的前提。其次第二点,对于数据结构,字符串、列表、字典等需要比较熟练运用。刚开始的这部分就...
python主要可以做什么
python主要可以做Web 和 Internet开发、科学计算和统计、桌面界面开发、软件开发、后端开发等领域的工作。Python是一种解释型脚本语言。Python可以应用于众多领域,如:数据分析、组件集成、网络服务、图像处理、数值计算和科学计算等众多领域。互联网公司广泛使用Python来做的事一般有:自动化运维、自动化测试、...
现在学好python能干什么?
python可以做什么 1 首先,最基本的功能就是借助python中自带的科学计算包Numpy、padas、matplotlib等,完成复杂的数据分析。2 网络爬虫,利用python可以从网络上爬取任何格式的数据,比如文本数据、音频、视频数据、图片等。## 标题 ##python爬取网络小说3 词云图,利用python对语料集分词处理后,输出个性化...
python能做什么
EVE- 网络游戏EVE大量使用Python进行开发 Reddit - 社交分享网站 Dropbox - 文件分享服务 Pylons - Web应用框架 TurboGears - 另一个Web应用快速开发框架 Fabric - 用于管理成百上千台Linux主机的程序库 Trac - 使用Python编写的BUG管理系统 Mailman - 使用Python编写的邮件列表软件 Mezzanine - 基于...
米超可谱: import datetime import calendardef getYM():''' 这是一个简单的年月输入方法 '''year = raw_input('Input Year: ')month = raw_input('Input Month: ')return year, monthdef saveGetYM():''' 这是一个安全的年月输入方法 '''while True:try:...
丰城市17157667482: 求Python 代码 从键盘输入年份和月份,在屏幕上输出该月的天数(要考虑闰年) - ?
米超可谱: 楼上的写的没什么问题,可是你的算法中有一个失误,那就是年份为100的倍数时,要能够整除400才能是29天,所以“”“case2:day=year%4==0?29:28;”“”这一句要改为"""case2:day=year%100==0?year%400==0?29:28:year%4==0?29:28;""
丰城市17157667482: Python:要求用 函数 实现: 从键盘输入年份和月份,然后计算返回该年该月共有多少天. - ?
米超可谱: # encoding: utf-8 # Python 3.6.0 def getdays():year=input("输入年份:")month=input("输入月份:")if year=="" or month==""or year.isdigit()==False or month.isdigit()==False:return "输入非法"m=[31,28,31,30,31,30,31,31,...
丰城市17157667482: 一个用python做的小日历 - ?
米超可谱: 如果要做成桌面应用的话,你还需要安装eric4 就有点类似vc++,可以选择控件并添加代码
丰城市17157667482: Python编程的问题,输入年月日,例如20100722,输出年,月,日,这个才学Python,语法用词搅不清楚啊. - ?
米超可谱: data="20120722" print data[0:4]+'年'+data[4:6]+'月'+data[-2:]+'日' 上面是最简单的输出方式. 还有格式化输出 data="20120722" print ' %s年%s月%s日' % (data[0:4],data[4:6],data[-2:])
丰城市17157667482: python编程,使用Tkinter中的文本框显示系统时间 - ?
米超可谱: Python编程中,用Tkinter中的文本框获取系统当前的时间并且显示,代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23importsys fromtkinter import* importtime deftick():globaltime1# 从运行程序的计算机上面获取当前的...
丰城市17157667482: python编程. 编写函数,输入年、月、日,计算该日是该年的第几天(注意判断该年是否为闰年).跪 - ?
米超可谱: 1 2 3 4!/usr/bin/python importdatetime dt =datetime.datetime(2012, 3, 16) print'%s'%dt.strftime('%j')简单写写,自己添加吧
丰城市17157667482: python中想输出a年b月c日,怎么编 - ?
米超可谱: import datetime,timenow = time.strftime("%Y年%m月%d日)python中时间日期格式化符号:%y 两位数的年份表示(00-99)%Y 四位数的年份表示(000-9999)%m 月份(01-12)%d 月内中的一天(0-31)%H 24小时制小时数(0-23) ...
丰城市17157667482: 在python中用if编写输入一个月份并计算有多少天 - ?
米超可谱: a = eval(input('请输入月份:'))while not(isinstance(a, int) and 0<a<13):a = eval(input('请输入正确的月份:'))da = [1,3,5,7,10,12] xiao = [4,6,8,9,11]if (a in da):print(a,'月有31天') elif (a in xiao):print(a,'月有30天') else:n=eval(input('请输入月所...
丰城市17157667482: 新手求助,如何用Python写日历让三个月在在 - ?
米超可谱: 直接使用python calender模块即可.1. calendar.calendar(year,w=2,l=1,c=6) 返回一个多行字符串格式的year年年历,3个月一行,间隔距离为c. 每日宽度间隔为w字符.每行长度为21* W+18+2* C.l是每星期行数. 2. calendar.firstweekday( )...