第四章 课后练习

1、使用循环输出九九乘法表,输出如下结果:

for i in range(1,10):
    for j in range(1,i+1):
        print("%2d * %2d = %2d" % (j, i, i * j), end = " ")
    print()

2、使用循环输出等腰三角形

lines = int(input("输入要打印的行数:"))
for i in range(lines):
    for j in range(0, lines - i):
        print(end=" ")
    for k in range(2 * i + 1):
        print("*", end="")
    print()

3.

size = int(input("输入要打印的SIZE(奇数):"))
array = [[0] * size]
# 创建一个长度size * size的二维列表
for i in range(size - 1):
    array += [[0] * size]
row, col = 0, size // 2
for i in range(1, size * size + 1):
    array[row][col] = i
    if i % size == 0:
        row += 1
    elif row == 0:
        row = size - 1
        col += 1
    elif col == size - 1:
        row -= 1
        col = 0
    else:
        row -= 1
        col += 1
for i in range(size):
    for j in range(size):
        print('%02d' % array[i][j], end=" ")
    print()

4

lines = int(input("输入要打印的行数(奇数):"))
if lines % 2 == 0:
    print('请输入奇数')
    import sys
    sys.exit(0)
half_lines = lines // 2 + 1
# 打印上半
for i in range(half_lines):
    print(" " * (half_lines - i), end="")
    print("*" * (2 * i + 1))
# 打印下半
for i in range(half_lines - 1):
    print(" " * (i + 2), end="")
    print("*" * (lines - 2 - 2 * i))

5

lines = int(input("输入要打印的行数(奇数):"))
if lines % 2 == 0:
    print('请输入奇数')
    import sys
    sys.exit(0)
half_lines = lines // 2 + 1
# 打印上半
for i in range(half_lines):
    print(" " * (half_lines - i), end="")
    if i == 0:
        print("*")
    else:
        print("*", end="")
        print(" " * (2 * i - 1), end="")
        print("*")
# 打印下半
for i in range(half_lines - 1):
    print(" " * (i + 2), end="")
    if i == half_lines - 2:
        print("*")
    else:
        print("*", end="")
        print(" " * (lines - 4 - 2 * i), end="")
        print("*")

6

while(True):
    s = input("输入自己的成绩:")
    if s == 'exit':
        import sys
        sys.exit(0)
    score = float(s)
    if score >= 90:
        print('A')
    elif 90 > score >= 80:
        print('B')
    elif 80 > score >= 70:
        print('C')
    else:
        print('D')

7

start = 101
end = 200
for i in range(101, end + 1):
    is_prime = True
    for j in range(2 , int(i ** 0.5) + 1):
        if i % j == 0:
            is_prime = False
    if is_prime:
        print(i)

8

start = 101
end = 999
for i in range(start, end + 1):
    # 计算百位上的数
    bai = i // 100
    # 计算十位、个位上的数
    shi, ge = (i - bai * 100) // 10, i % 10
    # 判断是否为水仙花数
    if ge ** len(str(i)) + shi ** len(str(i)) + bai ** len(str(i)) == i:
        print(i)
print('-' * 50)

9

string = input("请输入一行内容: ")
char_num, digit_num, other_num = 0, 0, 0
for c in string:
    if c.isdigit(): digit_num += 1
    elif c.isalpha(): char_num += 1
    else: other_num += 1
print('字母个数', char_num)
print('数字个数', digit_num)
print('其他字母个数', other_num)

10

radius = int(input("请输入半径: "))
for i in range(2 * radius + 1):
    half = round((radius ** 2 - (radius - i) ** 2) ** 0.5)
    print("  " * (radius - half), end="")
    print("*", end="")
    print("  " * half * 2, end="")
    print("*")

11

lines = int(input("请输入一半的行数: "))
# 打印上半
for i in range(lines):
    print("-" * (lines * 2 - 2 - 2 * i), end="")
    my_list = []
    for j in range(i + 1):
        my_list.append(chr(96 + lines - j))
    for j in range(i):
        my_list.append(chr(ord(my_list[-1]) + 1))
    print('-'.join(my_list), end="")
    print("-" * (lines * 2 - 2 - 2 * i))
# 打印下半
for i in range(lines - 1):
    print("-" * (2 * (i + 1)), end="")
    # 换一种方式初始化list列表
    my_list = [chr(96 + lines - j) for j in range(lines - 1 - i)]
    for j in range(lines - 2 - i):
        my_list.append(chr(ord(my_list[-1]) + 1))
    print('-'.join(my_list), end="")
    print("-" * (2 * (i + 1)))

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 点我我会动 设计师:白松林 返回首页