免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2762 | 回复: 8
打印 上一主题 下一主题

为什么我的“+=”py不识别呢? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-24 11:47 |只看该作者 |倒序浏览
我在学习语言的时候看见,有 Person.population += 1 ,但是当我运行是,py提示说syntax error--invalid syntax

论坛徽章:
0
2 [报告]
发表于 2008-03-25 13:11 |只看该作者
把源码整上来

论坛徽章:
0
3 [报告]
发表于 2008-03-26 09:42 |只看该作者
class Person:     
         '''Represents a person.'''     
         population = 0  
   
         def __init__(self, name):         '
                   ''Initializes the person's data.'''         
                   self.name = name         
                   print '(Initializing %s)' % self.name
                   Person.population += 1    

         def __del__(self):        
                   '''I am dying.'''
                    print '%s says bye.' % self.name         
                    Person.population -= 1

                    if Person.population == 0:            
                              print 'I am the last one.'         
                    else:            
                              print  'There  are  still  %d  people left.' % Person.population
       def sayHi(self):         
                   '''Greeting by the person.         
                      Really, that's all it does.'''         
                   print 'Hi, my name is %s.' % self.name
      def howMany(self):        
                    '''Prints the current population.'''        
                    if Person.population == 1:            
                              print 'I am the only person here.'         
                    else:            
                               print 'We have %d persons here.' % Person.population

swaroop = Person('Swaroop')
swaroop.sayHi()
swaroop.howMany()  

kalam = Person('Abdul Kalam')
kalam.sayHi()
kalam.howMany()  

swaroop.sayHi()
swaroop.howMany()

论坛徽章:
0
4 [报告]
发表于 2008-03-26 10:07 |只看该作者
输出如下  没报错
(Initializing Swaroop)
Hi, my name is Swaroop.
I am the only person here.
(Initializing Abdul Kalam)
Hi, my name is Abdul Kalam.
We have 2 persons here.
Hi, my name is Swaroop.
We have 2 persons here.

论坛徽章:
0
5 [报告]
发表于 2008-03-27 13:35 |只看该作者
没这功能

论坛徽章:
0
6 [报告]
发表于 2008-03-28 10:44 |只看该作者

回复 #4 luffy.deng 的帖子

那是不是我的PY有问题啊~!

论坛徽章:
0
7 [报告]
发表于 2008-03-28 13:16 |只看该作者
你的python的版本是多少
在早于2.0的版本上使用增量赋值符,会出现语法错误

论坛徽章:
0
8 [报告]
发表于 2008-03-28 20:43 |只看该作者
>>> x = 1
>>> x += x
>>> print x
2
>>>

论坛徽章:
0
9 [报告]
发表于 2008-04-16 14:00 |只看该作者
try:
============
change from
Person.population += 1
to
Person.population+=1
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP