python - 为什么下面的模式程序没有给出所需的输出?

标签 python

n=int(input('enter no of rows\n'))
for row in range(0,n):
    for col in range(0,n):
        if row==0 or col==(n-1):
             print("A",end="")
        else:
            print(end="")
    print() 

我需要如下所示的输出。我没有得到它。任何人都可以帮助我吗?代码有任何问题吗?

AAAAA
 A  A
  A A
   AA
    A

最佳答案

如果你想用你的方式:

n=int(input('enter no of rows\n'))
for row in range(0,n):
    for col in range(0,n):
        if row == 0 or col == (n-1) or row == col:
            print ("A",end ="")
        else:
             print(" ", end ="")
    print()

输出:

enter no of rows
5
AAAAA
 A  A
  A A
   AA
    A

关于python - 为什么下面的模式程序没有给出所需的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56986939/

相关文章:

python - 在运行时扩展一个实例

python追加到嵌套列表,索引超出范围

Python:如何确保使用 JSON 转储/加载验证数据

python - 如何从 C 扩展引发 Python 异常

python - 昆布+RabbitMQ : Check whether a queue is empty

python - 在 python 中创建进程,仅当它不存在时

python - 在 Apache Zeppelin 中调用 pandas_profiling.ProfileReport.to_widgets() 时出现 NotImplementedError

python - 将多个excel文件导入python pandas并将它们连接到一个数据帧中

javascript - 在 Flask/Javascript 中获取动态列表元素到服务器

python - TF 2.0 打印张量值