Why puting empty string to a variable? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why puting empty string to a variable?

why does it mean and why do we use it? ex: name = "" while name != "zoom": print("type your name") name = input() print("Thank you")

6th Nov 2016, 1:55 AM
Zoom
8 Answers
+ 4
In your case you have to initialise your variables before you check them in if statements!
6th Nov 2016, 4:56 AM
Ayoub Walsh Alden
Ayoub Walsh Alden - avatar
+ 2
Answered by others...here's the error Python reports if you don't define before checking: >>> while name != "zoom": ... pass ... Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'name' is not defined
6th Nov 2016, 7:00 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
@Zoom Because your code initializes the variable before first use. Mine doesn't. Empty string is because your next operation might be: name + "another string" If it were numeric or None, Python would throw an error and you'd have to convert, stop to initialize anyway or write a special case for first assignment (like if there's something in there already that you don't need). In short, it's done that way because it's simpler.
6th Nov 2016, 7:41 PM
Kirk Schafer
Kirk Schafer - avatar
+ 1
in this case an empty variable to store the input from the user, may be
6th Nov 2016, 2:15 AM
Mohamad Basori
Mohamad Basori - avatar
+ 1
Its like creating a box or empty container, which you plan to store variables in it later in your coding. That is to say, anything can fill the box. i hope my explanation is clear
24th Nov 2016, 3:06 PM
Oh Jay
Oh Jay - avatar
0
ok i get that part but how come when i run it in my computer i do not get this error...i do not get any error at all (on my pc)?
6th Nov 2016, 7:19 PM
Zoom
0
And also generally why do we assign empty strings to variables?
6th Nov 2016, 7:21 PM
Zoom
0
Ahhh ok...make sense now. Thank you very much for your explanation @Kirk Schafer
6th Nov 2016, 8:02 PM
Zoom