python输出减法口诀表
作者:野牛程序员:2024-11-18 13:40:34python阅读 2126
python输出减法口诀表
# 打印减法口诀表 def print_triangle_subtraction_table(): print("减法口诀表:") for i in range(1, 10): # 控制行数 for j in range(1, i + 1): # 每行输出的列数 print(f"{i} - {j} = {i - j:2}", end=" ") # 减法结果占2字符宽度 print() # 每行结束后换行 # 调用函数 print_triangle_subtraction_table()
外层循环:
控制行数,
i
表示减法中的第一个数字。内层循环:
控制每行的列数,
j
表示减法中的第二个数字,范围从 1 到i
。结果对齐:
使用
f"{i - j:2}"
格式化减法结果,确保至少占 2 个字符宽度,使表格对齐整齐。换行:
每完成一行输出后,通过
print()
换行。
减法口诀表: 1 - 1 = 0 2 - 1 = 1 2 - 2 = 0 3 - 1 = 2 3 - 2 = 1 3 - 3 = 0 4 - 1 = 3 4 - 2 = 2 4 - 3 = 1 4 - 4 = 0 5 - 1 = 4 5 - 2 = 3 5 - 3 = 2 5 - 4 = 1 5 - 5 = 0 6 - 1 = 5 6 - 2 = 4 6 - 3 = 3 6 - 4 = 2 6 - 5 = 1 6 - 6 = 0 7 - 1 = 6 7 - 2 = 5 7 - 3 = 4 7 - 4 = 3 7 - 5 = 2 7 - 6 = 1 7 - 7 = 0 8 - 1 = 7 8 - 2 = 6 8 - 3 = 5 8 - 4 = 4 8 - 5 = 3 8 - 6 = 2 8 - 7 = 1 8 - 8 = 0 9 - 1 = 8 9 - 2 = 7 9 - 3 = 6 9 - 4 = 5 9 - 5 = 4 9 - 6 = 3 9 - 7 = 2 9 - 8 = 1 9 - 9 = 0
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892