如何编码需要用行打印的东西

发布于 2025-01-18 17:17:32 字数 330 浏览 3 评论 0原文

adef star(a)中的参数,

我必须定义def和编码。

我在问a = int(输入(“什么是?:”)

例如 第一行有一个标志, 第二行有两个标志, 第三行有3个标志,完成了。

def star(a):
    #idk
    return #dk
a=int(input("a is a:  "))
print("Shape:  ", star(a))

a is a parameter in def star(a)

I have to define a def and coding.

Im asking a= int(input("what is a?: ").

For example if a is 3:
first line has one sign,
second line has two sign,
third line has 3 sign and done.

def star(a):
    #idk
    return #dk
a=int(input("a is a:  "))
print("Shape:  ", star(a))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不甘平庸 2025-01-25 17:17:32

你可以在 python 中使用 * 来乘以字符串!

def print_line(number_of_signs):
    print("*"*number_of_signs+1) # plus one since it otherwise prints 0 starts at first iteration


a = int(input("a is a: "))
for line in range(a):
    print_line(line)

如果您希望星星从中间开始,您应该在 print_line 中添加一些逻辑以形成一个字符串,然后打印该字符串。

编辑:在 print_line 中添加 +1 以处理第一次迭代为 0 的情况。

You can just multiply strings in python using *!

def print_line(number_of_signs):
    print("*"*number_of_signs+1) # plus one since it otherwise prints 0 starts at first iteration


a = int(input("a is a: "))
for line in range(a):
    print_line(line)

If you want the stars to start in the middle, you should add some logic in print_line to form a string, than print that string.

edit: added +1 in print_line to deal with the first iteration being 0.

最笨的告白 2025-01-25 17:17:32

你的问题不是最清楚,但我还是会尽力回答。

def star(a):
    # Will only work if a is odd
    if (a % 2) == 0:
        print("a should be odd!")
        return None

    output = ""  # Final output string
    for i in range(1, a + 1, 2):                                     # This represents the number of signs per line, must always be odd so start at 1 and increment by 2
        padding = int((a - i) / 2)                                   # This is the padding on either side of the signs

        line = (" " * padding) + ("-" * i) + (" " * padding) + "\n"  # Create the line, \n creates a new line

        output += line                                               # Add the line to the final output

    return output


a = int(input("a > "))
print(star(a))

输出:

a > 5
  -  
 --- 
-----

这就是你想要的吗?

Your question isn't the clearest, but I'll try to answer nonetheless.

def star(a):
    # Will only work if a is odd
    if (a % 2) == 0:
        print("a should be odd!")
        return None

    output = ""  # Final output string
    for i in range(1, a + 1, 2):                                     # This represents the number of signs per line, must always be odd so start at 1 and increment by 2
        padding = int((a - i) / 2)                                   # This is the padding on either side of the signs

        line = (" " * padding) + ("-" * i) + (" " * padding) + "\n"  # Create the line, \n creates a new line

        output += line                                               # Add the line to the final output

    return output


a = int(input("a > "))
print(star(a))

Output:

a > 5
  -  
 --- 
-----

Is that what you were going for?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文