1.略
2.
# 输入一个学生的成绩,将其转化成简单描述:不及格(小于 60)、及格(60-79)、良好(80-89)、优秀(90-100)//优化:可重复添加成绩 grades = ['优秀','良好','及格','不及格'] #录入成绩库 while True: score = input('请输入学生成绩,输入q或Q退出') if score.upper() == 'Q': print('录入终止,退出!') break else: num = int(int(score)/10) if num < 6: num = 6 elif num == 10: num = 9 print('分数是{0},成绩是{1}'.format(score,grades[9-num])) #简便地调用成绩库,提高编程效率
3.
#已知点的坐标(x,y),判断其所在的象限 x = int(input('请输入横坐标:')) y = int(input('请输入纵坐标:')) name = '' if x == 0 and y == 0: name = '零点' elif x == 0: name = 'y轴' elif y == 0: name = 'x轴' elif x > 0 and y > 0: name = '第一象限' elif x > 0 and y < 0: name = '第二象限' elif x < 0 and y < 0: name = '第三象限' else: name = '第四象限' print(name)
4.
#输入一个分数。分数在 0-100 之间。90 以上是 A,80 以上是 B,70 以上是 C,60 以上是D,60一下是E[常规选择操作] score = int(input('请输入分数:')) grade = '' if score < 60: grade = 'E' elif score < 70: grade = 'D' elif score < 80: grade = 'C' elif score < 90: grade = 'B' else: grade = 'A' print(grade)
5.
#利用 while 循环,计算 1-100 之间数字的累加和;计算 1-100 之间偶数的累加和,计算1-100 之间奇数的累加和 num = 0 sum_all = 0 sum_even = 0 sum_odd = 0 while num <= 100: sum_all += num if num % 2 == 0: sum_even += num else: sum_odd += num num += 1 print('纍加和是{0},偶數和為{1},奇數和為{2}'.format(sum_all,sum_even,sum_odd))
6.
#利用 for 循环,计算 1-100 之间数字的累加和;计算 1-100 之间偶数的累加和,计算1-100 之间奇数的累加和 sum_all = 0 sum_even = 0 sum_odd = 0 for num in range(101): sum_all += num if num % 2 == 0: sum_even += num else: sum_odd += num print('纍加和是{0},偶數和是{1},奇數和是{2}'.format(sum_all,sum_even,sum_odd))
7.
#打印數組 for m in range(5): for n in range(5): print(m,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()
9.
#用列表和字典存储表格信息,并打印出表中工资高于 15000 的数据 r1 = dict(name = '高小一',age = 18,salary = 30000,city = 'Beijing') r2 = dict(name = '高小二',age = 19,salary = 20000,city = 'Shanghai') r3 = dict(name = '高小五',age = 20,salary = 10000,city = 'Shenzhen') tb = [r1,r2,r3] for x in tb: if x.get('salary') > 15000: print(x)
10.
#要求输入员工的薪资,若薪资小于 0 则重新输入。最后打印出录入员工的数量和薪资明细,以及平均薪资 salaries = [] sum = 0 num = 0 while True: salary = input('请输入薪资:(输入q或Q终止输入并查看已有信息)') if salary.upper() == 'Q': print('已终止录入,并查看已有信息:') break if float(salary) < 0: print('数据有误,请重新输入') continue num += 1 sum += float(salary) salaries.append(salary) print('员工数量为:{0},薪资明细:{1},平均薪资为{2:.2f}'.format(num,salaries,sum/num))
11.
#员工一共 4 人。录入这 4 位员工的薪资。全部录入后,打印提示“您已经全部录入 4名员工的薪资”。最后,打印输出录入的薪资和平均薪资 sum = 0 salaries = [] n = 0 for num in range(4): salary = input('请输入薪资:(输入q或Q退出)') if salary.upper() == 'Q': print('终止录入,退出!') break if float(salary) < 0: print('数据有误,跳过') continue n += 1 salaries.append(salary) sum += float(salary) else: print('已录入完成。录入薪资为{0},平均薪资为{1:.2f}'.format(salaries,sum/n))
12.
#使用海龟绘图,绘制同心圆 import turtle t = turtle.Pen() t.width(4) t.speed(0) colors = ['green','blue','yellow','red'] for i in range(50): t.penup() t.goto(0,-10*i) t.pendown() t.color(colors[i%4]) t.circle(10+10*i) turtle.done()
13.
#使用海龟绘图,绘制18*18棋盘 import turtle t = turtle.Pen() t.hideturtle() t.width(2) t.speed(0) for m in range(19): t.penup() t.goto(10*m,0) t.pendown() t.goto(10*m,180) for n in range(19): t.penup() t.goto(0,10*n) t.pendown() t.goto(180,10*n) turtle.done()