(Feb-01-2020, 08:25 PM)el_bueno Wrote: They should be predefined in order to use them, when defined they means true and if they aren't defined they means False and we get an error
Almost correct. When you use a variable that is not defined it is neither True nor False: you get an error:
>>> if a:
... print(True)
... else:
... print(False)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
If the variable is defined but it is empty string (or a zero integer) it means False:
>>> a = ""
>>> if a:
... print(True)
... else:
... print(False)
...
False
If the variable is defined and has a value it is true:
>>> a = "Hello"
>>> if a:
... print(True)
... else:
... print(False)
...
True
All these boolean side effects of variables stem from a long forgotten era of coding in machine language. In those days only 0 and 1 bits existed. 0 meant false. By coincidence 0 also meant 0. This is when programmers started to mis-use numbers as booleans thus obfuscating their programs. Now we have modern languages like Python and we do not have to rely on the side effects. So my advice is: use boolean variables if you mean to work with True and False. And else use string and int variables. This will make your program clear and easy to understand.
So I would advice to re-write your program like this.:
is_hungry = False
is_noteaten = True
is_full = False
if is_hungry:
print('Im hungry')
elif is_noteaten:
print("I didn't eat")
elif is_full:
print('Im full')
else:
print('I do not know what I feel')
Does this help you?