Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用 for 循环。我得到一个输入n,我必须打印一个边长为 size 的正方形n。
n
例如,使用n = 4:
n = 4
******* *** *** ** ** * * ** ** *** *** *******
看起来很奇怪,但里面的空白是正方形;n(4) 是空白正方形边上的星数。
关键是将四分之一的模式存储到一个列表中。有了它,您将能够水平和垂直镜像图案:
def square(n): lines = ['*' * (n - i) + " " * i for i in range(n)] for l in lines + lines[-2::-1]: print(l + l[::-1]) square(4)
输出:
******** *** *** ** ** * * ** ** *** *** ********