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

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

not giving output the way I’m expecting

问题

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

</blockquote>

this was the question

  1. for i in range(1, 6):
  2. # Print numbers in increasing order
  3. for j in range(1, i + 1):
  4. print(j, end=&#39; &#39;)
  5. # Print numbers in decreasing order
  6. for j in range(i - 1, 0, -1):
  7. print(j, end=&#39; &#39;)
  8. # Move to the next line
  9. print()

when we print the above code I’m getting

  1. 1
  2. 1 2 1
  3. 1 2 3 2 1
  4. 1 2 3 4 3 2 1
  5. 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. 1 2 1
  2. 1 2 3 2 1
  3. 1 2 3 4 3 2 1
  4. 1 2 3 4 5 4 3 2 1

</blockquote>

this was the question

  1. for i in range(1, 6):
  2. # Print numbers in increasing order
  3. for j in range(1, i + 1):
  4. print(j, end=&#39; &#39;)
  5. # Print numbers in decreasing order
  6. for j in range(i - 1, 0, -1):
  7. print(j, end=&#39; &#39;)
  8. # Move to the next line
  9. print()

when we print the above code I’m getting

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

答案1

得分: 0

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

  1. 正确的代码应该是
  2. for i in range(2, 6):
  3. for j in range(1, i + 1):
  4. print(j, end=' ')
  5. for j in range(i - 1, 0, -1):
  6. print(j, end=' ')
  7. print()
  8. 第一个循环将执行4每次应该从1开始所以第二个循环的范围从1开始
英文:

the correct code should be:

  1. for i in range(2, 6):
  2. for j in range(1, i + 1):
  3. print(j, end=&#39; &#39;)
  4. for j in range(i - 1, 0, -1):
  5. print(j, end=&#39; &#39;)
  6. 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:

确定