简体   繁体   English

我需要帮助在 python 中打印此模式,我已经给出了我的方法,但无法弄清楚请支持

[英]I need help to print this pattern in python, i have given my approach but couldnt figure out pls support

Required Pattern所需模式

My Approach Code:我的方法代码:

n=int(input(":"))
for i in range(n+1):
    for j in range(1,i+1):
        print(","+str(j), end="")
    print()
    
for i in range(n, 1, -1):
    for j in range(1, i):
        print(","+str(j), end="")
    print()

But Couldnt get required pattern pls help但无法获得所需的模式请帮助

You can use "," as the print separator instead of concatenating the comma as long as you print all numbers in one call to print (which you can do with unpacking):您可以使用“,”作为打印分隔符,而不是连接逗号,只要您在一次打印调用中打印所有数字(您可以通过拆包来完成):

n = 5
for d in range(1-n,n):
    print(*range(1,n-abs(d)+1),sep=",")

1
1,2
1,2,3
1,2,3,4
1,2,3,4,5
1,2,3,4
1,2,3
1,2
1

If you want to adjust your code, you could use a variable for the comma and only have it contain an actual comma from the second number onward:如果你想调整你的代码,你可以为逗号使用一个变量,并且只让它包含从第二个数字开始的实际逗号:

n=int(input(":"))
for i in range(n+1):
    comma = ""
    for j in range(1,i+1):
        print(comma+str(j), end="")
        comma = ","
    print()
    
for i in range(n, 1, -1):
    comma = ""
    for j in range(1, i):
        print(comma+str(j), end="")
        comma = ","
    print()

Note that you are printing an extra blank line at the beginning.请注意,您在开头打印了一个额外的空白行。 If that is not your intention, you should make the first loop use range(1,n+1)如果这不是你的意图,你应该让第一个循环使用range(1,n+1)

Another way to fix your code would be to systematically print a comma as the ending character and use the final print for the last number on each line:修复代码的另一种方法是系统地打印逗号作为结束字符,并将最终打印用于每行的最后一个数字:

n=int(input(":"))
for i in range(1,n+1):
    for j in range(1,i):
        print(j, end=",")
    print(i)
    
for i in range(n-1, 0, -1):
    for j in range(1, i):
        print(j, end=",")
    print(i)

I follow your code and get this result.我按照你的代码得到这个结果。 What point do you want to change?你想改变什么点?

pattern图案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法弄清楚这段代码中关于python的问题,需要帮助 - I can't figure out the problem in this code about python, need help 在我的代码中没有得到我需要的输出,我不知道为什么。 Python - Not getting the output I need in my code and I can't figure out why. Python Python + Selenium:页面对象模式:需要帮助找出为什么看不到属性 - Python + Selenium: Page object pattern: Need help to figure out why it doesn't see an attribute 我在自学python时给自己写了一些气泡排序代码,但无法弄清楚为什么它不起作用 - I write myself a little bubble sorting code while self-learning python, but couldnt figure out why it doesnt work 我的程序中有一个我无法弄清楚的错误 - I have a bug in my program that I cannot figure out 我需要弄清楚如何使我的程序重复。 (Python编码类) - I need to figure out how to make my program repeat. (Python coding class) 我需要帮助来弄清楚我需要哪些代码来删除重复项 - I need help to figure out what code i need to drop duplicates 我需要在我的列表中找出一个自动化求和操作 - I need to figure out an automation sum op in my lists 我不知道如何处理行长> 79 pep8的规则? - I couldnt figure out how can I handle line too long > 79 pep8 rule? 我需要在我的代码末尾打印一个计算好的奖金,但我似乎不知道怎么做。 我将如何 go 关于这个? - I need to make a calculated bonus print out at the end of my code but I can not seem to figure out how. How would I go about this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM