2016-02-06 114 views
1

我正在试验在Xcode IDE中用Python编写的Hilbert曲线。代码清单是:python打印语法错误

# python code to run the hilbert curve pattern 
# from http://www.fundza.com/algorithmic/space_filling/hilbert/basics/index.html 
import sys, math 

def hilbert(x0, y0, xi, xj, yi, yj, n): 
    if n <= 0: 
     X = x0 + (xi + yi)/2 
     Y = y0 + (xj + yj)/2 

     # Output the coordinates of the cv 
     print '%s %s 0' % (X, Y) 
    else: 
     hilbert(x0,    y0,    yi/2, yj/2, xi/2, xj/2, n - 1) 
     hilbert(x0 + xi/2,  y0 + xj/2,  xi/2, xj/2, yi/2, yj/2, n - 1) 
     hilbert(x0 + xi/2 + yi/2, y0 + xj/2 + yj/2, xi/2, xj/2, yi/2, yj/2, n - 1) 
     hilbert(x0 + xi/2 + yi, y0 + xj/2 + yj, -yi/2,-yj/2,-xi/2,-xj/2, n - 1) 

def main(): 
    args = sys.stdin.readline() 
    # Remain the loop until the renderer releases the helper... 
    while args: 
     arg = args.split() 
     # Get the inputs 
     pixels = float(arg[0]) 
     ctype = arg[1] 
     reps = int(arg[2]) 
     width = float(arg[3]) 

     # Calculate the number of curve cv's 
     cvs = int(math.pow(4, reps)) 

     # Begin the RenderMan curve statement 
     print 'Basis \"b-spline\" 1 \"b-spline\" 1' 
     print 'Curves \"%s\" [%s] \"nonperiodic\" \"P\" [' % (ctype, cvs) 

     # Create the curve 
     hilbert(0.0, 0.0, 1.0, 0.0, 0.0, 1.0, reps) 

     # End the curve statement 
     print '] \"constantwidth\" [%s]' % width 

     # Tell the renderer we have finished 
     sys.stdout.write('\377') 
     sys.stdout.flush() 

     # read the next set of inputs 
     args = sys.stdin.readline() 
if __name__ == "__main__": 
    main() 

我得到在Xcode以下错误: 文件 “/Users/248239j/Desktop/hilbert/hilbertexe.py”,第12行 打印 '%s%S' %( X,Y) ^ 语法错误:无效的语法

是否有人有该代码行的替代方案?提前致谢。我今天开始使用python。

+1

可能的复制(http://stackoverflow.com/questions/826948/语法错误的打印与蟒蛇-3) – tripleee

回答

2

这是与不同的Python的版本中的问题。
似乎这个代码是为Python2.x编写的,但是您正尝试使用Python3.x来运行它。

的解决方案是使用2to3的自动改变这些小的差异:

2to3 /Users/248239j/Desktop/hilbert/hilbertexe.py 

或手工print(<string>)取代的print <string>的出现次数(参见Print is a function更多的解释)。

或者只是安装Python2.x并运行该代码Python的版本

python2 /Users/248239j/Desktop/hilbert/hilbertexe.py 
的[语法错误的打印与Python 3]
+0

非常感谢你 –

2

print,在Python 3,是一个功能 - 你必须用括号括起来的参数:

print ('%s %s' % X, Y) 
+0

非常感谢你 –