LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1564|回复: 3

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

[复制链接]
发表于 2008-1-9 11:31:35 | 显示全部楼层 |阅读模式
为什么我下面的程序运行总是报错呀,
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):
        """rints object of class Time in military format """

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

    def printStandard(self):
        """rints 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()

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
发表于 2008-1-10 18:12:49 | 显示全部楼层
首先,你应该将代码用CODE标符括起来。还有就是缩进。你现在的代码格式完全违背了python的style guide。
回复 支持 反对

使用道具 举报

发表于 2008-1-12 22:48:16 | 显示全部楼层
看看先!再说
回复 支持 反对

使用道具 举报

发表于 2008-1-14 15:05:47 | 显示全部楼层
既然你是在linux下写script, 而且是通过 ./scriptname 的方式运行,那么还是养成添加sha-bang行的习惯吧。 我现在在windows下写python/perl都会添加该行的。  (:
或者直接调用python解释器也行:  $ python fig07_02.py

fix:
1. 在文件fig07_02.py的首行添加 #!/usr/bin/python   或者 #!/usr/bin/env python即可;否则其实是将python的代码让bash运行了,肯定会出错的
2. 文件myClock.py的第17行的minite更正为minute.

关于sha-bang行的注释(引用自abs-guide):
The  sha-bang  ( #!) at the head of a script tells your system that this file is a set of commands to be fed to the command interpreter indicated. The #! is actually a two-byte [1]  magic number, a special marker that designates a file type, or in this case an executable shell script (type man magic for more details on this fascinating topic). Immediately following the sha-bang is a path name. This is the path to the program that interprets the commands in the script, whether it be a shell, a programming language, or a utility. This command interpreter then executes the commands in the script, starting at the top (line following the sha-bang line), ignoring comments.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表