第二章习题

dollar = eval(input("请输入美元金额:"))
rmb = int(dollar * 6.)
print("对应的人民币金额是:", rmb)

while True:
choice = input("请选择兑换方向:1. 美元兑换人民币 2. 人民币兑换美元(输入1或2):")
if choice == '1':
dollar = float(input("请输入美元金额:"))
yuan = dollar * 6
print(f"{dollar}美元可兑换{yuan}人民币")
elif choice == '2':
yuan = float(input("请输入人民币金额:"))
dollar = yuan / 6
print(f"{yuan}人民币可兑换{dollar}美元")
else:
print("输入无效,请重新输入。")
another = input("是否继续进行兑换?(y/n)")
if another.lower()!= 'y':
break

while True:
choice = input("请选择转换方向:1. 千克转换为磅 2. 磅转换为千克(输入1或2):")
if choice == '1':
kg = float(input("请输入千克数:"))
pounds = kg * 2.2046
print(f"{kg}千克等于{pounds}磅")
elif choice == '2':
pounds = float(input("请输入磅数:"))
kg = pounds / 2.2046
print(f"{pounds}磅等于{kg}千克")
else:
print("输入无效,请重新输入。")
another = input("是否继续进行转换?(y/n)")
if another.lower()!= 'y':
break

import turtle

def draw_serpent():
colors = ["red", "blue", "green", "yellow", "purple"] # 定义颜色列表
turtle.pensize(40)
turtle.seth(-40)
color_index = 0
for i in range(4):
turtle.pencolor(colors[color_index % len(colors)]) # 设置画笔颜色
turtle.circle(40, 80)
color_index += 1
turtle.pencolor(colors[color_index % len(colors)])
turtle.circle(-40, 80)
color_index += 1
turtle.pencolor(colors[color_index % len(colors)])
turtle.circle(40, 80 / 2)
color_index += 1
turtle.fd(40)
turtle.pencolor(colors[color_index % len(colors)])
turtle.circle(16, 180)
color_index += 1
turtle.fd(40 * 2 / 3)

turtle.setup(650, 350, 200, 200)
draw_serpent()
turtle.done()

import turtle

创建turtle对象

my_turtle = turtle.Turtle()

设置画笔的初始方向为0度(朝上)

my_turtle.seth(0)

开始绘制

for _ in range(3):
my_turtle.fd(200) # 画笔前进200个单位长度,这里的长度可根据需求调整
my_turtle.seth(my_turtle.heading() + 120) # 将画笔方向设置为当前方向加上120度,因为等边三角形内角为60度,外角为120度,每次转弯角度是外角

import turtle

定义绘制等边三角形的函数

def draw_triangle(length):
for _ in range(3):
turtle.fd(length)
turtle.right(120)

抬起画笔,移动到起始位置

turtle.penup()
turtle.goto(-100, 0)
turtle.pendown()

绘制外部大等边三角形

draw_triangle(200)

抬起画笔,移动到内部三角形的起始位置

turtle.penup()
turtle.goto(0, 100)
turtle.pendown()

绘制内部等边三角形

draw_triangle(100)
turtle.done()

import turtle

创建一个turtle对象

t = turtle.Turtle()

绘制六角形

for _ in range(6):
t.forward(100) # 前进100个单位长度,可以根据需要调整边长
t.right(60) # 右转60度

turtle.done()

from turtle import *
left(90) # 将画笔向左旋转90度,使初始绘制方向向上
length = 5 # 初始线段长度设为5
speed = 20 # 设置绘图速度为20
for i in range(30): # 循环30次,绘制30组线段来构成螺旋线
fd(length) # 向前绘制指定长度的线段
left(90) # 向左旋转90度
fd(length) # 再向前绘制指定长度的线段
left(90) # 再次向左旋转90度
length += 5 # 每次循环后,线段长度增加5,这样就会形成螺旋效果

posted @ 2025-03-17 23:16  kkkk0515  阅读(36)  评论(0)    收藏  举报