Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

Here's my code:

import math 

print("Hey, let's solve Task 4 :)" 

number1 = input ("How many digits do you want to look at? ") 

number2 = input ("What would you like the digits to add up to? ")

if number1 == 1: 

   cow = range(0,10) 

elif number1 == 2: 

   cow = range(10,100) 

elif number1 == 3: 

   cow = range(100,1000) 

elif number1 == 4: 

   cow = range(1000,10000) 

elif number1 == 5: 

   cow = range(10000,100000) 

elif number1 == 6: 

   cow = range(100000,1000000) 

elif number1 == 7: 

   cow = range(1000000,10000000) 

elif number1 == 8: 

   cow = range(10000000,100000000) 

elif number1 == 9: 

   cow = range(100000000,1000000000)
elif number1 == 10: 

   cow = range(1000000000,10000000000) 

number3 = cow[-1] + 1 

n = 0 

while n < number3: 

   number4 = list(cow[n]) 

   n += 1

I am looking to make a loop so that for each element in the list, it will get broken down into each of its characters. For example, say the number 137 was in the list then it would be turned into [1,3,7]. Then I want to add these numbers together (I haven't started that bit yet but I have some idea of how to do it).

However, I keep getting the error message

TypeError: 'int' object is not iterable

when I try and run this.

What am I doing wrong?

1 Answer

0 votes
by (106k points)
edited by

The error you are getting is because the following line:-

number4 = list(cow[n])

What it is doing is it is trying to take cow[n], which returns an integer, and make it a list. But it is not work, as demonstrated below.

>>> a = 1 

>>> list(a) 

Traceback (most recent call last):

 File "<stdin>", line 1, in <module> 

TypeError: 'int' object is not iterable 

>>>

So what you should do is put cow[n] inside a list and error will be gone:

number4 = [cow[n]]

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
asked Jul 8, 2019 in Python by Sammy (47.6k points)

Browse Categories

...