我一直在尝试用Python绘制分形壳,我必须输入大小和级别的数量。每次我尝试实现代码时都会出现错误。我已经尝试过研究如何在Python中绘制一个shell,看看我能修复什么,但我似乎找不到任何有用的东西。对此的任何帮助都会很棒。
这是我到目前为止的代码:
import math
import turtle
from turtle import*
turtle = turtle.Turtle()
t= turtle
t.speed(1000)
print("MENU")
def main():
print ("1. Polygon Fractal")
print("2. Fractal Shell")
print("3. Snowflake")
print("4. Fractal Tree")
print("5. Exit")
x=input("Please select a number")
if x=="1":
z=input("Please input the number of sides")
a=input("Please input length")
polygon(int(z),int(a))
if x=="2":
f=input("Please input a size")
j=input("Please input a level")
fractalshell(int(f),int(j))
if x=="3":
b=input("Please input a length")
snowflake(int(b))
if x=="4":
fractaltree()
if x=="5":
t.hideturtle()
done()
def polygon(n,l):
f = (n - 2) * 180/n
for i in range(n):
t.forward(l)
t.right(180 - f)
def fractalshell(size, level):
def curve(size):
t.right(45)
for curve in range(0.1 *(size)):
t.forward(0.1*(size))
t.right(15)
t.right(170)
for curve in range(0.12*(size)):
t.forward(0.1*(size))
t.left(15)
if (level > 0):
t.forward(size)
curve(size)
t.right(45)
fractalshell = (0.93 * (size), level - 1)'
以下是不断出现的错误:
Traceback (most recent call last):
File "/Users/isabelavaldes/Dropbox/Recursion.py", line 113, in <module>
main()
File "/Users/isabelavaldes/Dropbox/Recursion.py", line 27, in main
fractalshell(int(f),int(j))
File "/Users/isabelavaldes/Dropbox/Recursion.py", line 59, in
fractalshell
curve(size)
File "/Users/isabelavaldes/Dropbox/Recursion.py", line 50, in curve
for curve in range(0.1 *(size)):
TypeError: 'float' object cannot be interpreted as an integer
答案 0 :(得分:0)
你的问题似乎与Python语法比Fractal Shell更相关。例如,您不清楚预期如何工作:
fractalshell = (0.93 * (size), level - 1)'
其中fractalshell
是我们所处的函数的名称,这似乎是对它的递归调用,但不是。我将您的代码重新设计为可以运行的代码(以及更多MVCE):
from turtle import Turtle, Screen
def fractalshell(size, level):
def curve(size):
turtle.right(45)
for step in range(int(10 * size)):
turtle.forward(0.1 * step)
turtle.right(15)
turtle.right(170)
for step in range(int(12 * size)):
turtle.forward(0.8333 * step)
turtle.left(15)
if level > 0:
turtle.forward(size)
curve(size)
turtle.right(45)
fractalshell(0.93 * size, level - 1)
screen = Screen()
turtle = Turtle()
turtle.speed('fastest')
f = input("Please input a size: ")
j = input("Please input a level: ")
fractalshell(int(f), int(j))
turtle.hideturtle()
screen.mainloop()
它目前产生有趣的形状,但我不是他们想要的:
<强> INPUT 强>
Please input a size: 5
Please input a level: 3