macOS下运行turtle斐波那契螺旋代码报错:颜色字符串无效
TurtleGraphicsError: bad color string: blue I've got your back—let's figure out why this is failing on macOS and get it fixed. Your code works on Windows, but macOS is stricter about a couple of details that are tripping things up.
Your Original Code
import turtle Import math def fiboPlot(n): a = 0 b = 1 square_a = a square_b = b #Setting the colour of the plotting pen to blue x.pencolor("blue") # Drawing the first square x.forward(b * factor) x.left(90) x.forward(b * factor) x.left(90) x.forward(b * factor) x.left(90) x.forward(b * factor) # Proceeding in the Fibonacci Series temp = square_b square_b = square_b + square_a square_a = temp # Drawing the rest of the squares for i in range(1, n): x.backward(square_a * factor) x.right(90) x.forward(square_b * factor) x.left(90) x.forward(square_b * factor) x.left(90) x.forward(square_b * factor) # Proceeding in the Fibonacci Series temp = square_b square_b = square_b + square_a square_a = temp # Bringing the pen to starting point of the spiral plot x.penup() x.setposition(factor, 0) x.seth(0) x.pendown() # Setting the colour of the plotting pen to red x.pencolor("red") # Fibonacci Spiral Plot x.left(90) for i in range(n): print(b) fdwd = math.pi * b * factor / 2 fdwd /= 90 for j in range(90): x.forward(fdwd) x.left(1) temp = a a = b b = temp + b # Here 'factor' signifies the multiplicative # factor which expands or shrinks the scale # of the plot by a certain factor. factor = 1 # Taking Input for the number of # Iterations our Algorithm will run n = int(input('Enter the number of iterations (must be > 1): ')) # Plotting the Fibonacci Spiral Fractal # and printing the corresponding Fibonacci Number if n > 0: print("Fibonacci series for", n, "elements :") x = turtle.Turtle() x.speed(100) fiboPlot(n) turtle.done() else: print("Numb
The Error You're Seeing
File "/Users/als/PycharmProjects/spiral_of_fibonacci/main.py", line 81, in <module> fiboPlot(n) File "/Users/als/PycharmProjects/spiral_of_fibonacci/main.py", line 12, in fiboPlot x.pencolor("blue") File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/turtle.py", line 2253, in pencolor color = self._colorstr(args) File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/turtle.py", line 2697, in _colorstr return self.screen._colorstr(args) File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/turtle.py", line 1159, in _colorstr raise TurtleGraphicsError("bad color string: %s" % str(color)) turtle.TurtleGraphicsError: bad color string: blue
What's Wrong & How to Fix It
Let's go through the issues one by one:
HTML-escaped quotes are breaking color parsing
Look closely at your code—you're using"blue"instead of regular double quotes"blue". Your Windows VM might be ignoring this quirk, but macOS's turtle module reads the entire"blue"as the color name (not justblue), which isn't a valid color. Replace every instance of"with standard"in your code.Case-sensitive import mistake
Python is case-sensitive, soImport mathneeds to be lowercase:import math. Windows lets this slide, but macOS will throw an error once you fix the quote issue.Incomplete else clause
Your finalelseblock cuts off mid-print statement (print("Numb). Finish this to avoid a syntax error—something likeprint("Number must be greater than 1")will work.
Fully Corrected Code
Here's the fixed version that should run smoothly on macOS:
import turtle import math def fiboPlot(n): a = 0 b = 1 square_a = a square_b = b # Setting the colour of the plotting pen to blue x.pencolor("blue") # Drawing the first square x.forward(b * factor) x.left(90) x.forward(b * factor) x.left(90) x.forward(b * factor) x.left(90) x.forward(b * factor) # Proceeding in the Fibonacci Series temp = square_b square_b = square_b + square_a square_a = temp # Drawing the rest of the squares for i in range(1, n): x.backward(square_a * factor) x.right(90) x.forward(square_b * factor) x.left(90) x.forward(square_b * factor) x.left(90) x.forward(square_b * factor) # Proceeding in the Fibonacci Series temp = square_b square_b = square_b + square_a square_a = temp # Bringing the pen to starting point of the spiral plot x.penup() x.setposition(factor, 0) x.seth(0) x.pendown() # Setting the colour of the plotting pen to red x.pencolor("red") # Fibonacci Spiral Plot x.left(90) for i in range(n): print(b) fdwd = math.pi * b * factor / 2 fdwd /= 90 for j in range(90): x.forward(fdwd) x.left(1) temp = a a = b b = temp + b # Here 'factor' signifies the multiplicative # factor which expands or shrinks the scale # of the plot by a certain factor. factor = 1 # Taking Input for the number of # Iterations our Algorithm will run n = int(input('Enter the number of iterations (must be > 1): ')) # Plotting the Fibonacci Spiral Fractal # and printing the corresponding Fibonacci Number if n > 0: print("Fibonacci series for", n, "elements :") x = turtle.Turtle() x.speed(100) fiboPlot(n) turtle.done() else: print("Number must be greater than 1")
Extra Tip
If you still run into color issues, try using RGB tuples instead of named colors (e.g., x.pencolor((0, 0, 1)) for blue). This bypasses any macOS-specific color name parsing quirks that might still be lurking.
内容的提问来源于stack exchange,提问作者why_als

