Name "word" is not defined. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Name "word" is not defined.

in for statement we have "word". but this variable is not defined. how do we not getting error? here is the code: words = ["hello", "world", "spam", "eggs"] for word in words: print(word + "!")

27th Oct 2017, 9:17 AM
sahil baranwal
sahil baranwal - avatar
5 Answers
+ 4
@Sahil...well,there is no need to define your variable word or any other variables in Python prior to using it ,like in other languages..eg: int a in other languages in Python just a is enough & you could store anything in a like int, float ,string
27th Oct 2017, 10:05 AM
Sri Lakshmi
Sri Lakshmi - avatar
+ 8
27th Oct 2017, 9:50 AM
Shamima Yasmin
Shamima Yasmin - avatar
+ 2
When we write like this:- "for i in range (9):" , "i" variable is defined instantly.
27th Oct 2017, 9:59 AM
Ahmmad Jobayer
Ahmmad Jobayer - avatar
+ 1
show us your code..it will be easy to explain
27th Oct 2017, 9:36 AM
Sri Lakshmi
Sri Lakshmi - avatar
0
There are 2 things which explain your case and also depict how python is awesome 1. word in your case is a reference variable which gets the reference of the elements of the list one by one iteratively 2. Python follows dynamic type casting that's the reason you do not need to give datatypes while assigning the variable.
16th Nov 2017, 5:46 PM
Utkarsh Dhawan