1、导入一个小乌龟模块turtle,控制turtle的爬行轨迹组成一副图案
import turtle
turtle.pencolor('red')
turtle.fillcolor('red')
turtle.begin_fill()
turtle.penup()
turtle.goto(-300,200)
turtle.pendown()
turtle.forward(660)
turtle.rt(90)
turtle.forward(440)
turtle.rt(90)
turtle.forward(660)
turtle.rt(90)
turtle.forward(440)
turtle.end_fill()
turtle.penup()
turtle.goto(-190,156)
turtle.pendown()
turtle.pencolor('yellow')
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.rt(162)
for _ in range(5):
turtle.forward(125)
turtle.rt(144)
turtle.end_fill()
turtle.penup()
turtle.goto(-80,150)
turtle.pendown()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.rt(162)
for _ in range(5):
turtle.forward(42)
turtle.rt(144)
turtle.end_fill()
turtle.penup()
turtle.goto(-36,100)
turtle.pendown()
turtle.fillcolor('yellow')
turtle.begin_fill()
for _ in range(5):
turtle.forward(42)
turtle.rt(144)
turtle.end_fill()
turtle.penup()
turtle.goto(-36,20)
turtle.pendown()
turtle.fillcolor('yellow')
turtle.begin_fill()
for _ in range(5):
turtle.forward(42)
turtle.rt(144)
turtle.end_fill()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.penup()
turtle.goto(-80,-30)
turtle.pendown()
for _ in range(5):
turtle.forward(42)
turtle.rt(144)
turtle.end_fill()
turtle.mainloop()
2、求圆的面积
radius=float(input('输入圆的半径'))
PI=3.14
print('圆的面积=%f'%(PI*radius**2))
print('圆的周长=%f'%(2*PI*radius))
说明:在python中语言层面中没有定义常量的语法,但是我们可以通过把变量名用大写来做一个隐含提示(隐喻),全大写的变量要当常量来看待在代码中不能修改它的值(比如上面程序的PI),符号常量总是优于字面常量(比如3.14如果直接写数字的话,用到3.14的地方很多的话,每次都要写3.14太麻烦,所以我们把3.14赋值给PI即:PI=3.14)。在这里我们求圆的面积需要用到圆周率pi,这里我们也可以通过import math(导入一个数学模块)从数学模块提取pi格式:math.pi
3、英制单位转换为公制单位
inch = float(input('请输入英寸:'))
cm = inch * 2.54
print('换算后得到%.2fcm' % cm)
print(str(inch) + '英寸' + '=' + str(cm) + '厘米')
4、任意输入一年判断年份是否为闰年
year = int(input('请输入年份:'))
condition1 = year % 4 == 0
condition2 = year % 100 != 0
condition3 = year % 400 ==0
is_leap = condition1 and condition2 or condition3
print(is_leap)
if is_leap:
print('%d年是闰年:' % year,is_leap)
else:
print('%d年不是闰年' % year)
5、华氏温度和摄氏温度的转换
fahrenheit_temp=float(input('请输入一个华氏温度'))
celsius_temp=(fahrenheit_temp-32)/1.8
print('输出摄氏温度=',celsius_temp,'℃')
celsius_temp=float(input('请输入一个摄氏温度'))
fahrenheit_temp=(celsius_temp*1.8)+32
print('输出华氏温度=',fahrenheit_temp,'℉')
6、求修建泳池过道和围墙的费用
from math import pi
radius = float(input('请输入泳池的半径:'))
aisle_area = pi * (radius + 3) ** 2 - pi * radius ** 2
fence_length = 2 * pi * (radius + 3)
aisle_charge = 25 * aisle_area
fence_charge = 35.5 * fence_length
print('修建过道的费用%.2f' % aisle_charge)
print('修建围墙的费用%.2f' % fence_charge)
7、已知三角形三边求三角形面积
import math
a=float(input('请输入第一个边长'))
b=float(input('请输入第二个边长'))
c=float(input('请输入第三个边长'))
p=(a+b+c)/2
if a+b>c and a+c>b and b+c>a:
# print('三角形的周长l=',a+b+c)
# print('三角形的面积=',math.sqrt(p*(p-a)*(p-b)*(p-c)))
print('三角形的周长=%f' % (a+b+c))
print('三角形的面积=%f' % (math.sqrt(p*(p-a)*(p-b)*(p-c))))
else:
print('这不能构成三角形')
8、求1到100数字之和,偶数之和,求3或者5的倍数数之和
my_num = 0
for num in range(101):
my_num += num
print(my_num)
even_num = 0
for _ in range(0,101,2):
even_num += num
print(even_num)
my_num = 0
for num in range(101)
if num % 3 == 0 or num % 5 == 0:
my_num += num
print(my_num)
9、计算机随机产生一个随机数,人来猜,机器来猜
from random import randint
com_num = randint(1,100)
while True:
my_num = int(input('猜的数字:'))
if my_num > com_num:
print('请小一点')
elif my_num < com_num:
print('请大一点')
else:
print('恭喜你猜对啦')
break
print(my_num)
from random import randint
my_num = int(input('我的数字='))
game_over = False
while not game_over:
com_answer = randint(1,100)
print(com_answer)
if com_answer < my_num:
print('请大一点')
elif com_answer > my_num:
print('请小一点')
else:
print('恭喜你猜正确了')
game_over = True
print('游戏结束')
改进后
from random import randint
my_num = int(input('请输入我的数字:'))
a = 1
b =100
# com_num = randint(a,b)
game_over = 0
while not game_over:
com_num = randint(a,b)
print(com_num)
if com_num > my_num:
print('请小一点')
b = com_num - 1
elif com_num < my_num:
print('请大一点')
a = com_num + 1
else:
print('恭喜猜正确了')
game_over = 1
print('游戏结束')
10、人机猜拳
from random import randint
my_asset = 1000
com_asset = 1000
# bet =int(input('请下注:'))
round = 1
while my_asset != 0 and com_asset != 0:
bet = int(input('请下注:'))
if bet <= my_asset and bet <= com_asset:
print('round %d' % round)
my_num = int(input('你出的是:'))
com_num = randint(1,3)
if my_num == 1 and com_num == 2:
print('电脑赢')
my_asset -= bet
com_asset += bet
print(my_asset)
print(com_asset)
round += 1
elif my_num == 1 and com_num == 3:
print('人赢')
my_asset += bet
com_asset -= bet
print(my_asset)
print(com_asset)
round += 1
elif my_num == 2 and com_num == 1:
print('人赢')
my_asset += bet
com_asset -= bet
print(my_asset)
print(com_asset)
round += 1
elif my_num == 2 and com_num == 3:
print('电脑赢')
my_asset -= bet
com_asset += bet
print(my_asset)
print(com_asset)
round += 1
elif my_num == 3 and com_num == 1:
print('电脑赢')
my_asset -= bet
com_asset += bet
print(my_asset)
print(com_asset)
round += 1
elif my_num == 3 and com_num == 2:
print('人赢')
my_asset += bet
com_asset -= bet
print(my_asset)
print(com_asset)
round += 1
else:
print('平局')
print(my_asset)
print(com_asset)
round += 1
else:
print('请重新下注:')
print('game over')
11、计算个人所得税
personal_wage = float(input('个人工资:'))
insurance = float(input('五险一金'))
diff = personal_wage - insurance - 3500
if diff <= 1500:
tax = 0
deduction = 0
elif diff <= 4500:
tax = 0.03
deduction = 105
elif diff <= 9000:
tax = 0.1
deduction = 555
elif diff <= 35000:
tax = 0.2
deduction = 1005
elif diff <= 55000:
tax = 0.25
deduction = 2755
elif diff <= 80000:
tax = 0.3
deduction = 5505
elif diff <= 83500:
tax = 0.35
deduction = 13505
else:
tax = 0.45
print('个人所得税=%.1f' % tax)