零基础学Python————每日一练,打印输出(1)

题目

"""
Python  每日一练
题目:输入一组整数a b,打印输出示例中的字符需要a行b列
输入:1 1
输出:
+---+
|   |
+---+
输入:2 3
输出:
+---+---+---+
|   |   |   |
+---+---+---+
|   |   |   |
+---+---+---+
输入:4 5
+---+---+---+---+---+
|   |   |   |   |   |
+---+---+---+---+---+
|   |   |   |   |   |
+---+---+---+---+---+
|   |   |   |   |   |
+---+---+---+---+---+
|   |   |   |   |   |
+---+---+---+---+---+
"""

思考

怎样用对简短的代码输出上述内容?此题类似于大部分Python教程中的乘法表打印,思路可参考!

参考代码

str1 = input().split(" ")
a = int(str1[0])
b = int(str1[1])
for i in range(b):
    if i == 0:
        print("+---+", end="")
    else:
        print("---+", end="")
print()
for i in range(b):
    if i == 0:
        print("|   |", end="")
    else:
        print("   |", end="")
print()
for i in range(b):
    if i == 0:
        print("+---+", end="")
    else:
        print("---+", end="")
j = 1
while j < a:
    j += 1
    print()
    for i in range(b):
        if i == 0:
            print("|   |", end="")
        else:
            print("   |", end="")
    print()
    for i in range(b):
        if i == 0:
            print("+---+", end="")
        else:
            print("---+", end="")

零基础学Python————每日一练,打印输出(1)_第1张图片
零基础学Python————每日一练,打印输出(1)_第2张图片
零基础学Python————每日一练,打印输出(1)_第3张图片
零基础学Python————每日一练,打印输出(1)_第4张图片

讨论

如果你还有最优解,欢迎留言讨论!!

你可能感兴趣的:(#,2,python的学习训练,python)