1

Hey I am new to programming with python and just want that this code printing out every number in range 0 and 100 that has the digit 9 in it, but I always get this message: argument of type 'int' is not iterable

for i in range(0, 100):
    if 9 in i:
        print(i)

So what I expect is that my code will return:

9 19 29 39 49 59 69 79 89 90 91 92 93 94 95 96 97 98 99

tushar_lokare
  • 461
  • 1
  • 8
  • 22
LEL
  • 23
  • 4
  • 1
    `i != str(i)` You are confusing a number and its string representation. `9 in i` is nonsensical since a number is not a container of digits. On the other hand, `'9' in str(i)` makes perfect sense. – John Coleman Feb 10 '19 at 12:19
  • Ok thanks now it works :) – LEL Feb 10 '19 at 12:22

1 Answers1

0
for i in range(0, 100): 
    if 9 in i:
        print(i)

The line

if 9 in i:

will work if i is a list or an array since your iterator returns only one value which is int( int is not iterable), it fails. A simpler way to do this is

for i in range(0, 100): 
    if "9" in str(i):
        print(i)

What's happening here is that you are converting the int to string( which is an array of characters), no you can use the "in" method.

shahidammer
  • 1,026
  • 2
  • 10
  • 24