我的直角图案不完整,为什么我缺少一些底部的行或列?

问题描述 投票:0回答:1

我即将弄清楚这段代码,但它一直导致底线缺失。

The pattern is supposed to be like this:

🐟
🐟🐟
🐟🐟🐟
🐟🐟
🐟

My code:
def get_right_arrow_pattern(max_cols)
    emoji = "🐟"
    result = ""
    max_rows = max_cols
    for row in range(1, max_rows+1):
        for column in range(1, max_cols+1):
            result = "" + result
        result += str(emoji)
        print(result)
    return emoji

Output:
🐟
🐟🐟
🐟🐟🐟
🐟🐟🐟🐟
🐟
None

技巧是我不能在函数或 for 循环中使用 print() ,但它给了我我需要的半结果。有什么建议吗?

python function for-loop design-patterns pattern-matching
1个回答
0
投票

您可以通过在现有代码中添加一些行来获得所需的输出:

def get_right_arrow_pattern(max_cols):
    emoji = "🐟"
    result = ""
    max_rows = max_cols

    for row in range(1, max_rows+1):
        result += str(emoji)
        print(result)

    for row in range(1, max_rows):
        result = result[:-1] # This will remove the last character from the string
        print(result)

    return emoji

我希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.