+ 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")
8 Respostas
+ 4
In your case you have to initialise your variables before you check them in if statements!
+ 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
+ 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.
+ 1
in this case an empty variable to store the input from the user, may be
+ 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
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)?
0
And also generally why do we assign empty strings to variables?
0
Ahhh ok...make sense now. Thank you very much for your explanation @Kirk Schafer