python 为什么书的例子也运行不了?

Python/PHP/Perl 开发与设计
回复
头像
newman0708
帖子: 188
注册时间: 2007-09-22 13:09
送出感谢: 0
接收感谢: 0
联系:

python 为什么书的例子也运行不了?

#1

帖子 newman0708 » 2008-01-09 11:15

为什么我下面的程序运行总是报错呀,
newman0708@newman0708-desktop:~/doc/python$ ./fig07_02.py
from: can't read /var/mail/myClock
./fig07_02.py: line 6: syntax error near unexpected token `('
./fig07_02.py: line 6: `time1=Time() #create object of class Time'
应该 怎么改呀,请各位指点一下,谢谢


#Fig.7.1: myClock.py
#Simple definition of class Time.

class Time:
"""Time abstract data type (ADT) definition"""
def __init__(self):
"""initialize hour,minute and second to zero"""

self.hour=0
self.minute=0
self.second=0

def printMilitary(self):
"""Prints object of class Time in military format """

print"%.2d:%.2d:%.2d" % \
(self.hour,self.minite,self.second),

def printStandard(self):
"""Prints object of class Time in standard format"""

standardTime=""

if self.hour==0 or self.hour==12:
standardTime+="12:"
else:
standardTime+="%d:" %(self.hour % 12)

standardTime+="%.2d:%.2d" % (self.minute,self.second)

if self.hour<12:
standardTime+=" AM"
else:
standardTime+=" PM"

print standardTime,





#Fig. 7.2 : fig07_02.py
#Creating and manpulating object of class Time.

from myClock import Time #import class definition from file

time1=Time() #create object of class Time

#access object's attributes
print "Time attributes of time1 are:"
print "time1.hour:", time1.hour
print "time1.minute:", time1.minute
print "time1.second:", time1.second

#access object's metnoas
print "\n Calling method printMilitary:",
time1.printMilitary()

print "\n Calling method printStandard:",
time1.printStandard()

#change value of object's attributes
print "\n\nChanging time1's hour attribute..."
time1.hour=25
print "Calling method printMilitary after alteration:",
time1.printMilitary()
附件
pythontest.tar.gz
(718 Bytes) 下载 84 次
xep007
帖子: 870
注册时间: 2006-01-03 20:44
送出感谢: 0
接收感谢: 6 次

#2

帖子 xep007 » 2008-01-09 18:37

myClock.py的第17行,好像把minute打成minite了。
头像
newman0708
帖子: 188
注册时间: 2007-09-22 13:09
送出感谢: 0
接收感谢: 0
联系:

#3

帖子 newman0708 » 2008-01-10 8:30

好了,问题解决了,谢谢 楼上的回答。

我们在运行python程序时,是不是一定要打
python fig07_02.py

我在里面加入 了 #!/usr/bin/python
把它chmod +x fig07_02.py
然后运行./fig07_02.py
好像不能运行的,
不知道是怎么回事?
xep007
帖子: 870
注册时间: 2006-01-03 20:44
送出感谢: 0
接收感谢: 6 次

#4

帖子 xep007 » 2008-01-10 11:32

newman0708 写了:好了,问题解决了,谢谢 楼上的回答。

我们在运行python程序时,是不是一定要打
python fig07_02.py

我在里面加入 了 #!/usr/bin/python
把它chmod +x fig07_02.py
然后运行./fig07_02.py
好像不能运行的,
不知道是怎么回事?


应该可以运行的。有什么错误提示呢?
头像
newman0708
帖子: 188
注册时间: 2007-09-22 13:09
送出感谢: 0
接收感谢: 0
联系:

#5

帖子 newman0708 » 2008-01-10 11:47

好了,现在 好了,

谢谢xep007 ,耐心回答
回复

回到 “Python/Php/Perl”