"int" is not iterable (Python) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

"int" is not iterable (Python)

Здравствуйте, при написании кода возникла ошибка int is not iterable Код: from time import sleep from random import randint listNum = int(input("List lenght: ")) listSort =[] for i in listNum: rndElement = randint(1, listnum) print(rndElement)

25th May 2019, 12:17 PM
Павел Буримский
Павел Буримский - avatar
2 Answers
+ 4
for i in range(listNum): rndElement = randint(1, listNum) print(rndElement)
25th May 2019, 12:52 PM
Diego
Diego - avatar
+ 2
hi, the issue is that you take an int from input and try to iterate it with a for .. loop. I am not sure why you do the for loop, because you don't use "i" from the loop header. Try to do it without for loop, or if you really need it you must make the object you want to use iterable, for example a string. An other issue is that you use different spelling for variable names: rndElement = randint(1, listnum) should be: rndElement = randint(1, listNum) so your coplete revised and running code is: #from time import sleep <--- remove this, is not needed from random import randint listNum = int(input("List lenght: ")) #listSort =[] <--- remove this, is not needed #for i in listNum: <--- remove this, is not needed rndElement = randint(1, listNum) print(rndElement) *** if you can tell us what you are planning to do, we can give you some more support. Perhaps you want to collect some numbers in a while loop, and put these in a list that you can iterate and finally print out ?
25th May 2019, 12:24 PM
Lothar
Lothar - avatar