2016-02-06 3 views
1

J'expérimente des courbes de Hilbert écrites en Python dans un IDE Xcode. La liste de code est:Erreur de syntaxe d'impression 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() 

Je reçois l'erreur suivante de Xcode: Fichier "/Users/248239j/Desktop/hilbert/hilbertexe.py", ligne 12 print '% s% s' % (X, Y) ^ SyntaxError: syntaxe invalide

Quelqu'un aurait-il une alternative à cette ligne de code? Merci d'avance. J'ai commencé à utiliser Python aujourd'hui.

+1

double possible de [Erreur de syntaxe sur l'impression avec Python 3] (http://stackoverflow.com/questions/826948/ syntaxe-erreur-sur-impression-avec-python-3) – tripleee

Répondre

2

C'est un problème avec les différentes versions de Python-.
On dirait que ce code a été écrit pour Python2.x mais vous essayez de l'exécuter avec Python3.x.

La solution consiste à utiliser 2to3 pour modifier ces petites différences automatiquement:

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

ou remplaçons manuellement les occurences de print <string> avec print(<string>) (voir Print is a function pour plus d'explications).

Ou tout simplement installer python2.X et exécuter le code avec Python-Version

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

merci beaucoup –

2

print, en python 3, est une fonction - vous devez entourer les arguments par des parenthèses:

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

merci beaucoup –