-2

I want to print all of the integers in the range 1000-3000 that are even and then print them in one line, each seperated by a comma. This is my code so far:

for i in range (1000,3001):
      s = str(i)
      if i % 2 == 0:
            print (i)

If I try to add the split() function, this error occurs: 'int' object has no attribute 'split'

How can I do this?

Hashinu
  • 25
  • 5

3 Answers3

2

As simple as:

print(','.join([str(i) for i in range(1000, 3001, 2)]))

Or alternatively:

print(*range(1000, 3001, 2), sep=',')

You don't need to check for even numbers, since you can just print every second number starting with an even number.

ruohola
  • 21,987
  • 6
  • 62
  • 97
0

Te answer to this is as simple as follows:

out = [str(i) for i in range(1000, 3001) if i % 2 == 0]

print(",".join(out))

The output reads:

1000, 1002, ..., 3000
pdrersin
  • 311
  • 3
  • 10
-1

Try this, it works fine :

even_number_list=[]    # taking an empty list to store even numbers

for i in range(1000,3001):
        if(i%2==0):
                even_number_list.append(i)   # appending even numbers in our list

print(*even_number_list,sep=',')            # unpacking list with values seperated by comma