2005年10月12日 星期三 18:23
对下面的程序有些疑问:
1)sys.argv 如果是代表在文件后的年数吗?如果是,那么应该:
if len ( sys.argv ) <2 #什么意思?:
print "Usage: leap.py year, year, year..."
sys.exit ( 0 )
2) try :
y = string.atoi ( i )
指什么意思?
3) leap = "no"
if y % 400 == 0 :
leap = "yes"
elif y % 100 == 0 :
leap = "no"
elif y % 4 == 0 :
leap = "yes"
else :
leap = "no"
假设年份是 1904, leap应该为多少?
#!c:\python\python.exe
import sys
import string
if len ( sys.argv ) < 2 :
print "Usage: leap.py year, year, year..."
sys.exit ( 0 )
for i in sys.argv[ 1 : ] :
try :
y = string.atoi ( i )
except :
print i, "is not a year."
continue
leap = "no"
if y % 400 == 0 :
leap = "yes"
elif y % 100 == 0 :
leap = "no"
elif y % 4 == 0 :
leap = "yes"
else :
leap = "no"
print y, "leap:", leap, "in the Gregorian calendar"
if y % 4 == 0 :
leap = "yes"
else :
leap = "no"
print y, "leap:", leap, "in the Julian calendar"
print "Calculated leapness for", len ( sys.argv ) - 1, "years"
2005年10月12日 星期三 18:31
1. 代表你传过去的参数 len ( sys.argv ) 取得参数数量 2. 没查文档,看名字猜测是ascii转整形 3. %代表取余,答案应该知道了吧:) 在05-10-12,Shi Mu <samrobertsmith at gmail.com> 写道: > > 对下面的程序有些疑问: > 1)sys.argv 如果是代表在文件后的年数吗?如果是,那么应该: > if len ( sys.argv ) <2 #什么意思?: > print "Usage: leap.py year, year, year..." > sys.exit ( 0 ) > > 2) try : > y = string.atoi ( i ) > 指什么意思? > > 3) leap = "no" > if y % 400 == 0 : > leap = "yes" > elif y % 100 == 0 : > leap = "no" > elif y % 4 == 0 : > leap = "yes" > else : > leap = "no" > 假设年份是 1904, leap应该为多少? > > > #!c:\python\python.exe > import sys > import string > > if len ( sys.argv ) < 2 : > print "Usage: leap.py year, year, year..." > sys.exit ( 0 ) > > > for i in sys.argv[ 1 : ] : > try : > y = string.atoi ( i ) > except : > print i, "is not a year." > continue > > leap = "no" > > if y % 400 == 0 : > leap = "yes" > elif y % 100 == 0 : > leap = "no" > elif y % 4 == 0 : > leap = "yes" > else : > leap = "no" > > print y, "leap:", leap, "in the Gregorian calendar" > > if y % 4 == 0 : > leap = "yes" > else : > leap = "no" > print y, "leap:", leap, "in the Julian calendar" > > print "Calculated leapness for", len ( sys.argv ) - 1, "years" > > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > > > -- Blog:http://www.donews.net/changzheng -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20051012/928a9b5f/attachment.htm
2005年10月14日 星期五 05:25
不能理解TRY, EXCEPT的用法, 能否指点一下?
2005年10月14日 星期五 08:33
用来捕获异常,try后面的是可能会出现异常的代码,except后面加异常类型和变 量名称,如果,出现匹配的异常,就执行后面的代码。 这个可以查手册 Shi Mu wrote: > 不能理解TRY, EXCEPT的用法, > 能否指点一下? > > ------------------------------------------------------------------------ > > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese >
Zeuux © 2025
京ICP备05028076号