python用for和while语句求解

python
用for和while语句分别编程求解从1-1000内,有哪些数能够被三整除,并求出一共几个

望采纳


count_for = 0
lst_for=[]
count_while = 0
lst_while=[]
count_tmp = 0
for i in range(1000):
    if i % 3 ==0:
        count_for+=1
        lst_for.append(i)
        
print("For:被3整除的个数:",count_for,"分别为", lst_for)

while(count_tmp < 1000):
    if count_tmp %3 ==0:
        count_while +=1
        lst_while.append(count_tmp)
    count_tmp +=1

print("While:被3整除的个数:",count_while,"分别为", lst_while)

count = 0
for i in range(1,1001):
    if i % 3 == 0:
        count += 1
        print(i,end=" ")
print("\ncount= ",count)

i = 0
count = 0
while i<1001:
    if i % 3 == 0:
        count += 1
        print(i,end=" ")
    i += 1
print("\ncount= ",count)

img