概述
我的解决方案:# Response to StackOverflow post:
# Making a hollow box in Python
# The multiplication operator (*) is the same as repeated
# concatenation when applied to strings in Python.
# I solved the problem by creating an array with N elements
# and gluing them together (str.join(array)) with newline
# characters.
# I trust you're already familiar with string formatting and
# concatenation, but in case you're not, please feel free to
# ask for clarification.
def main():
n = int (input("Enter an integer between 1 and 15"))
box = "n".join(["*"*(2*n)] + ["*%s*" % (" "*(2*n-2))]*(n-2) + ["*"*(int(n>1)*2*n)])
print (box)
if __name__ == '__main__':
main()
input() # Prevents the console from closing immediately
至于你的解决办法。在我看来,循环条件似乎混乱了;列循环和行循环的顺序相反,列循环中range()的参数应该是2*n(因为这是与每行相交的列数)。您还应该再次查看第一个print语句中的条件。
最后
以上就是温婉自行车为你收集整理的python打印空心长方形用*表示_用Python制作符合这些规范的空心盒?的全部内容,希望文章能够帮你解决python打印空心长方形用*表示_用Python制作符合这些规范的空心盒?所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复