繁体   English   中英

我该怎么做?

[英]What should I do for this?

问题:程序应该读取两个正整数 n 和 m,其中 n 和 m 分别是行数和列数(0 < n,m < 100)。 例如,下表是 n = 2 和 m = 3 时的结果。

这是我的代码。 但这是错误的。 请帮我。

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)

你不能像这样调用函数 line,而是调用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                                                                                                                                                                     
+---+---+---+                                                                                                                                                               
|   |   |   |                                                                                                                                                               
+---+---+---+                                                                                                                                                               
|   |   |   |                                                                                                                                                                 
+---+---+---+ 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM