问题:程序应读取两个正整数n和m,其中n 和m分别是行数和列数(0
这是我的代码。但这是错误的。 请帮我。
def line():
print('+---+')
def repeat(c, n):
print(c * n)
def box(row, col):
while row > 0:
repeat(line, n)
print('| |')
row = row - 1
print('+---+'*n)
while col > 0:
repeat(line, m)
print('| |')
col = col - 1
print('+---+'*m)
n = int(input())
m = int(input())
box(n, m)
答案 0 :(得分:1)
您不能像这样调用函数行,而改为line()
。同样,如果使用两个while循环,它将打印2x2的网格和3x3的网格。
下面是使用递归函数的另一种方法。
def repeat(row, col):
if row == 0:
return
print(col * '+---' + '+')
print(col * '| ' + '|')
return repeat(row-1, col)
def box(row, col):
repeat(row, col)
print(col * '+---' + '+')
n = int(input("rows: "))
m = int(input("cols: "))
box(n, m)
Output:
rows: 2
cols: 3
+---+---+---+
| | | |
+---+---+---+
| | | |
+---+---+---+