# 第四章 课后练习

### 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)))


• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 打赏

打赏

Ronny-starting

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 举报
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文

10-17

11-16 2755
05-15 1364
11-12 350
11-15 3609
05-13 297
10-31 457
11-13 2868
03-14 794
11-15 1113
11-14 1540
04-24 1139
04-01 218
04-19
03-09 120
12-18 2230