(1)编写写入文件函数,使用Python新建文件,在其中第一行写入3,4,5三个数,保存文件。 (2)编写读取文件函数,从文件中读取边长信息,并绘制直角三角形。 (3)编写主程序进行测试。
时间: 2024-01-21 08:18:16 浏览: 22
(1)写入文件函数:
```python
def write_to_file(file_path):
with open(file_path, 'w') as f:
f.write('3,4,5')
```
(2)读取文件函数:
```python
import turtle
def read_from_file(file_path):
with open(file_path, 'r') as f:
sides = f.readline().strip().split(',')
a, b, c = int(sides[0]), int(sides[1]), int(sides[2])
angle_c = turtle.degrees(turtle.atan(a/b))
turtle.forward(b*10) # 绘制底边
turtle.left(90)
turtle.forward(a*10) # 绘制垂线
turtle.right(180-angle_c)
turtle.forward(c*10) # 绘制斜边
```
(3)主程序:
```python
if __name__ == '__main__':
file_path = './triangle.txt'
write_to_file(file_path)
turtle.speed(1)
read_from_file(file_path)
turtle.done()
```
注:这里使用了turtle库进行绘图,需要在终端或命令行中安装此库。