《可爱的python》解答代码在python自带的IDLE中有语法错误!

5 views
Skip to first unread message

Michael He

unread,
Nov 6, 2009, 10:49:26 AM11/6/09
to pyth...@googlegroups.com
大作《可爱的python》刚收到,迫不及待开始学习python。
 
Python version: 3.1
Tk version: 8.5
IDLE version: 3.1
 
codes:
   1 #coding:utf-8
   2 '''cdays-5-exercise-1.py 判断今年是否是闰年
   3     @note: 使用了import, time模块, 逻辑分支, 字串格式化等
   4 '''
   5
   6 import time                             #导入time模块
   7 thisyear = time.localtime()[0]             #获取当前年份
   8 if thisyear % 400 == 0 or thisyear % 4 ==0 and thisyear % 100 <> 0: #判断闰年条件, 满足模400为0, 或者模4为0但模100不为0
   9     print 'this year %s is a leap year' % thisyear
  10 else:
  11     print 'this year %s is not a leap year' % thisyear
 
请回复一个初学者的疑问:
为什么输入
if thisyear % 400 == 0 or thisyear % 4 ==0 and thisyear % 100 <> 0:
这一行之后回车就出错呢?
谢谢!
 

@@

unread,
Nov 6, 2009, 10:59:14 AM11/6/09
to pyth...@googlegroups.com
In [12]: import time

In [13]: thisyear = time.localtime()[0]

In [14]: if thisyear % 400 == 0 or thisyear % 4 ==0 and thisyear % 100 <> 0:
....: print 'this year %s is a leap year' % thisyear
....: else:
....: print 'this year %s is not a leap year' % thisyear
....:
this year 2009 is not a leap year


没错阿

2009/11/6 Michael He <heyanpi...@gmail.com>:

Hu.Ziming

unread,
Nov 6, 2009, 11:15:57 AM11/6/09
to pyth...@googlegroups.com
> 请回复一个初学者的疑问:
> 为什么输入
> if thisyear % 400 == 0 or thisyear % 4 ==0 and thisyear % 100 <> 0:
> 这一行之后回车就出错呢?

报的什么错?

> 谢谢!
>
>
> >
>

Jiahua Huang

unread,
Nov 7, 2009, 4:53:11 AM11/7/09
to pyth...@googlegroups.com
妳好歹得将错误信息贴出来吧,

不然神也不知道妳在说些什么

2009/11/6 Michael He <heyanpi...@gmail.com>

Michael He

unread,
Nov 8, 2009, 11:42:36 AM11/8/09
to python-cn`CPyUG`华蟒用户组(中文Py用户组)
Thank you for your reply!
The problem only happened in Windows. <> can not use in Windows? I
replaced <> with !=, then the problem is solved.
Now I study Python in Ubuntu.
The <> can use now.
Thank you again. I love your book.

On 11月7日, 下午5时53分, Jiahua Huang <jhuangjia...@gmail.com> wrote:
> 妳好歹得将错误信息贴出来吧,
>
> 不然神也不知道妳在说些什么
>

> 2009/11/6 Michael He <heyanpingli...@gmail.com>

Hu.Ziming

unread,
Nov 8, 2009, 8:43:04 PM11/8/09
to pyth...@googlegroups.com
2009/11/9 Michael He <heyanpi...@gmail.com>:
> Thank you for your reply!
> The problem only happened in Windows. <> can not use in Windows? I
> replaced <> with !=, then the problem is solved.
> Now I study Python in Ubuntu.
> The <> can use now.
> Thank you again. I love your book.

windows下两个都不出错啊:
>>> thisyear = 2000
>>> if thisyear % 400 == 0 or thisyear % 4 ==0 and thisyear % 100 <> 0: print thisyear
...
2000
>>> if thisyear % 400 == 0 or thisyear % 4 ==0 and thisyear % 100 != 0: print thisyear
...
2000

Python 2.6.3 (r263rc1:75186, Oct 2 2009, 20:40:30) [MSC v.1500 32 bit
(Intel)] on win32

小人物小人物

unread,
Nov 8, 2009, 9:16:13 PM11/8/09
to pyth...@googlegroups.com
<>跟!=是一样的,我经常混着用

2009/11/9 Hu.Ziming <hzma...@gmail.com>

Leo Jay

unread,
Nov 8, 2009, 9:35:10 PM11/8/09
to pyth...@googlegroups.com
2009/11/9 Michael He <heyanpi...@gmail.com>:

> Thank you for your reply!
> The problem only happened in Windows. <> can not use in Windows? I
> replaced <> with !=, then the problem is solved.
> Now I study Python in Ubuntu.
> The <> can use now.
> Thank you again. I love your book.
>

你在windows下的版本是3.1, <>这个操作符在3.0版本里去掉了:
http://docs.python.org/3.1/whatsnew/3.0.html#removed-syntax

以后只能使用!=了

--
Best Regards,
Leo Jay

winy wind

unread,
Nov 9, 2009, 10:08:51 AM11/9/09
to pyth...@googlegroups.com


2009/11/9 Leo Jay <python...@gmail.com>
Reply all
Reply to author
Forward
0 new messages