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

在Jupyter Notebook中用Python打印占星师星形图案失败求助

占星师星形图案打印问题排查与修复

问题分析

你的代码无法输出预期的星形图案,核心问题不在Jupyter Notebook,而是代码本身的逻辑错误:每次调用print("*")时,Python默认会在输出后添加换行符,导致每个星号单独占一行,无法形成每行连续的星号序列。

错误点说明

原代码中内层循环的print("*")会让每个星号单独换行,而你需要的是在同一行打印多个星号,行末再换行。

修正后的代码

print("Astrologer's star")
print("How many row of star you want?")
n = int(input())
print("Please input 1 if you want stars in ascending form or \nprint 0 if you want stars in descending form")
oneORzero = bool(int(input()))

if oneORzero == True:
    for i in range(1, n+1):
        for j in range(1, i+1):
            # 取消自动换行,让星号在同一行连续输出
            print("*", end="")
        # 每行星号打印完后,手动换行
        print("")
else:
    for i in range(n, 0, -1):
        for j in range(1, i+1):
            print("*", end="")
        print("")

效果说明

修正后,递增模式下会输出从上到下星号数量逐行增加的星形,递减模式则相反,每行星号连续排列在同一行,符合预期的图案效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 12:57:46