社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

如何用Python制作带有turtle图形的线性图形计算器?

Nicholas Chan • 5 年前 • 1351 次点击  

为什么当我试图绘制图形时,这段代码不起作用?y截距似乎不起作用。

from turtle import *

m = float(input("What is the slope? "))

b = float(input("What is the y-intercept? "))

x= window_width()

y= window_height()

y= int(m*x + b)

pd()

goto(x , y)

pd()

goto(-x,-y)

pu()

goto(x/2,0)

pd()

goto(-x/2,0)


pu()

goto(0,2*y)

pd()

goto(0,-2*y)

update()

当我用y截取测试值时,它们会穿过原点,这意味着它不起作用。我在试着让y-截距工作。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/49878
 
1351 次点击  
文章 [ 1 ]  |  最新文章 5 年前
cdlane
Reply   •   1 楼
cdlane    6 年前

为什么当我试图绘制图形时,这段代码不起作用?

我认为有两个问题:1)你似乎做事情的顺序不对;2)你错误地认为如果y是f(x),那么f(-x)是-y,这不是真的:

from turtle import *

m = float(input("What is the slope? "))

b = float(input("What is the y-intercept? "))

x, y = window_width(), window_height()

# Draw Axes
penup()
goto(x / 2, 0)
pendown()
goto(-x / 2, 0)
penup()
goto(0, y / 2)
pendown()
goto(0, -y / 2)

# Plot function

y = int(m * x + b)

penup()
goto(x, y)

x = -x
y = int(m * x + b)

pendown()
goto(x, y)

done()

用法

> python3 test.py
What is the slope? 0.5
What is the y-intercept? 100
>

输出

enter image description here