python基础之练习题(二)
- 九九乘法表
i = 0 #while 九九乘法表 j = 0 while i < 9: i += 1 while j<9: j += 1 sum = i + j total="%s + %s = %s"% (i,j,sum) print(total,end=" ") if i == j: j = 0 print("\n") break
for i in range(1,10): # for 九九乘法表 for j in range(1,10): sum = i + j total = ("%d + %d = %d" % (i,j,sum)) print(total,end=" ") if i == j: print("") break
2.成绩判断
输入成绩自动,判断成绩等级。 需求: 0-59 不及格 60-69 及格 70-79 中 80-89 良 90-99 优 100 very good -1 抛出异常 101异常
while True: num = str(input("pls input num:")) if not num.isdigit() or int(num) > 100: print("请重新输入") continue else: num = int(num) if 0<= num <= 59: print("不及格") elif 60 <=num<= 69: print("及格") elif 70 <=num<= 79: print("中") elif 80<=num<= 89: print("良") elif 90 <=num<=99: print("优") elif num == 100: print("very good") break # break 也可以放到else同级
3.兔子计算
需求:12个月总共会有多少只兔子
rabbit = [1,1] for i in range(12): rabbit.append(rabbit[i] + rabbit[i+1]) print(i+1,"月有",rabbit[i]*2,"只小兔子") print(rabbit)
4.排列数字,不重复
需求:分别有四个数字 1 2 3 4 ,实现三个数字匹配在一起,并且每个数字不重复。
for i in range(1,5): for j in range(1,5): if (i == j): continue for k in range(1,5): if (j == k or i==k): continue else: print("%d%d%d"%(i,j,k))