实验2-1 绘制蟒蛇
import turtle
import random
turtle.setup(650,350,200,200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("purple")
turtle.seth(-40)
for i in range(4):
turtle.circle(40,80)
turtle.circle(-40,80)
turtle.circle(40,80/2)
turtle.fd(40)
turtle.circle(16,180)
turtle.fd(40*2/3)
turtle.done()
实验2-2 实例2的修改。改造实例代码2.1,绘制一条彩色蟒蛇,即在绘制python蟒蛇的每个小段时,画笔的绘制颜色会发生变化。
import turtle
import random
turtle.setup(650,350,200,200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.colormode(255)
turtle.seth(-40)
for i in range(4):
turtle.pencolor(random.randint(0,255),random.randint(0,255),random.randint(0,255))
turtle.circle(40,80)
turtle.circle(-40,80)
turtle.circle(40,80/2)
turtle.fd(40)
turtle.circle(16,180)
turtle.fd(40*2/3)
turtle.done()
实验2-3 叠加等边三角形的绘制,使用turtle库中的相关函数绘制一个叠加等边三角形,如图所示。(注意绘制的起点、终点和三角形大小)
from turtle import *
setup(650,350,200,200)
penup()
fd(-250)
pendown()
pensize(2)
pencolor("red")
seth(60)
fd(50)
seth(-60)
fd(50)
seth(-180)
fd(50)
seth(-120)
fd(50)
seth(0)
fd(100)
seth(120)
fd(50)
seth(-120)
fd(50)
seth(120)
fd(50)
done()
实验2-4 把绘制一个三角形的功能,编写为一个函数,通过调用完成叠加三角形的功能。
from turtle import *
def drawtriangle(angle,len):
seth(angle)
fd(len)
seth(-1*angle)
fd(len)
seth(-3*angle)
fd(len)
setup(650,350,200,200)
penup()
fd(-250)
pendown()
pensize(2)
pencolor("red")
drawtriangle(60,50)
penup()
seth(-120)
fd(50)
pendown()
drawtriangle(60,50)
penup()
seth(0)
fd(50)
pendown()
drawtriangle(60,50)
done()
实验3.1 编写程序,分别计算15°、30°、45°、60°、75°、90°对应的弧度值。
import math
for i in [0,15,30,45,60,75,90]:
print(math.radians(i))
实验3.2 一年365天,初始水平值为1.0,每工作一天水平增加N,不工作时水平不下降,一周连续工作5天,请编写程序运算结果并填写下表:
import math
workday=365//7*5+365%7
result=0
for i in range(11):
result=pow(1+0.001*(i+1),workday)
print(result)
实验3.3 设计一个替换加密算法,替换方案如下:
如:明文为:study hard and make progress every day
密文为:hgfwb sziw zmw nzpv kiltivhh vevib wzb
plaincode=input("请输入明文:")
str=""
for p in plaincode:
if ord("a")<=ord(p)<=ord("z"):
str=str+chr(ord("a")+ord("z")-ord(p))
else:
str=str+p
print(str)
实验3.4 大圆距离
假设(x1,y1)和(x2,y2)为两点的经度和维度,两点之间的大圆距离可以用如下公式计算:
D=radius*arccos(sin(x1)*sin(x2)+cos(x1)*cos(x2)*cos(y1-y2))
编写程序,提示用户输入两点的经度和维度(负值表示东经或北纬),显示两点的球面距离,地球半径radius=6371.01km,
例如:39.55,-116.25 41.5,87.37
The distance between the two points is:10691.79183231593km
from math import *
radius=6371
x1=eval(input())
y1=eval(input())
x2=eval(input())
y2=eval(input())
d=radius*acos(sin(x1)*sin(x2)+cos(x1)*cos(x2)*cos(y1-y2))
print("The distance between the two points is:{}KM".format(d))
year=eval(input("输入年份"))
d=year%12
if d==0:
print("猴")
elif d==1:
print("鸡")
elif d==2:
print("狗")
elif d==3:
print("猪")
elif d==4:
print("鼠")
elif d==5:
print("牛")
elif d==6:
print("虎")
elif d==7:
print("兔")
elif d==8:
print("龙")
elif d==9:
print("蛇")
elif d==10:
print("马")
elif d==11:
print("羊")
实验4.2 克拉姆法则求解二元一次方程组:
输入a,b,c,d,e,f计算x和y的值,如果ad-bc等于零,则输出“无解”。
a=eval(input("a:"))
b=eval(input("b:"))
c=eval(input("c:"))
d=eval(input("d:"))
e=eval(input("e:"))
f=eval(input("f:"))
if a*d-b*c==0:
print("无解!")
else:
x=(e*d-b*f)/(a*d-b*c)
y = (a * f - e * c) / (a * d - b * c)
print("x={},y={}".format(x,y))
实验4.3 猜数游戏
生成一个1到100的随机整数number,利用循环完成猜数游戏:输入一个整数guess,如果guess大于number,提示“大了”;如果guess小于number,提示“小了”;如果guess等于number,输出number,并退出循环。
from random import *
number=randint(0,100)
count=0
while True:
guess=eval(input("你猜是多少?"))
count+=1
if guess>number:
print("大了!")
if guess<number:
print("小了!")
if guess==number:
break
print("该数据为:",number)
print("共猜了{}次".format(count))
实验4.4 最大数和次大数
编程输入学生人数和每个学生的分数,利用循环找出最大数和次大数,并输出。
stu_num=eval(input("输入学生数:"))
max1=-1
max2=-1
for i in range(stu_num):
x=eval(input("输入成绩:"))
if x>max1:
max2=max1
max1=x
elif max2<x<max2:
max2=x
print("max number is:",max1)
print("second number is:",max2)
实验5.1 将十进制数转换为十六进制,包括如下函数:
• decimal2hex(decimalvalue)将十进制decimalvalue转换为十六进制
• hex2char(haxvalue)将十六进制的数值转换为十六制编码(0-9,‘A’~’F’)
• main()函数输入十进制数、调用decimal2hex函数、输出转换后的十六进制数对应的字符串
def hex2char(hexvalue):
if 0<=hexvalue<=9:
return str(hexvalue)
elif hexvalue==10:
return 'A'
elif hexvalue==11:
return 'B'
elif hexvalue==12:
return 'C'
elif hexvalue==13:
return 'D'
elif hexvalue==14:
return 'E'
elif hexvalue==15:
return 'F'
def decimal2hex(decimalvalue):
str=""
while decimalvalue!=0:
str=hex2char(decimalvalue%16)+str
decimalvalue=decimalvalue//16
return str
def main():
num=eval(input("输入十进制数:"))
resultstr=decimal2hex(num)
print("result=",resultstr)
main()
实验5.2 编写一个程序,显示给定年月的日历,程序提示输入年月,然后显示该月的整个日历,显示结果如下:
def printmonth(year,month):
printmonthtitle(year,month)
printmonthbody(year,month)
print('\r\n***************************')
def printmonthtitle(year,month):
ls=['Sun','Mon','Tue','Wed','Thu','Fri','Sat']
print(' ',getmonthname(month),' ',year)
print('***************************')
print('Sun Mon Tue Wed Thu Fri Sat')
def printmonthbody(year,month):
startday=getstartday(year,month)
numberofdaysinmonth=getnumberofdaysinmonth(year,month)
i=0
for i in range(0,startday):
print(' ',end='')
for i in range(1,numberofdaysinmonth+1):
print(format(i,'^4d'),end='')
if(i+startday)%7==0:
print()
def getmonthname(month):
if month==1:
monthname="January"
elif month==2:
monthname="February"
elif month==3:
monthname="March"
elif month==4:
monthname="April"
elif month==5:
monthname="May"
elif month==6:
monthname="June"
elif month==7:
monthname="July"
elif month==8:
monthname="August"
elif month==9:
monthname="September"
elif month==10:
monthname="October"
elif month==11:
monthname="November"
else:
monthname="December"
return monthname
def getstartday(year,month):
START_DAY_FOR_JAN_1_1800=3
totalnumberofdays=gettotalnumberofdays(year,month)
return (totalnumberofdays+START_DAY_FOR_JAN_1_1800)%7
def gettotalnumberofdays(year,month):
total=0
for i in range(1800,year):
if isleapyear(i):
total=total+366
else:
total=total+365
for i in range(1,month):
total=total+getnumberofdaysinmonth(year,i)
return total
def getnumberofdaysinmonth(year,month):
if(month==1 or month==3 or month==5 or month==7 or month==8 or month==10 or month==12):
return 31
if (month==4 or month==6 or month==9 or month==11):
return 30
if month==2:
return 29 if isleapyear(year) else 28
return 0
def isleapyear(year):
return year%400==0 or (year%4==0 and year%100!=0)
def main():
year=eval(input("enter full year(e.g.,2018):"))
month=eval(input(("enter month as number between 1 and 12:")))
printmonth(year,month)
main()