控制语句0523

7. 打印如下图案
0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4

解:

for x in range(5):
    for y in range(5):
        print(x, end="\t")
    print(x)

for m in range(1,10):
    for n in range(1,m+1):
        print("{0}*{1}={2}".format(m,n,(m*n)),end="\t")
    print()


8. 利用嵌套循环打印九九乘法表

解:

for m in range(1,10):
    for n in range(1,m+1):
        print("{0}*{1}={2}".format(m,n,(m*n)),end="\t")
    print()
1*1=1	
2*1=2	2*2=4	
3*1=3	3*2=6	3*3=9	
4*1=4	4*2=8	4*3=12	4*4=16	
5*1=5	5*2=10	5*3=15	5*4=20	5*5=25	
6*1=6	6*2=12	6*3=18	6*4=24	6*5=30	6*6=36	
7*1=7	7*2=14	7*3=21	7*4=28	7*5=35	7*6=42	7*7=49	
8*1=8	8*2=16	8*3=24	8*4=32	8*5=40	8*6=48	8*7=56	8*8=64	
9*1=9	9*2=18	9*3=27	9*4=36	9*5=45	9*6=54	9*7=63	9*8=72	9*9=81	


9. 用列表和字典存储下表信息,并打印出表中工资高于15000 的数据
姓名年龄薪资城市
高小一18 30000 北京
高小二19 20000 上海
高小五20 10000 深圳

解:

r1= dict(name="高小一",age=18,salary=30000,city="北京")
r2= dict(name="高小二",age=19,salary=20000,city="上海")
r3= dict(name="高小三",age=20,salary=10000,city="深圳")
tb=[r1,r2,r3]

for x in tb:
    if x.get("salary")>15000:
        print(x)

{'name': '高小一', 'age': 18, 'salary': 30000, 'city': '北京'}
{'name': '高小二', 'age': 19, 'salary': 20000, 'city': '上海'}

10. 要求输入员工的薪资,若薪资小于0 则重新输入。最后打印出录入员工的数量和薪资明
细,以及平均薪资

解:

emNum = 0
salarySum = 0
salarys = []
while True:
    s = input("请输入员工薪资(q输入/Q结束):")
    if s.upper()=="Q":
        print("录入完成,退出。")
        break
    if float(s) < 0:
        continue
    emNum += 1
    salarys.append(float(s))
    salarySum += float(s)

print("员工人数{0}".format(emNum))
print("员工薪资:", salarys)
print("员工平均薪资:{0}".format(salarySum / emNum))
请输入员工薪资(q输入/Q结束):1000
请输入员工薪资(q输入/Q结束):2000
请输入员工薪资(q输入/Q结束):3000
请输入员工薪资(q输入/Q结束):9000
请输入员工薪资(q输入/Q结束):12000
请输入员工薪资(q输入/Q结束):Q
录入完成,退出。
员工人数5
员工薪资: [1000.0, 2000.0, 3000.0, 9000.0, 12000.0]
员工平均薪资:5400.0


11. 员工一共4 人。录入这4 位员工的薪资。全部录入后,打印提示“您已经全部录入4
名员工的薪资”。最后,打印输出录入的薪资和平均薪资

解:

salarySum = 0
salarys = []
for x in range(4):
    s=input("请输入薪资(输入Q/q会导致中途结束)")
    if s.upper()=="Q":
        print("录入完成,将退出。")
        break
    if float(s)<0:
        continue
    salarys.append(float(s))
    salarySum+=float(s)
else:
    print("您已经全部录入4名员工的薪资。")
print("录入薪资",salarys)
print("员工平均薪资{0}".format(salarySum/4))
请输入薪资(输入Q/q会导致中途结束)10000
请输入薪资(输入Q/q会导致中途结束)18000
请输入薪资(输入Q/q会导致中途结束)30000
请输入薪资(输入Q/q会导致中途结束)Q
录入完成,将退出。
录入薪资 [10000.0, 18000.0, 30000.0]
员工平均薪资14500.0


12. 使用海龟绘图,绘制同心圆:

解:


import turtle

t=turtle.Pen()
my_color=("green","blue","purple","brown")
t.width(5)
t.speed(0.5)
for i in range(10):
    t.penup()
    t.goto(0,-i*10)
    t.pendown()
    t.color(my_color[i%len(my_color)])
    t.circle(10+i*10)
turtle.done()


13. 使用海龟绘图,绘制18*18 的棋盘:

import turtle
t=turtle.Pen()
t.width(1)
t.speed(6)
t.color("black")
for x in range(19):
    t.penup()
    t.goto(0,-x*10)
    t.pendown()
    t.goto(180,-x*10)
turtle.right(90)
for y in range(19):
    t.penup()
    t.goto(y*10,0)
    t.pendown()
    t.goto(y*10,-180)
turtle.done()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值