簡體   English   中英

python打印語法錯誤

[英]python print syntax error

我正在嘗試在Xcode IDE中用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行print'%s%s'%(X,Y)^ SyntaxError:無效語法

任何人都可以替代該代碼行。 提前致謝。 我今天開始使用python。

在python 3中, print是一個函數-您必須用括號將參數括起來:

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

這是使用不同的Python版本的問題。
似乎這段代碼是為Python2.x編寫的,但是您正嘗試在Python3.x上運行它。

解決方案是使用2to3自動更改這些小的差異:

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

或用print(<string>)手動替換print <string>出現print(<string>)有關更多說明,請參見Print是一個函數 )。

或者只是安裝Python2.x並使用該Python版本運行代碼

python2 /Users/248239j/Desktop/hilbert/hilbertexe.py

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM