python - Python中的星星组成的正方形
问题描述
你们中有人知道像 JetBrains Academy 这样的东西吗?你能帮我完成这个任务吗?我当然知道这很简单,但我真的做不到。我已经尝试了很多次。
解决方案
正方形中唯一变化的是内部,所以只需检查您是在第一行还是最后一行。
不是最有效的,但 python 让它变得简单。
size = 4
for i in range(size): # each loop prints a row
print(*
['*'] + # first star in the row
['*' if i in [0, size-1] else ' '] * (size-2) + # inside stars of the row
['*'] # last star in the row
)
* * * *
* *
* *
* * * *
推荐阅读
- ios - 为什么带有惰性变量注册单元类的 UIColletionView 会引发错误?
- javascript - 使用 JavaScript 隐藏/删除 ul 类中的文本
- extjs - 使用构面优化 AEM QueryBuilder
- php - pdo按钮删除行
- sql - 防止 SQL 注入的动态 SQL 搜索
- git - 使用 git log -L 通过提交跟踪特定的代码行
- python - salt-ssh:尝试 cpmile sls 命令时出现错误
- javascript - 在 javascript 中重载 `?` 运算符以处理 null 或 undefined
- sql - 基于触发器的多列的row_number
- amazon-web-services - AWS Greengrass 核心在 lambda 函数中拥有自己的名称