读书人

PYTHON 一个程序23行有错不明白为

发布时间: 2012-03-12 12:45:33 作者: rapoo

PYTHON 一个程序,23行有错,不明白为什么 敬请指教!

Python code
class Rectangle:    def __init__(self, width, height):        self.width = width        self.height = height    def paint(self, factory):        point = factory.getPoint()        corner = factory.getCorner()        corner.leftUp()        point.line(self.width - 2)        corner.rightUp()        print()        for i in range(self.height - 2):            point.line(self.width)            print()                corner.leftDown()        point.line(self.width - 2);        corner.rightDown();        print()        class Dot:    def line(self, width):        for i in range(width):            print("-",end="")class Sharp:    def leftUp(self):        print("#", end="")    def rightUp(self):        print("#", end="")    def leftDown(self):        print("#", end="")    def rightDown(self):        print("#", end="")class DotSharpFactory:    def getPoint(self):        return Dot()    def getCorner(self):            return Sharp()        rect = Rectangle(20, 10)rect.paint(DotSharpFactory())


[解决办法]
觉得python 3下应该不会错。我是用2.6的,print当函数用要先
from __future__ import print_function
[解决办法]
楼主用的是哪个版本的解释器, python3.1 还是Python2.x?
[解决办法]
估计就像楼上所说的, 你可能用的是2.x的解释器,
>> python tmp.py # 默认的就是2.6的解释器, 楼主可能就是这么用的了
>>File "tmp.py", line 23
>> print("-",end="")
^
>>SyntaxError: invalid syntax


>>python3 tmp.py
>>#------------------#
>>--------------------
>>--------------------
>>--------------------
>>--------------------
>>--------------------
>>--------------------
>>--------------------
>>--------------------
>>#------------------#


[解决办法]
你在命令行中输入python

不是就能看到版本了嘛。

C:\Documents and Settings\Administrator>python
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
[解决办法]
探讨
我的解释器应该是3。1的 但我机器里还有没有别的版本的我也不知道。我吧文件存成.py后缀,运行的时候,
用的是 python [filenam].py
那么,LS我怎么改正,能运行起来这个程序呢?多谢!!!

[解决办法]
顶!估计是用的是python3.0前的版本。

读书人网 >perl python

热点推荐