分页: 1 / 1

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

发表于 : 2008-01-09 11:15
newman0708
为什么我下面的程序运行总是报错呀,
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()

发表于 : 2008-01-09 18:37
xep007
myClock.py的第17行,好像把minute打成minite了。

发表于 : 2008-01-10 8:30
newman0708
好了,问题解决了,谢谢 楼上的回答。

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

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

发表于 : 2008-01-10 11:32
xep007
newman0708 写了:好了,问题解决了,谢谢 楼上的回答。

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

我在里面加入 了 #!/usr/bin/python
把它chmod +x fig07_02.py
然后运行./fig07_02.py
好像不能运行的,
不知道是怎么回事?
应该可以运行的。有什么错误提示呢?

发表于 : 2008-01-10 11:47
newman0708
好了,现在 好了,

谢谢xep007 ,耐心回答