打印数值的步骤

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

Printing values in steps

问题

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

总数 = 11
for t in range(0, 总数, 10):
    print(t)

当前输出是

0
10

预期输出是

0
10
英文:

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

Total = 11
for t in range(0, Total):
    print(t)

The current output is

0
1
2
3
4
5
6
7
8
9
10

The expected output is

0
10

答案1

得分: 1

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

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:

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:

确定