按照这些规范用Python制作一个空心盒子?

2024-03-26

我要“编写一个 python 程序,提示用户输入一个正整数 n。然后该程序打印一个包含 n 行和 2*n 列的空心矩形。例如,输入 3 将输出:”

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

我的代码是:

n=int(input('Please enter a positive integer between 1 and 15: '))
for col in range(n):
    for row in range(n):
        print('*' if col in(0,(2*n)+1) or row in(0,n+1) else ' ', end=' ')
    print()

但我的输出看起来根本不像我需要的;它就像一个空心盒子的上半部分和左半部分。此外,我没有得到我需要的双列。我究竟做错了什么?

编辑:谢谢大家的帮助!给了我很多见解并且非常有帮助。我将我的代码修改为以下内容,并且效果很好:

>n=int(input('Please enter a positive integer between 1 and 15: '))
>for row in range(n):
>    for col in range(2*n):
>        print('*' if row in(0,n-1) or col in(0,(2*n)-1) else ' ', end=' ')
>    print()

特别感谢用户2357112;你让我清楚地意识到我犯了什么错。


我的解决方案:

# 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 (因为这是与每行相交的列数)。您还应该再次查看第一个打印语句中的条件。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

按照这些规范用Python制作一个空心盒子? 的相关文章

随机推荐

  • Razor 中枚举下拉列表的显示名称

    如何在 Razor 的下拉列表中显示枚举的自定义名称 我当前的代码是 Html DropDownListFor model gt model ExpiryStage new SelectList Enum GetValues typeof
  • 制作 CSS 精灵的工具? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 在android项目中添加“jfeinstein10 / SlidingMenu”的确切过程

    我对android完全陌生 我正在尝试实现Sliding Menu having ListView with Swipe Gesture 通过一些搜索我发现https github com jfeinstein10 SlidingMenu
  • 图像增强使性能变得更差[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我正在研究一个图像分割项目 并一直在尝试采用图像增强技术来增加训练集的大小 起初 我只是尝试使用水平翻转将图像尺寸放大一倍 但我发现性
  • 在php中的共享主机上创建并导入mysql数据库

    我正在尝试编写 php 脚本 连接到 mysql 创建数据库 创建用户和密码 将用户添加到数据库 将准备好的sql文件导入数据库 问题是它将位于共享主机上 我正在努力使其通用并与不同的主机一起使用 我猜数据库和 db 用户将在共享主机上以我
  • Apache 2.4 需要 ip 不起作用

    尝试从旧的允许 拒绝 顺序语法转换为新语法以保护 WordPress 管理部分 但我无法让它识别我的 IP 这就是我的 htaccess文件包含在 wp admin folder ErrorDocument 401 default Erro
  • Java变量声明在性能方面的差异[重复]

    这个问题在这里已经有答案了 这两种声明的区别在哪里String s 1 public static void main String args String s for int i 0 i lt 1000000 i s String i S
  • 请批评我的 PHP 身份验证工作

    发帖后this https stackoverflow com questions 443988 simple authorisation login capability in php不久前 我决定用 PHP 创建自己的注册 身份验证功能
  • 使用 Ubuntu,如何从 CPAN 安装 DBD::Sybase?

    每当我尝试构建DBD Sybase连接到 MSSQL 时出现错误 sudo cpanp install DBD Sybase Installing DBD Sybase 1 15 Running usr bin perl usr bin c
  • 在 Go 中将事物通道作为接口通道传递

    我的程序有一个管道结构 我刚刚实现了一个缓存过滤器 如果已处理的数据版本在缓存中 则该过滤器将直接将内容发送到输出 func Run in chan downloader ReadyDownload chan CCFile out make
  • 绘制曲线连接网页上的元素

    I have been tasked with turning this rough idea into a live page 虽然我已经建立了基本结构 但我想知道创建连接月份的曲线的最佳方法 我应该只创建图像并覆盖它们吗 有没有办法用某
  • PHP gnupg'导入失败'

    我一直在尝试在 PHP 中使用 gnupg 来加密将上传到 ftp 文件夹的文件 我目前正在使用 MAMP 在 mac 上运行 并且我相信 gnupg 已正确安装 我有来自文件接收者的公钥 当我尝试导入该密钥 无论是作为字符串文字还是从文本
  • 如何在 Rails 内的 AREL 子查询上进行连接

    我有一个简单的模型 class User has many logs class Logs 通过外键logs user id以通常的方式关联 我正在尝试使用 Arel 执行以下操作 根据 Arel 文档 它应该可以工作 u t Arel T
  • Pyspark 命令无法识别

    我安装了 anaconda 还下载了 Spark 1 6 2 我使用此答案中的以下说明为 Jupyter 配置 Spark在此输入链接描述 https stackoverflow com questions 33064031 link sp
  • JPA,外部化列、表或模式名称?

    我将 Spring Hibernate Tomcat Oracle 应用程序改编为分布式形式 并且需要在 JPA 注释中自定义模式名称 类似于 Spring EL Entity Table name LOSS schema app data
  • 重写java中的方法,然后将对象转换为父类行为

    我有一个父类 A 和一个子类 B B 重写了 A 中的方法 f public class A public String f return A public class B extends A public String f return
  • Monotouch/WCF:为什么无法覆盖wcf绑定超时设置

    Monotouch WCF 为什么无法覆盖 wcf 绑定默认超时设置 public class MyServiceClient ClientBase
  • Jade/Pug if else 条件用法

    我正在使用 js 文件将日期发送到 jade 文件Node js 当 的时候 date 场是false 它执行 else 并打印man因为它的答案 可能出了什么问题 if date false date else man 如果 date 为
  • ActiveRecord什么时候保存关联?

    我知道它会保存关联autosave true as per https api rubyonrails org classes ActiveRecord AutosaveAssociation html https api rubyonra
  • 按照这些规范用Python制作一个空心盒子?

    我要 编写一个 python 程序 提示用户输入一个正整数 n 然后该程序打印一个包含 n 行和 2 n 列的空心矩形 例如 输入 3 将输出 我的代码是 n int input Please enter a positive intege