【发布时间】:2022-11-15 09:01:16
【问题描述】:
我列出了从 0 到 500 的所有数字,然后我正在寻找以某个整数结尾的所有数字。问题是我不明白它是如何工作的。
我是编码新手,所以不知道这里会发生什么或它是如何工作的。
x = 0
y = []
while x <= 500:
y.append(x)
x = x + 1
a = 0
b = []
c = 0
# if i remove c from this or change c from 0 to 1 or any other number it just appends with that value
# but if c is as i have it, it some how appends the list with the values i am pulling with my if statment
while a <= 500:
if int(repr(y[a])[-1]) == 0:
b.append(c)
a = a + 1
c = c + 1
print(len(b))
print(b)
【问题讨论】:
-
不需要
c,它始终与a相同。所以只需使用b.append(a) -
要获取数字的最后一位,请使用
y[a] % 10。 -
你的实际问题是什么? “我不明白它是如何工作的”太含糊了。没看懂怎么写的,随便乱码拼凑就走运了?
-
如果您更改
c,那么您添加到列表中的数字将从您想要的数字偏移c和a之间的差异,因为您正在测试a但附加c,并且他们是不同的。
标签: python