我即将弄清楚这段代码,但它一直导致底线缺失。
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() ,但它给了我我需要的半结果。有什么建议吗?
您可以通过在现有代码中添加一些行来获得所需的输出:
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
我希望这有帮助。