Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: Input() inside of a for loop error

Tags:

python

for i in range(5):
    mylist[i]=int(input("Input an integer: "))

Do I really still have to define mylist before the for loop before I can actually use it? At the first loop it works fine but at the second loop it will show a NameError do I have to use a different inut method or what?

NameError: name 'mylist' is not defined

like image 867
TF KALIBER Avatar asked Apr 11 '26 03:04

TF KALIBER


1 Answers

Yes, you need to define what "mylist" is before you assign values to it.

mylist = []
for i in range(5):
    mylist.append(int(input("Input an integer: ")))
like image 117
TimTam Avatar answered Apr 12 '26 16:04

TimTam