【发布时间】:2023-01-25 06:51:13
【问题描述】:
绘制斐波那契螺旋线的程序可以在 Windows 的虚拟机上运行,但在 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("Numb
以下是错误:
kEventMenuPopulate to menu ''
kEventMenuPopulate to menu '<Apple>'
kEventMenuPopulate to menu 'python'
kEventMenuPopulate to menu 'File'
kEventMenuPopulate to menu 'Edit'
kEventMenuPopulate to menu 'Window'
kEventMenuPopulate to menu 'Help'
kEventMenuPopulate to menu 'Help'
kEventMenuPopulate to menu 'Help'
kEventMenuPopulate to menu 'Help'
kEventMenuPopulate to menu ''
和这个
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
在 macOS 中
在 Windows 中
我看不到代码有问题,因为它在 Windows 上运行。
【问题讨论】:
-
这回答了你的问题了吗? Bad Color String Error in Python Using Turtle
-
我没有看到你的颜色代码有任何问题。问题可能出在 Mac 上的底层
Tk安装上。"red"等其他颜色字符串也会失败吗?解决方法可能是使用rgb值而不是颜色字符串,例如x.pencolor(0,0,255) -
顺便说一句——您发布的代码不能按原样运行。第二行有一个拼写错误,它也在代码底部的字符串文字中间突然中断。
标签: python macos turtle-graphics python-turtle