如何让它画一个带点的矩形?

时间:2015-03-29 21:18:09

标签: python python-3.x

经过2个小时的寻找答案,我放弃了。现在我得到了点线,但我希望它是一个矩形。我希望这能打印用户输入长度和宽度的矩形。

print("Insert width:")
width_number= int(input())
print("Insert length:")
length_number = int(input())

for length in range(length_number):
    for width in range(width_number):
        print("* "* width, end="")
    print("* " * width)
    print()

预期产出:

**********
*        *
*        *
*        *
*        *
*        *
*        *
*        *
*        *
**********

2 个答案:

答案 0 :(得分:2)

在开头和结尾打印*倍的宽度。打印宽度为"*"的两个{ - 1>}

print("*" * width_number)
for _ in range(length_number):
    print("*" + (" " * (width_number-2)+ "*") )
print("*" * width_number)

您还可以使用str.format

print("*" * width_number)   
for _ in range(length_number):
    print("*{:^{fill}}*".format(" ",fill=width_number-2))
print("*" * width_number)


Insert width:
10
Insert length:
10
**********
*        *
*        *
*        *
*        *
*        *
*        *
*        *
*        *
*        *
*        *
**********

答案 1 :(得分:0)

试试此代码

print("Insert width:")
width_number= int(input())
print("Insert length:")
length_number = int(input())
print("* "*width_number)
for length in range(length_number - 2):
    print("* ", end="")
    for width in range(width_number - 2):
        print("  ", end="")
    print("* ")
print("* "*width_number)

如果你想要* / / p>之间的空格