打印数值的步骤

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

Printing values in steps

问题

我正在按1的步长打印数值,但我想按10的步长打印。

  1. 总数 = 11
  2. for t in range(0, 总数, 10):
  3. print(t)

当前输出是

  1. 0
  2. 10

预期输出是

  1. 0
  2. 10
英文:

I am printing values in steps of 1, but I want to print in steps of 10.

  1. Total = 11
  2. for t in range(0, Total):
  3. print(t)

The current output is

  1. 0
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. 8
  10. 9
  11. 10

The expected output is

  1. 0
  2. 10

答案1

得分: 1

只需简单使用 range() 函数的 step 参数

  1. for t in range(0, Total, 10):

对于正步长,范围 r 的内容由公式 r[i] = start + step*i 确定,其中 i >= 0r[i] < stop

对于负步长,范围的内容仍然由公式 r[i] = start + step*i 确定,但约束条件是 i >= 0r[i] > stop

英文:

Simply use the step argument of the range() function:

  1. for t in range(0, Total, 10):

> For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i &gt;= 0 and r[i] &lt; stop.
>
> For a negative step, the contents of the range are still determined by the formula r[i] = start + step*i, but the constraints are i &gt;= 0 and r[i] &gt; stop.

huangapple
  • 本文由 发表于 2023年6月12日 11:54:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453557.html
匿名

发表评论

匿名网友

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

确定