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

Python嵌套循环问题求助:符号未按行列排列而是竖向输出

修复嵌套循环的符号行列排列问题

你的问题出在print(symbol)的默认行为上——Python的print()函数默认会在输出末尾添加换行符,所以每个符号都会单独占一行,最终变成竖向单列。

修复后的代码如下:

rows = int(input("Enter the number of rows: "))
columns = int(input("Enter the number of columns: "))
symbol = input("Enter the symbol to use: ")

for i in range(rows):
    for j in range(columns):
        print(symbol, end='')  # 添加end=''取消自动换行
    print()  # 每行结束后手动换行

关键说明:

  • print(symbol, end=''):通过end参数指定输出结束时不添加换行符,让同一行的符号连续输出。
  • 内层循环完成后调用空的print(),会自动输出一个换行符,实现每行结束后换行,从而形成规整的行列结构。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 23:45:54