Q1有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
第一种解法:
m = 0
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):#嵌套循环
if (i!=j) and (i!=k) and(j!=k):#判断不重复
print(100*i+10*j+k)
m = m + 1
print("共有{}个符合条件的数".format(m))#输出个数
结果:
另一种方法:
nums = ['1','2','3','4']
a = []
for i in range(4):
for j in range(4):
for k in range(4):
if nums[i]!=nums[j] and nums[j]!=nums[k] and nums[i]!=nums[k]:
a.append(nums[i]+nums[j]+nums[k])
print("共有{}个符合条件的数".format(len(a)))
for z in range(0,len(a)):
print(a[z])
---------------------------------------------------------------------------------------------------------------------------------
Q2.企业发放的奖金根据利润提成。利润(I)低于或等于 10 万元时,奖金可提 10%;利润高于
10 万元,低于 20 万元时,低于10 万元的部分按10%提成,高于 10 万元的部分,可提成
7.5%;20 万到40 万之间时,高于 20 万元的部分,可提成 5%;40 万到60 万之间时高于40万元的部分,可提成3%;60 万到 100 万之间时,高于 60 万元的部分,可提成15%,高于100 万元时,超过 100 万元的部分按1%提成,从键盘输入当月利润I(整数),求应发放奖金总数?
def calculator(I):
while (I.isdigit()) == 0:
print("您输入的利润不是整数,请调整您的输入值类型") #判断是否为整数
break
else:
I = int(I)
if 0<= I <=10:
print("对应奖金为{}万元".format(I * 0.1))
elif 10 < I <= 20:
print("对应奖金为{}万元".format((10 * 0.1)+(I-10) * 0.075))
elif 20 < I <= 40:
print("对应奖金为{}万元".format((10 * 0.1)+(10 * 0.075)+(I-20) * 0.05))
elif 40 < I <= 60:
print("对应奖金为{}万元".format((10 * 0.1)+(10 * 0.075)+(20 * 0.05)+(I-40) * 0.03))
elif 60 < I <= 100:
print("对应奖金为{}万元".format((10 * 0.1)+(10 * 0.075)+(20 * 0.05)+(20 * 0.03)+(I-60) * 0.015))
else:
print("对应奖金为{}万元".format((10 * 0.1)+(10 * 0.075)+(20 * 0.05)+(20 * 0.03)+(40 * 0.015)+(I-100) * 0.01))
profit = input("请输入当月利润(单位:万元):") #提示用户输入
calculator(profit) #调用函数
注意:input()函数默认把输入的数据转换成字符串类型
可以这样:
a = eval(input("请输入数字:"))
另一种更完美的办法:
def calculator(I):
while not I.isdigit():
print("您输入的利润不是整数,请调整您的输入值类型") #判断是否为整数
I = input("请输入当月利润(单位:万元):") #提示用户重新输入
else:
I = int(I)
if 0 <= I <= 10:
bonus = I * 0.1
elif 10 < I <= 20:
bonus = 1 + (I - 10) * 0.075
elif 20 < I <= 40:
bonus = 1.75 + (I - 20) * 0.05
elif 40 < I <= 60:
bonus = 2.75 + (I - 40) * 0.03
elif 60 < I <= 100:
bonus = 3.35 + (I - 60) * 0.015
else:
bonus = 3.95 + (I - 100) * 0.01
print("对应奖金为{}万元".format(bonus))
profit = input("请输入当月利润(单位:万元):") #提示用户输入
calculator(profit) #调用函数
展示一种有语法错误且不够简洁的代码
def calculator(I):
while(I.isdigit()) == 0:
print("您输入的利润不是整数,请调整您的输入值类型") #判断是否为整数
else:
if 0<= int(I) <=10:
print("对应奖金为{}".format(int(I) * 10%))
elif 10 < int(I) <= 20:
print("对应奖金为{}".format((10 * 10%)+((int(I)-10) * 7.5%))
elif 20 < int(I) <= 40:
print("对应奖金为{}".format((10 * 10%)+(10 * 7.5%)+((int(I)-20) * 5%)))
elif 40 < int(I) <= 60:
print("对应奖金为{}".format((10 * 10%)+(10 * 7.5%)+(20 * 5%)+((int(I)-40) * 3%)))
elif 60 < int(I) <= 100:
print("对应奖金为{}".format((10 * 10%)+(10 * 7.5%)+(20 * 5%)+(20 * 3%)+((int(I)-60) * 1.5%)))
else:
print("对应奖金为{}".format((10 * 10%)+(10 * 7.5%)+(20 * 5%)+(20 * 3%)+(40 * 1.5%)+((int(I)-100) * 1%)))
break
profit = input("请输入当月利润(单位:万元):") #提示用户输入
def calculator(profit) #调用函数
缺点:1.最后一行,调用函数弄成了定义函数
2.int(I)应该类似前两种方法一样处理,否则会影响运行速度和简洁度
3.break没有搞清楚,break是用来跳出循环的,而不是在这里!
---------------------------------------------------------------------------------------------------------------------------------
利用math库将47°角转化为弧度制,并将结果赋给一个变量
import math as m
a = m.radians(47)
print(a)
---------------------------------------------------------------------------------------------------------------------------------
利用math库将pi/7的弧度值转换为角度值,并将结果赋给一个变量
import math as m
b = m.degrees(m.pi/7)
print(b)
---------------------------------------------------------------------------------------------------------------------------------
使用turtle库绘制一个六角形
import turtle as t
for i in range(6):
t.left(30)
t.fd(100)
t.left(120)
t.fd(100)
t.left(120)
t.fd(100)
t.left(120)
t.fd(100)
t.right(90)
t.seth(30)
t.done()
---------------------------------------------------------------------------------------------------------------------------------
使用turtle库绘制正方形螺旋线
import turtle as t
t.speed(100)
t.pencolor("purple")
def luoxuanline(times,length):
for i in range(times):
t.fd((i+1)*length)
t.left(90)
luoxuanline(152,5)
#注意在循环之中寻找规律,先直走再左转
---------------------------------------------------------------------------------------------------------------------------------利用turtle库和random库绘制一条随机颜色的蟒蛇
import turtle as t
import random as r
def randomcolor():
colorArr = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
color =""
for i in range(6):
color += colorArr[r.randint(0,14)] #设置随机颜色
t.pencolor('#'+color)
t.penup()
t.fd(-250)
t.pendown()
t.pensize(30)
t.seth(-40)
for i in range(4):
randomcolor()
t.circle(40,80)
t.circle(-40,80)
t.circle(40,40)
t.fd(40)
t.circle(16,180)
t.fd(40*2/3)
注意:1.随机生成颜色也可以用RGB
import turtle as t
t.pencolor('#FFFFFF')#通过十六进制字符串形式设置成白色
t.pencolor((160/255,32/255,240/255)) #通过RGB设置成紫色
2.受此启发,我们或许可以利用嵌套循环遍历生成RGB码。请看下面:
def randomcolorprint():
for i in range(255):
for j in range(255):
for k in range(255):
print("{}\255,{}\255,{}\255\t".format(i,j,k))
print("这样设置:t.pencolor(i/255,j/255,k/255)")#设置RGB"
randomcolorprint()
使用随机数生成RGB码,并用于绘制随机色蟒蛇:
import turtle as t
import random as r
def randomcolor():
a = r.randint(0,255)
b = r.randint(0,255)
c =r.randint(0,255)
t.pencolor(a/255,b/255,c/255) #将画笔颜色设置成随机色
t.penup()
t.fd(-250)
t.pendown()
t.pensize(30)
t.seth(-40)
for i in range(4):
randomcolor()
t.circle(40,80)
t.circle(-40,80)
t.circle(40,40)
t.fd(40)
t.circle(16,180)
t.fd(40*2/3)
最后:在代码末端可以使用turtle.done() -->防止已经打开的绘图窗口在不注意的情况下被关闭,也就是把作图图像可以保留下来,除非退出界面。
以及turtle.hideturtle() -->隐藏小箭头