Python论坛  - 讨论区

标题:[python-chinese] 帮助

2005年10月12日 星期三 18:23

Shi Mu samrobertsmith at gmail.com
Wed Oct 12 18:23:33 HKT 2005

对下面的程序有些疑问:
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"

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年10月12日 星期三 18:31

清风 paradise.qingfeng at gmail.com
Wed Oct 12 18:31:26 HKT 2005

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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年10月14日 星期五 05:25

Shi Mu samrobertsmith at gmail.com
Fri Oct 14 05:25:04 HKT 2005

不能理解TRY, EXCEPT的用法,
能否指点一下?

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年10月14日 星期五 08:33

ShiningRay tsowly at bentium.net
Fri Oct 14 08:33:44 HKT 2005

用来捕获异常,try后面的是可能会出现异常的代码,except后面加异常类型和变 
量名称,如果,出现匹配的异常,就执行后面的代码。
这个可以查手册

Shi Mu wrote:
> 不能理解TRY, EXCEPT的用法,
> 能否指点一下?
>   
> ------------------------------------------------------------------------
>
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
>   


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号