from turtle import*
speed(10)
penup()
goto(-280,-250)
fillcolor("yellow")
begin_fill()
pendown()
for i in range(3):
forward(600)
left(120)
end_fill()
hideturtle()
exitonclick()
#############2——2
import turtle
turtle.fillcolor("yellow")
turtle.begin_fill()
for i in range(3):
turtle.seth(i*120)
turtle.fd(100)
turtle.end_fill()
turtle.hideturtle()
turtle.exitonclick()
##############2——3
import turtle
turtle.setup(600,400)
turtle.pensize(2)
turtle.penup()
turtle.goto(0,180)
turtle.left(135)
turtle.begin_fill()
turtle.color("pink","pink")
turtle.pendown()
turtle.circle(63.65,180)
turtle.goto(0,0)
turtle.goto(90,90)
turtle.setheading(45)
turtle.circle(63.65,180)
turtle.end_fill()
turtle.hideturtle()
turtle.exitonclick()
########4-2
for i in range(1, 10):
for j in range(i, 10):
print(f"{i}*{j}={i*j}", end="\t")
print()
print("\t"*2*i,end="")
# ############5-1
num_list=[23, 11, 12, 23, 9, 2, 1, 4]
new_list=set(num_list)
if len(num_list)==len(new_list):
print("无重复")
else:
print("有重复")
############5-3
run_ist=["0分钟","20分钟","40分钟","60分钟"]
swim_list=["0米","200米","400米","600米"]
calories_list=[i*200+j*100 for i in range(len(run_ist))for j in range(len(swim_list))]
print("卡路里列表:",calories_list)
print(f"运动计划中最多消耗{max(calories_list)}卡路里,最少消耗{min(calories_list)}卡路里")
# ############6-2
favorite_dict ={}
while True:
classify=input("请输入您喜欢的事物输入空则退出循环):")
if classify =="":
break
items=input("您喜欢的"+classify+"包括哪些(逗号隔开)?")
favorite_dict[classify]=items.split(",")
print(favorite_dict)
for classify,items in favorite_dict.items():
print(f"{classify}包括:",end="")
for item in items:
if items.index(item)==len(items)-1:
print(item)
else:
print(item,end=",")
# ############7-1
def scene_province(scenic_spot,province,abbr_province):
print(f"我最喜欢的景点是{scenic_spot}")
print(f"它位于{province}")
print(f"{province}的简称是{abbr_province}")
scene_dict={
"scenic_spot'":"塔里拉马干",
"province":"新疆",
"abbr_province'":"疆"
}
scene_province(**scene_dict)
########7-2
def product(*number):
sum = 1
for item in number:
sum *= item
return sum
result = product(1,2,3)
print(result)
##########8-2
class House:
number=0
def __init__(self,name,length,width):
self.name=name
self.length=length
self.width=width
House.number +=1
def cal_square(self):
square = self.length * self.width
return f"{self.name}的面积是{square}"
if __name__ =="__main__":
h1 = House("2620",20, 10)
print(h1.cal_square())
h2 = House("2626",10, 10)
print(h2.cal_square())
print("房子的个数是", House.number)
##########8-3
from datetime import datetime
class Medicine:
def __init__(self,name,price,pd,exp):
self.name = name
self.__price = price
self.__pd = datetime.strptime(pd, "%Y-%m-%d")
self.__exp = datetime.strptime(exp, "%Y-%m-%d")
def guarantee_period(self):
gp = self.__exp - self.__pd
return gp.days
def is_expire(self):
today = datetime.today()
until_now = (today - self.__pd).days
gp = self.guarantee_period()
if until_now > gp:
print("商品已经过期")
else:
print("商品没有过期")
if __name__ =="__main__":
m1 = Medicine("999感冒灵",20,"2023-1-1","2024-1-1")
print("保质期共有:", m1.guarantee_period())
m1.is_expire()
########9-2
class Student:
number = 0
def __init__(self, chinese, math, physics):
self.chinese = chinese
self.math = math
self.physics = physics
Student.number += 1
def __add__(self, other):
Student.number -=1
chinese = self.chinese + other.chinese
math = self.math + other.math
physics = self.physics + other.physics
return Student(chinese,math,physics)
def __str__(self):
return f"语文: {self.chinese},数学: {self.math},物理: {self.physics},总人数:{Student.number})"
s1 = Student(90, 100, 90)
s2 = Student(80, 80, 80)
s3 = Student(90, 100, 100)
sum = s1+s2+s3
print(sum)
print(f"语文平均分:{sum.chinese/sum.number:.2f},
数学平均分:{sum.math/sum.number:.2f},
物理平均分:{sum.physics/sum.number:.2f}")
Python练习题
最新推荐文章于 2025-05-10 15:19:48 发布