Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在海龟身上画一个函数_Python_Turtle Graphics - Fatal编程技术网

Python 在海龟身上画一个函数

Python 在海龟身上画一个函数,python,turtle-graphics,Python,Turtle Graphics,我需要在turtle上绘制一个函数f1(x)。我需要从x=-7开始,然后前进.01直到到达x=-3。我还需要使用比例因子25 我在主函数中创建了一个for循环。当我转到坐标(I,I)并前进.01,然后画一条对角线,但当我用第二条I代替f1(I)时,什么也没有发生 def f1(x): return 2 * sqrt((-abs(abs(x)-1)) * abs(3 - abs(x))/((abs(x)-1)* (3-abs(x)))) * \(1 + abs(abs(x)-3)/(ab

我需要在turtle上绘制一个函数
f1(x)
。我需要从
x=-7
开始,然后前进.01直到到达
x=-3
。我还需要使用比例因子
25

我在主函数中创建了一个for循环。当我转到坐标
(I,I)
并前进
.01
,然后画一条对角线,但当我用第二条
I
代替
f1(I)
时,什么也没有发生

def f1(x):
    return 2 * sqrt((-abs(abs(x)-1)) * abs(3 - abs(x))/((abs(x)-1)*  (3-abs(x)))) * \(1 + abs(abs(x)-3)/(abs(x)-3))*sqrt(1-(x/7)**2)+(5+0.97* (abs(x-0.5)+abs(x+0.5))-\3*(abs(x-0.75)+abs(x+0.75)))*(1+abs(1- abs(x))/(1-abs(x)))
def main():
    wn=turtle.Screen()
    wn.bgcolor("white")
    wn.title("plotting")
    draw=turtle.Turtle()
    draw.fillcolor("black")
    draw.speed(10)
    draw.penup()
    scale=25
    for i in range(-700,-300,1):
        draw.goto(((i/100)*scale),((f1(i)/100)*scale))
        draw.pendown()

我不太确定应该画什么,因为这是我们应该知道的,但是现在没有画任何东西,当我运行它时,所有发生的事情就是打开turtle窗口。

我发现强制代码适应
范围()
可能会导致更多的问题,一个简单的
,而
循环有时是可行的:

from math import sqrt
from turtle import Screen, Turtle

SCALE = 25

def f1(x):  # long line broken up based on where parentheses balance
    return 2 * \
        sqrt((-abs(abs(x) - 1)) * abs(3 - abs(x)) / ((abs(x) - 1) * (3 - abs(x)))) * (1 + abs(abs(x) - 3) / (abs(x) - 3)) * \
        sqrt(1 - (x / 7) ** 2) + \
        (5 + 0.97 * (abs(x - 0.5) + abs(x + 0.5)) - 3 * (abs(x - 0.75) + abs(x + 0.75))) * \
        (1 + abs(1 - abs(x)) / (1 - abs(x)))

wn = Screen()
wn.title("plotting")

draw = Turtle()
draw.speed('fastest')
draw.penup()

i = -7.0

while i < -3.0:
    draw.goto(i * SCALE, f1(i) * SCALE)
    draw.pendown()

    i += 1.0 / 100

wn.exitonclick()
从数学导入sqrt
从海龟导入屏幕,海龟
比例=25
def f1(x):#根据括号的位置将长线拆分
返回2*\
sqrt((-abs(abs(x)-1))*abs(3-abs(x))/((abs(x)-1)*(3-abs(x)))*(1+abs(abs(x)-3)/(abs(x)-3))*\
sqrt(1-(x/7)**2+\
(5+0.97*(abs(x-0.5)+abs(x+0.5))-3*(abs(x-0.75)+abs(x+0.75))*\
(1+abs(1-abs(x))/(1-abs(x)))
wn=屏幕()
wn.标题(“绘图”)
draw=海龟()
牵引速度(“最快”)
draw.penup()
i=-7.0
而我<-3.0:
绘制.转到(i*比例,f1(i)*比例)
draw.pendown()
i+=1.0/100
wn.exitonclick()

在Python3或Python2下,似乎对我来说也是如此。

我认为问题在于您试图计算负数的平方根。更正代码:

import turtle
from math import sqrt

def f1(x):
    return 2 * sqrt(abs(abs(x)-1)) * abs(3 - abs(x))/((abs(x)-1)*  (3-abs(x))) * (1 + abs(abs(x)-3)/(abs(x)-3))*sqrt(abs(1-(x/7)**2))+(5+0.97* (abs(x-0.5)+abs(x+0.5))-3*(abs(x-0.75)+abs(x+0.75)))*(1+abs(1- abs(x))/(1-abs(x)))
def main():
    wn=turtle.Screen()
    wn.bgcolor("white")
    wn.title("plotting")
    draw=turtle.Turtle()
    draw.fillcolor("black")
    draw.speed(10)
    draw.penup()
    scale=25
    for i in range(-700,-300):
        draw.goto(i*(scale/100),(f1(i)*(scale/100)))
        draw.pendown()
main()
您想要的是
f1(i/100)
,而不是
f1(i)/100
。另外,如果您碰巧在Python2.x上运行此函数,请将
100
s和
100.0
都更改为
100.0
,这样您就不会进行整数除法。