python基础之练习题(二)

  1. 九九乘法表
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
while实现九九乘法表
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
for实现九九乘法表

     2.成绩判断

输入成绩自动,判断成绩等级。
需求:
0-59 不及格
60-69 及格
70-7980-8990-99100 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同级
if 实现成绩判断

    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)
for 实现兔子计算

    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))
for 实现数字排列

 

posted @ 2017-04-09 17:03  luchuangao  阅读(423)  评论(0编辑  收藏  举报