You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python绘制宽度可变空心+号方框 中间行代码编写求助

解决方法

空心方框的中间行逻辑为仅首尾为+,中间填充对应数量的空格,总长度和上下边框保持一致。

补全middle变量的代码

middle = "+" + " " * (width - 2) + "+"

完整可运行代码

width = int(input("Enter box size(3-30) >>>"))
top = "+" * width
middle = "+" + " " * (width - 2) + "+"
bottom = "+" * width

# 输出完整方框
print(top)
# 中间行需要输出 width-2 次
for _ in range(width - 2):
    print(middle)
print(bottom)

效果验证

当输入3时,输出结果和预期效果完全一致:

+++
+ +
+++

如果输入5,会输出尺寸为5的空心方框:

+++++
+   +
+   +
+   +
+++++

内容的提问来源于stack exchange,提问作者Muramatsu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 06:57:02