《python程序语言设计》2018版第5章第52题利用turtle绘制sin函数


这道题是送分题。因为循环方式已经写到很清楚,大家照抄就可以了。

但是如果说光照抄可是会有问题。比如我们来演示一下。

python 复制代码
import turtle

turtle.penup()
turtle.goto(-175, 50 * math.sin((-175 / 100 * 2 * math.pi)))
turtle.pendown()
for x in range(-175, 176):
    turtle.goto(x, 50 * math.sin((x / 100 * 2 * math.pi)))

turtle.hideturtle
turtle.done()

这个问题我慢慢的思考明白。今天这个其实不是很难。

这句话即代表了起点也是整个线段的绘制。

python 复制代码
 turtle.goto(x, 50 * math.sin((x / 100 * 2 * math.pi)))

我是不是应该先把turle放到sin这个线断起始点再开始绘制呢?

所以我在循环外又加写了一句

python 复制代码
import turtle

turtle.speed(40)
turtle.penup()
turtle.goto(-175, 50 * math.sin((-175 / 100 * 2 * math.pi)))
turtle.pendown()
for x in range(-175, 176):
    turtle.goto(x, 50 * math.sin((x / 100 * 2 * math.pi)))

turtle.hideturtle
turtle.done()

##结果成功了

接下来丰富一下其他内容

python 复制代码
import turtle

turtle.speed(40)
turtle.penup()
turtle.goto(-175, 50 * math.sin((-175 / 100 * 2 * math.pi)))
turtle.pendown()
for x in range(-175, 176):
    turtle.goto(x, 50 * math.sin((x / 100 * 2 * math.pi)))

turtle.penup()
turtle.goto(-300, 0)
turtle.pendown()
turtle.goto(300, 0)
turtle.penup()
turtle.goto(0, 300)
turtle.pendown()
turtle.goto(0, -300)
turtle.penup()
turtle.goto(-100, -15)
turtle.write("-2\u03c0")
turtle.goto(100, -15)
turtle.write("2\u03c0")
turtle.hideturtle()
turtle.done()

今天吃火锅,早上练完瑜伽88.3到87.5 现在89.3.火锅粉丝肉片各种蔬菜,还有可乐。我的减肥呀

相关推荐
用户83562907805114 小时前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng815 小时前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi16 小时前
Chapter 2 - Python中的变量和简单的数据类型
python
JordanHaidee16 小时前
Python 中 `if x:` 到底在判断什么?
后端·python
ServBay16 小时前
10分钟彻底终结冗长代码,Python f-string 让你重获编程自由
后端·python
闲云一鹤17 小时前
Python 入门(二)- 使用 FastAPI 快速生成后端 API 接口
python·fastapi
Rockbean18 小时前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
曲幽19 小时前
FastAPI + Ollama 实战:搭一个能查天气的AI助手
python·ai·lora·torch·fastapi·web·model·ollama·weatherapi
用户606487671889619 小时前
国内开发者如何接入 Claude API?中转站方案实战指南(Python/Node.js 完整示例)
人工智能·python·api
只与明月听20 小时前
RAG深入学习之Chunk
前端·人工智能·python