没有按照我期望的方式输出。

huangapple go评论60阅读模式
英文:

not giving output the way I’m expecting

问题

<blockquote>

打印以下模式。任何两个相邻数字之间都恰好有一个空格。任何一行末尾都没有空格。
```none
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

</blockquote>

this was the question

for i in range(1, 6):
# Print numbers in increasing order
    for j in range(1, i + 1):
        print(j, end=&#39; &#39;)

# Print numbers in decreasing order
    for j in range(i - 1, 0, -1):
        print(j, end=&#39; &#39;)

# Move to the next line
    print()

when we print the above code I’m getting

1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
英文:

<blockquote>

Print the following pattern. There is exactly one space between any two consecutive numbers on any line. There are no spaces at the end of any line.

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

</blockquote>

this was the question

for i in range(1, 6):
# Print numbers in increasing order
    for j in range(1, i + 1):
        print(j, end=&#39; &#39;)

# Print numbers in decreasing order
    for j in range(i - 1, 0, -1):
        print(j, end=&#39; &#39;)

# Move to the next line
    print()

when we print the above code I’m getting

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

答案1

得分: 0

以下是翻译好的代码部分:

正确的代码应该是

    for i in range(2, 6):
        for j in range(1,  i + 1):
            print(j, end=' ')
        for j in range(i - 1, 0, -1):
            print(j, end=' ')
        print()

第一个循环将执行4次每次应该从1开始所以第二个循环的范围从1开始
英文:

the correct code should be:

for i in range(2, 6):
    for j in range(1,  i + 1):
        print(j, end=&#39; &#39;)
    for j in range(i - 1, 0, -1):
        print(j, end=&#39; &#39;)
    print()

The first loop will be 4 times, every time should begin with 1, so the range of the second loop starts with 1.

huangapple
  • 本文由 发表于 2023年7月4日 21:54:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定